Skip to content

SE-0138: Add UnsafeRawBufferPointer and UnsafeMutableRawBufferPointer. #4954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,72 @@ public func != <Element : Equatable>(
) -> Bool {
return !(lhs == rhs)
}

extension ${Self} {
/// Calls a closure with a view of the array's underlying bytes of memory as a
/// Collection of `UInt8`.
/// ${contiguousCaveat}
///
/// - Precondition: `Pointee` is a trivial type.
///
/// The following example shows how you copy bytes into an array:
///
/// var numbers = [Int32](repeating: 0, count: 2)
/// var byteValues: [UInt8] = [0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]
/// numbers.withUnsafeMutableBytes { destBytes in
/// byteValues.withUnsafeBytes { srcBytes in
/// destBytes.copyBytes(from: srcBytes)
/// }
/// }
///
/// - Parameter body: A closure with an `UnsafeRawBufferPointer` parameter
/// that points to the contiguous storage for the array. If `body` has a
/// return value, it is used as the return value for the
/// `withUnsafeBytes(_:)` method. The argument is valid only for the
/// duration of the closure's execution.
/// - Returns: The return value of the `body` closure parameter, if any.
///
/// - SeeAlso: `withUnsafeBytes`, `UnsafeMutableRawBufferPointer`
public mutating func withUnsafeMutableBytes<R>(
_ body: (inout UnsafeMutableRawBufferPointer) throws -> R
) rethrows -> R {
return try self.withUnsafeMutableBufferPointer {
var bytes = UnsafeMutableRawBufferPointer($0)
return try body(&bytes)
}
}

/// Calls a closure with a view of the array's underlying bytes of memory
/// as a Collection of `UInt8`.
/// ${contiguousCaveat}
///
/// - Precondition: `Pointee` is a trivial type.
///
/// The following example shows how you copy the contents of an array into a
/// buffer of `UInt8`:
///
/// let numbers = [1, 2, 3]
/// var byteBuffer = [UInt8]()
/// numbers.withUnsafeBytes {
/// byteBuffer += $0
/// }
///
/// - Parameter body: A closure with an `UnsafeRawBufferPointer` parameter
/// that points to the contiguous storage for the array. If `body` has a
/// return value, it is used as the return value for the
/// `withUnsafeBytes(_:)` method. The argument is valid only for the
/// duration of the closure's execution.
/// - Returns: The return value of the `body` closure parameter, if any.
///
/// - SeeAlso: `withUnsafeBytes`, `UnsafeRawBufferPointer`
public mutating func withUnsafeBytes<R>(
_ body: (UnsafeRawBufferPointer) throws -> R
) rethrows -> R {
return try self.withUnsafeBufferPointer {
try body(UnsafeRawBufferPointer($0))
}
}
}
%end

#if _runtime(_ObjC)
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ set(SWIFTLIB_ESSENTIAL
Unmanaged.swift
UnsafeBitMap.swift
UnsafeBufferPointer.swift.gyb
UnsafeRawBufferPointer.swift.gyb
UnsafePointer.swift.gyb
UnsafeRawPointer.swift.gyb
WriteBackMutableSlice.swift
Expand Down
3 changes: 2 additions & 1 deletion stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
"Pointer.swift",
"UnsafePointer.swift",
"UnsafeRawPointer.swift",
"UnsafeBufferPointer.swift"
"UnsafeBufferPointer.swift",
"UnsafeRawBufferPointer.swift"
],
"Protocols": [
"CompilerProtocols.swift",
Expand Down
Loading