Skip to content

Commit 5ae7571

Browse files
committed
[stdlib] UnsafeRawBufferPointer: new interactions with noncopyable types
1 parent d15d500 commit 5ae7571

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public struct Unsafe${Mutable}RawBufferPointer {
120120
}
121121
}
122122

123+
@available(*, unavailable)
124+
extension Unsafe${Mutable}RawBufferPointer: Sendable {}
125+
123126
%if not mutable:
124127
extension UnsafeRawBufferPointer {
125128
/// An iterator over the bytes viewed by a raw buffer pointer.
@@ -136,6 +139,9 @@ extension UnsafeRawBufferPointer {
136139
}
137140
}
138141

142+
@available(*, unavailable)
143+
extension UnsafeRawBufferPointer.Iterator: Sendable { }
144+
139145
extension UnsafeRawBufferPointer.Iterator: IteratorProtocol, Sequence {
140146
/// Advances to the next byte and returns it, or `nil` if no next byte
141147
/// exists.
@@ -400,6 +406,9 @@ extension Unsafe${Mutable}RawBufferPointer {
400406
return baseAddress!.load(fromByteOffset: offset, as: T.self)
401407
}
402408

409+
// FIXME(NCG): Add a consuming analogue of `load`, like `move(fromByteOffset:as:_:)`
410+
// FIXME(NCG): Add a borrow analogue of `load`, like `withBorrow(fromByteOffset:as:_:)`
411+
403412
/// Returns a new instance of the given type, constructed from the raw memory
404413
/// at the specified offset.
405414
///
@@ -431,7 +440,6 @@ extension Unsafe${Mutable}RawBufferPointer {
431440
/// - Returns: A new instance of type `T`, copied from the buffer pointer's
432441
/// memory.
433442
#if $BitwiseCopyable
434-
@inlinable
435443
@_alwaysEmitIntoClient
436444
public func loadUnaligned<T : _BitwiseCopyable>(
437445
fromByteOffset offset: Int = 0,
@@ -443,7 +451,7 @@ extension Unsafe${Mutable}RawBufferPointer {
443451
return baseAddress!.loadUnaligned(fromByteOffset: offset, as: T.self)
444452
}
445453
#endif
446-
@inlinable
454+
447455
@_alwaysEmitIntoClient
448456
public func loadUnaligned<T>(
449457
fromByteOffset offset: Int = 0,
@@ -508,7 +516,7 @@ extension Unsafe${Mutable}RawBufferPointer {
508516
// This unavailable implementation uses the expected mangled name
509517
// of `storeBytes<T>(of:toByteOffset:as:)`, and provides an entry point for
510518
// any binary linked against the stdlib binary for Swift 5.6 and older.
511-
@available(*, unavailable)
519+
@_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 2)
512520
@_silgen_name("$sSw10storeBytes2of12toByteOffset2asyx_SixmtlF")
513521
@usableFromInline func _legacy_se0349_storeBytes<T>(
514522
of value: T, toByteOffset offset: Int = 0, as type: T.Type
@@ -620,7 +628,8 @@ extension Unsafe${Mutable}RawBufferPointer {
620628
/// - Parameter buffer: The typed buffer to convert to a raw buffer. The
621629
/// buffer's type `T` must be a trivial type.
622630
@inlinable
623-
public init<T>(_ buffer: UnsafeMutableBufferPointer<T>) {
631+
@_preInverseGenerics
632+
public init<T: ~Copyable>(_ buffer: UnsafeMutableBufferPointer<T>) {
624633
self.init(start: buffer.baseAddress,
625634
count: buffer.count * MemoryLayout<T>.stride)
626635
}
@@ -631,7 +640,8 @@ extension Unsafe${Mutable}RawBufferPointer {
631640
/// - Parameter buffer: The typed buffer to convert to a raw buffer. The
632641
/// buffer's type `T` must be a trivial type.
633642
@inlinable
634-
public init<T>(_ buffer: UnsafeBufferPointer<T>) {
643+
@_preInverseGenerics
644+
public init<T: ~Copyable>(_ buffer: UnsafeBufferPointer<T>) {
635645
self.init(start: buffer.baseAddress,
636646
count: buffer.count * MemoryLayout<T>.stride)
637647
}
@@ -934,7 +944,7 @@ extension Unsafe${Mutable}RawBufferPointer {
934944
@discardableResult
935945
@inlinable
936946
@_alwaysEmitIntoClient
937-
public func moveInitializeMemory<T>(
947+
public func moveInitializeMemory<T: ~Copyable>(
938948
as type: T.Type,
939949
fromContentsOf source: UnsafeMutableBufferPointer<T>
940950
) -> UnsafeMutableBufferPointer<T> {
@@ -985,7 +995,6 @@ extension Unsafe${Mutable}RawBufferPointer {
985995
/// The returned buffer references memory starting at the same
986996
/// base address as this buffer, and its count is equal to `source.count`.
987997
@discardableResult
988-
@inlinable
989998
@_alwaysEmitIntoClient
990999
public func moveInitializeMemory<T>(
9911000
as type: T.Type,
@@ -997,8 +1006,8 @@ extension Unsafe${Mutable}RawBufferPointer {
9971006

9981007
% end # mutable
9991008

1000-
/// Binds this buffer’s memory to the specified type and returns a typed buffer
1001-
/// of the bound memory.
1009+
/// Binds this buffer’s memory to the specified type and returns a typed
1010+
/// buffer of the bound memory.
10021011
///
10031012
/// Use the `bindMemory(to:)` method to bind the memory referenced
10041013
/// by this buffer to the type `T`. The memory must be uninitialized or
@@ -1015,9 +1024,11 @@ extension Unsafe${Mutable}RawBufferPointer {
10151024
/// region is bound to `T`, but has not been modified in any other way.
10161025
/// The typed buffer references `self.count / MemoryLayout<T>.stride`
10171026
/// instances of `T`.
1027+
@inlinable
10181028
@_transparent
1029+
@_preInverseGenerics
10191030
@discardableResult
1020-
public func bindMemory<T>(
1031+
public func bindMemory<T: ~Copyable>(
10211032
to type: T.Type
10221033
) -> Unsafe${Mutable}BufferPointer<T> {
10231034
guard let base = _position else {
@@ -1076,7 +1087,7 @@ extension Unsafe${Mutable}RawBufferPointer {
10761087
/// - Returns: The return value, if any, of the `body` closure parameter.
10771088
@inlinable
10781089
@_alwaysEmitIntoClient
1079-
public func withMemoryRebound<T, Result, E: Error>(
1090+
public func withMemoryRebound<T: ~Copyable, Result: ~Copyable, E: Error>(
10801091
to type: T.Type,
10811092
_ body: (_ buffer: Unsafe${Mutable}BufferPointer<T>) throws(E) -> Result
10821093
) throws(E) -> Result {
@@ -1114,7 +1125,7 @@ extension Unsafe${Mutable}RawBufferPointer {
11141125
/// - Returns: A typed pointer to the same memory as this raw pointer.
11151126
@inlinable
11161127
@_alwaysEmitIntoClient
1117-
public func assumingMemoryBound<T>(
1128+
public func assumingMemoryBound<T: ~Copyable>(
11181129
to: T.Type
11191130
) -> Unsafe${Mutable}BufferPointer<T> {
11201131
guard let s = _position else {
@@ -1130,7 +1141,7 @@ extension Unsafe${Mutable}RawBufferPointer {
11301141
% if Mutable:
11311142
@inlinable
11321143
@_alwaysEmitIntoClient
1133-
public func withContiguousMutableStorageIfAvailable<R>(
1144+
public func withContiguousMutableStorageIfAvailable<R: ~Copyable>(
11341145
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
11351146
) rethrows -> R? {
11361147
#if $TypedThrows
@@ -1152,7 +1163,7 @@ extension Unsafe${Mutable}RawBufferPointer {
11521163
% end
11531164
@inlinable
11541165
@_alwaysEmitIntoClient
1155-
public func withContiguousStorageIfAvailable<R>(
1166+
public func withContiguousStorageIfAvailable<R: ~Copyable>(
11561167
_ body: (UnsafeBufferPointer<Element>) throws -> R
11571168
) rethrows -> R? {
11581169
#if $TypedThrows
@@ -1216,7 +1227,6 @@ extension ${Self} {
12161227
/// function. The buffer pointer argument is valid only for the duration
12171228
/// of the closure's execution.
12181229
/// - Returns: The return value, if any, of the `body` closure.
1219-
@inlinable
12201230
@_alwaysEmitIntoClient
12211231
public func withUnsafeMutableBytes<T, E: Error, Result>(
12221232
of value: inout T,
@@ -1281,7 +1291,6 @@ public func _withUnprotectedUnsafeMutableBytes<T, E: Error, Result>(
12811291
/// If you want to mutate a value by writing through a pointer, use
12821292
/// `withUnsafeMutableBytes(of:_:)` instead.
12831293
/// - Returns: The return value, if any, of the `body` closure.
1284-
@inlinable
12851294
@_alwaysEmitIntoClient
12861295
public func withUnsafeBytes<T, E: Error, Result>(
12871296
of value: inout T,
@@ -1341,7 +1350,6 @@ public func _withUnprotectedUnsafeBytes<T, E: Error, Result>(
13411350
/// If you want to mutate a value by writing through a pointer, use
13421351
/// `withUnsafeMutableBytes(of:_:)` instead.
13431352
/// - Returns: The return value, if any, of the `body` closure.
1344-
@inlinable
13451353
@_alwaysEmitIntoClient
13461354
public func withUnsafeBytes<T, E: Error, Result>(
13471355
of value: T,
@@ -1383,14 +1391,6 @@ public func _withUnprotectedUnsafeBytes<T, E: Error, Result>(
13831391
return try body(buffer)
13841392
}
13851393

1386-
@available(*, unavailable)
1387-
extension UnsafeRawBufferPointer: Sendable { }
1388-
@available(*, unavailable)
1389-
extension UnsafeRawBufferPointer.Iterator: Sendable { }
1390-
@available(*, unavailable)
1391-
extension UnsafeMutableRawBufferPointer: Sendable { }
1392-
1393-
13941394
// ${'Local Variables'}:
13951395
// eval: (read-only-mode 1)
13961396
// End:

0 commit comments

Comments
 (0)