|
| 1 | +/* |
| 2 | + * Copyright 2025, gRPC Authors All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +public import GRPCCore // internal but @usableFromInline |
| 18 | +public import SwiftProtobuf // internal but @usableFromInline |
| 19 | + |
| 20 | +/// Brides between `GRPCContiguousBytes` and `SwiftProtobufContiguousBytes` which have the same |
| 21 | +/// requirements. |
| 22 | +/// |
| 23 | +/// This is necessary as `SwiftProtobufContiguousBytes` can't be the protocol in the gRPC API (as |
| 24 | +/// it'd require a dependency on Protobuf in the core package), and `GRPCContiguousBytes` can't |
| 25 | +/// refine `SwiftProtobufContiguousBytes` for the same reason. |
| 26 | +@usableFromInline |
| 27 | +struct ContiguousBytesAdapter< |
| 28 | + Bytes: GRPCContiguousBytes |
| 29 | +>: GRPCContiguousBytes, SwiftProtobufContiguousBytes { |
| 30 | + @usableFromInline |
| 31 | + var bytes: Bytes |
| 32 | + |
| 33 | + @inlinable |
| 34 | + init(_ bytes: Bytes) { |
| 35 | + self.bytes = bytes |
| 36 | + } |
| 37 | + |
| 38 | + @inlinable |
| 39 | + init(repeating: UInt8, count: Int) { |
| 40 | + self.bytes = Bytes(repeating: repeating, count: count) |
| 41 | + } |
| 42 | + |
| 43 | + @inlinable |
| 44 | + init(_ sequence: some Sequence<UInt8>) { |
| 45 | + self.bytes = Bytes(sequence) |
| 46 | + } |
| 47 | + |
| 48 | + @inlinable |
| 49 | + var count: Int { |
| 50 | + self.bytes.count |
| 51 | + } |
| 52 | + |
| 53 | + @inlinable |
| 54 | + func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { |
| 55 | + try self.bytes.withUnsafeBytes(body) |
| 56 | + } |
| 57 | + |
| 58 | + @inlinable |
| 59 | + mutating func withUnsafeMutableBytes<R>( |
| 60 | + _ body: (UnsafeMutableRawBufferPointer) throws -> R |
| 61 | + ) rethrows -> R { |
| 62 | + try self.bytes.withUnsafeMutableBytes(body) |
| 63 | + } |
| 64 | +} |
0 commit comments