Skip to content

SE-0138: Add UnsafeRawBufferPointer and UnsafeMutableRawBufferPointer… #4969

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 5 commits into from
Sep 29, 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
67 changes: 67 additions & 0 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,73 @@ 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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc comment summaries are single sentence fragment.

///
/// ${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
/// `withUnsafeMutableBytes(_:)` 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: (UnsafeMutableRawBufferPointer) throws -> R
) rethrows -> R {
return try self.withUnsafeMutableBufferPointer {
return try body(UnsafeMutableRawBufferPointer($0))
}
}

/// 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 @@ -127,6 +127,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 @@ -119,7 +119,8 @@
"Pointer.swift",
"UnsafePointer.swift",
"UnsafeRawPointer.swift",
"UnsafeBufferPointer.swift"
"UnsafeBufferPointer.swift",
"UnsafeRawBufferPointer.swift"
],
"Protocols": [
"CompilerProtocols.swift",
Expand Down
Loading