Skip to content

Commit 303b277

Browse files
authored
Merge pull request #4969 from atrick/swift-3.0-branch
SE-0138: Add UnsafeRawBufferPointer and UnsafeMutableRawBufferPointer…
2 parents ccbe629 + d989323 commit 303b277

File tree

8 files changed

+1267
-113
lines changed

8 files changed

+1267
-113
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,73 @@ public func != <Element : Equatable>(
20872087
) -> Bool {
20882088
return !(lhs == rhs)
20892089
}
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+
}
20902157
%end
20912158

20922159
#if _runtime(_ObjC)

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ set(SWIFTLIB_ESSENTIAL
127127
Unmanaged.swift
128128
UnsafeBitMap.swift
129129
UnsafeBufferPointer.swift.gyb
130+
UnsafeRawBufferPointer.swift.gyb
130131
UnsafePointer.swift.gyb
131132
UnsafeRawPointer.swift.gyb
132133
WriteBackMutableSlice.swift

stdlib/public/core/GroupInfo.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@
119119
"Pointer.swift",
120120
"UnsafePointer.swift",
121121
"UnsafeRawPointer.swift",
122-
"UnsafeBufferPointer.swift"
122+
"UnsafeBufferPointer.swift",
123+
"UnsafeRawBufferPointer.swift"
123124
],
124125
"Protocols": [
125126
"CompilerProtocols.swift",

0 commit comments

Comments
 (0)