Skip to content

Commit 96fd749

Browse files
committed
[stdlib] Generalize unsafe buffer pointer operations on Slice
`Slice` continues to require `Element` to be copyable, so this is limited to`moveInitializeMemory`, `bindMemory`, `withMemoryRebound` and `assumingMemoryBound`. Generalizing these restores parity with the corresponding operations on unsliced buffer pointers, and reduces the need to artificially rebase the slices only to call these.
1 parent 989ea16 commit 96fd749

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

stdlib/public/core/UnsafeBufferPointerSlice.swift

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ extension Slice where Base == UnsafeMutableRawBufferPointer {
161161
@discardableResult
162162
@inlinable
163163
@_alwaysEmitIntoClient
164-
public func moveInitializeMemory<T>(
164+
public func moveInitializeMemory<T: ~Copyable>(
165165
as type: T.Type,
166166
fromContentsOf source: UnsafeMutableBufferPointer<T>
167167
) -> UnsafeMutableBufferPointer<T> {
@@ -228,7 +228,9 @@ extension Slice where Base == UnsafeMutableRawBufferPointer {
228228
@discardableResult
229229
@inlinable
230230
@_alwaysEmitIntoClient
231-
public func bindMemory<T>(to type: T.Type) -> UnsafeMutableBufferPointer<T> {
231+
public func bindMemory<T: ~Copyable>(
232+
to type: T.Type
233+
) -> UnsafeMutableBufferPointer<T> {
232234
let buffer = unsafe Base(rebasing: self)
233235
return unsafe buffer.bindMemory(to: T.self)
234236
}
@@ -279,8 +281,9 @@ extension Slice where Base == UnsafeMutableRawBufferPointer {
279281
/// - Returns: The return value, if any, of the `body` closure parameter.
280282
@inlinable
281283
@_alwaysEmitIntoClient
282-
public func withMemoryRebound<T, Result, E: Error>(
283-
to type: T.Type, _ body: (UnsafeMutableBufferPointer<T>) throws(E) -> Result
284+
public func withMemoryRebound<T: ~Copyable, E: Error, Result: ~Copyable>(
285+
to type: T.Type,
286+
_ body: (UnsafeMutableBufferPointer<T>) throws(E) -> Result
284287
) throws(E) -> Result {
285288
let buffer = unsafe Base(rebasing: self)
286289
return try unsafe buffer.withMemoryRebound(to: T.self, body)
@@ -304,7 +307,7 @@ extension Slice where Base == UnsafeMutableRawBufferPointer {
304307
/// - Returns: A typed pointer to the same memory as this raw pointer.
305308
@inlinable
306309
@_alwaysEmitIntoClient
307-
public func assumingMemoryBound<T>(
310+
public func assumingMemoryBound<T: ~Copyable>(
308311
to type: T.Type
309312
) -> UnsafeMutableBufferPointer<T> {
310313
let buffer = unsafe Base(rebasing: self)
@@ -465,7 +468,9 @@ extension Slice where Base == UnsafeRawBufferPointer {
465468
@discardableResult
466469
@inlinable
467470
@_alwaysEmitIntoClient
468-
public func bindMemory<T>(to type: T.Type) -> UnsafeBufferPointer<T> {
471+
public func bindMemory<T: ~Copyable>(
472+
to type: T.Type
473+
) -> UnsafeBufferPointer<T> {
469474
let buffer = unsafe Base(rebasing: self)
470475
return unsafe buffer.bindMemory(to: T.self)
471476
}
@@ -516,8 +521,9 @@ extension Slice where Base == UnsafeRawBufferPointer {
516521
/// - Returns: The return value, if any, of the `body` closure parameter.
517522
@inlinable
518523
@_alwaysEmitIntoClient
519-
public func withMemoryRebound<T, Result, E: Error>(
520-
to type: T.Type, _ body: (UnsafeBufferPointer<T>) throws(E) -> Result
524+
public func withMemoryRebound<T: ~Copyable, E: Error, Result: ~Copyable>(
525+
to type: T.Type,
526+
_ body: (UnsafeBufferPointer<T>) throws(E) -> Result
521527
) throws(E) -> Result {
522528
let buffer = unsafe Base(rebasing: self)
523529
return try unsafe buffer.withMemoryRebound(to: T.self, body)
@@ -541,7 +547,7 @@ extension Slice where Base == UnsafeRawBufferPointer {
541547
/// - Returns: A typed pointer to the same memory as this raw pointer.
542548
@inlinable
543549
@_alwaysEmitIntoClient
544-
public func assumingMemoryBound<T>(
550+
public func assumingMemoryBound<T: ~Copyable>(
545551
to type: T.Type
546552
) -> UnsafeBufferPointer<T> {
547553
let buffer = unsafe Base(rebasing: self)
@@ -693,9 +699,14 @@ extension Slice {
693699
/// - Returns: The return value, if any, of the `body` closure parameter.
694700
@inlinable
695701
@_alwaysEmitIntoClient
696-
public func withMemoryRebound<T, Result, Element>(
697-
to type: T.Type, _ body: (UnsafeBufferPointer<T>) throws -> Result
698-
) rethrows -> Result where Base == UnsafeBufferPointer<Element> {
702+
public func withMemoryRebound<
703+
T: ~Copyable, E: Error, Result: ~Copyable, Element
704+
>(
705+
to type: T.Type,
706+
_ body: (UnsafeBufferPointer<T>) throws(E) -> Result
707+
) throws(E) -> Result
708+
where Base == UnsafeBufferPointer<Element>
709+
{
699710
let rebased = unsafe UnsafeBufferPointer<Element>(rebasing: self)
700711
return try unsafe rebased.withMemoryRebound(to: T.self, body)
701712
}
@@ -1136,9 +1147,14 @@ extension Slice {
11361147
/// - Returns: The return value, if any, of the `body` closure parameter.
11371148
@inlinable
11381149
@_alwaysEmitIntoClient
1139-
public func withMemoryRebound<T, Result, Element>(
1140-
to type: T.Type, _ body: (UnsafeMutableBufferPointer<T>) throws -> Result
1141-
) rethrows -> Result where Base == UnsafeMutableBufferPointer<Element> {
1150+
public func withMemoryRebound<
1151+
T: ~Copyable, E: Error, Result: ~Copyable, Element
1152+
>(
1153+
to type: T.Type,
1154+
_ body: (UnsafeMutableBufferPointer<T>) throws(E) -> Result
1155+
) throws(E) -> Result
1156+
where Base == UnsafeMutableBufferPointer<Element>
1157+
{
11421158
try unsafe Base(rebasing: self).withMemoryRebound(to: T.self, body)
11431159
}
11441160

0 commit comments

Comments
 (0)