|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +//===--- ContiguousBytes --------------------------------------------------===// |
| 14 | + |
| 15 | +/// Indicates that the conforming type is a contiguous collection of raw bytes |
| 16 | +/// whose underlying storage is directly accessible by withUnsafeBytes. |
| 17 | +public protocol ContiguousBytes { |
| 18 | + /// Calls the given closure with the contents of underlying storage. |
| 19 | + /// |
| 20 | + /// - note: Calling `withUnsafeBytes` multiple times does not guarantee that |
| 21 | + /// the same buffer pointer will be passed in every time. |
| 22 | + /// - warning: The buffer argument to the body should not be stored or used |
| 23 | + /// outside of the lifetime of the call to the closure. |
| 24 | + func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R |
| 25 | +} |
| 26 | + |
| 27 | +//===--- Collection Conformances ------------------------------------------===// |
| 28 | + |
| 29 | +// FIXME: When possible, expand conformance to `where Element : Trivial`. |
| 30 | +extension Array : ContiguousBytes where Element == UInt8 { } |
| 31 | + |
| 32 | +// FIXME: When possible, expand conformance to `where Element : Trivial`. |
| 33 | +extension ArraySlice : ContiguousBytes where Element == UInt8 { } |
| 34 | + |
| 35 | +// FIXME: When possible, expand conformance to `where Element : Trivial`. |
| 36 | +extension ContiguousArray : ContiguousBytes where Element == UInt8 { } |
| 37 | + |
| 38 | +//===--- Pointer Conformances ---------------------------------------------===// |
| 39 | + |
| 40 | +extension UnsafeRawBufferPointer : ContiguousBytes { |
| 41 | + @inlinable |
| 42 | + public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { |
| 43 | + return try body(self) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +extension UnsafeMutableRawBufferPointer : ContiguousBytes { |
| 48 | + @inlinable |
| 49 | + public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { |
| 50 | + return try body(UnsafeRawBufferPointer(self)) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// FIXME: When possible, expand conformance to `where Element : Trivial`. |
| 55 | +extension UnsafeBufferPointer : ContiguousBytes where Element == UInt8 { |
| 56 | + @inlinable |
| 57 | + public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { |
| 58 | + return try body(UnsafeRawBufferPointer(self)) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// FIXME: When possible, expand conformance to `where Element : Trivial`. |
| 63 | +extension UnsafeMutableBufferPointer : ContiguousBytes where Element == UInt8 { |
| 64 | + @inlinable |
| 65 | + public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { |
| 66 | + return try body(UnsafeRawBufferPointer(self)) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// FIXME: When possible, expand conformance to `where Element : Trivial`. |
| 71 | +extension EmptyCollection : ContiguousBytes where Element == UInt8 { |
| 72 | + @inlinable |
| 73 | + public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { |
| 74 | + return try body(UnsafeRawBufferPointer(start: nil, count: 0)) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// FIXME: When possible, expand conformance to `where Element : Trivial`. |
| 79 | +extension CollectionOfOne : ContiguousBytes where Element == UInt8 { |
| 80 | + @inlinable |
| 81 | + public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R { |
| 82 | + let element = self.first! |
| 83 | + return try Swift.withUnsafeBytes(of: element) { |
| 84 | + return try body($0) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +//===--- Conditional Conformances -----------------------------------------===// |
| 90 | + |
| 91 | +extension Slice : ContiguousBytes where Base : ContiguousBytes { |
| 92 | + public func withUnsafeBytes<ResultType>(_ body: (UnsafeRawBufferPointer) throws -> ResultType) rethrows -> ResultType { |
| 93 | + let offset = base.distance(from: base.startIndex, to: self.startIndex) |
| 94 | + return try base.withUnsafeBytes { ptr in |
| 95 | + let slicePtr = ptr.baseAddress?.advanced(by: offset) |
| 96 | + let sliceBuffer = UnsafeRawBufferPointer(start: slicePtr, count: self.count) |
| 97 | + return try body(sliceBuffer) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments