@@ -2087,6 +2087,73 @@ public func != <Element : Equatable>(
2087
2087
) - > Bool {
2088
2088
return !( lhs == rhs)
2089
2089
}
2090
+
2091
+ extension ${ Self} {
2092
+ /// Calls a closure with a view of the array's underlying bytes of memory as a
2093
+ /// Collection of `UInt8`.
2094
+ ///
2095
+ /// ${contiguousCaveat}
2096
+ ///
2097
+ /// - Precondition: `Pointee` is a trivial type.
2098
+ ///
2099
+ /// The following example shows how you copy bytes into an array:
2100
+ ///
2101
+ /// var numbers = [Int32](repeating: 0, count: 2)
2102
+ /// var byteValues: [UInt8] = [0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]
2103
+ /// numbers.withUnsafeMutableBytes { destBytes in
2104
+ /// byteValues.withUnsafeBytes { srcBytes in
2105
+ /// destBytes.copyBytes(from: srcBytes)
2106
+ /// }
2107
+ /// }
2108
+ ///
2109
+ /// - Parameter body: A closure with an `UnsafeRawBufferPointer`
2110
+ /// parameter that points to the contiguous storage for the array. If `body`
2111
+ /// has a return value, it is used as the return value for the
2112
+ /// `withUnsafeMutableBytes(_:)` method. The argument is valid only for the
2113
+ /// duration of the closure's execution.
2114
+ /// - Returns: The return value of the `body` closure parameter, if any.
2115
+ ///
2116
+ /// - SeeAlso: `withUnsafeBytes`, `UnsafeMutableRawBufferPointer`
2117
+ public mutating func withUnsafeMutableBytes< R > (
2118
+ _ body: ( UnsafeMutableRawBufferPointer) throws -> R
2119
+ ) rethrows -> R {
2120
+ return try self . withUnsafeMutableBufferPointer {
2121
+ return try body ( UnsafeMutableRawBufferPointer ( $0) )
2122
+ }
2123
+ }
2124
+
2125
+ /// Calls a closure with a view of the array's underlying bytes of memory
2126
+ /// as a Collection of `UInt8`.
2127
+ ///
2128
+ /// ${contiguousCaveat}
2129
+ ///
2130
+ /// - Precondition: `Pointee` is a trivial type.
2131
+ ///
2132
+ /// The following example shows how you copy the contents of an array into a
2133
+ /// buffer of `UInt8`:
2134
+ ///
2135
+ /// let numbers = [1, 2, 3]
2136
+ /// var byteBuffer = [UInt8]()
2137
+ /// numbers.withUnsafeBytes {
2138
+ /// byteBuffer += $0
2139
+ /// }
2140
+ ///
2141
+ /// - Parameter body: A closure with an `UnsafeRawBufferPointer` parameter
2142
+ /// that points to the contiguous storage for the array. If `body` has a
2143
+ /// return value, it is used as the return value for the
2144
+ /// `withUnsafeBytes(_:)` method. The argument is valid only for the
2145
+ /// duration of the closure's execution.
2146
+ /// - Returns: The return value of the `body` closure parameter, if any.
2147
+ ///
2148
+ /// - SeeAlso: `withUnsafeBytes`, `UnsafeRawBufferPointer`
2149
+ public mutating func withUnsafeBytes< R> (
2150
+ _ body: ( UnsafeRawBufferPointer ) throws -> R
2151
+ ) rethrows -> R {
2152
+ return try self . withUnsafeBufferPointer {
2153
+ try body ( UnsafeRawBufferPointer ( $0) )
2154
+ }
2155
+ }
2156
+ }
2090
2157
% end
2091
2158
2092
2159
#if _runtime(_ObjC)
0 commit comments