-
Notifications
You must be signed in to change notification settings - Fork 188
[SE-0456, -0467] Data+Span+MutableSpan #1276
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ | |
#endif | ||
|
||
internal import _FoundationCShims | ||
import Builtin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Builtin module is public by virtue of being inlined all over the standard library; in this file we're calling it from opaque code, so there's nothing being reexported at all. |
||
|
||
#if canImport(Darwin) | ||
import Darwin | ||
|
@@ -604,6 +605,7 @@ internal final class __DataStorage : @unchecked Sendable { | |
|
||
@frozen | ||
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *) | ||
@_addressableForDependencies | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any ABI or source stability impact for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no source stability impact. It only changes the mangling for functions that try to establish a lifetime dependence on |
||
public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollection, RangeReplaceableCollection, MutableDataProtocol, ContiguousBytes, Sendable { | ||
|
||
public typealias Index = Int | ||
|
@@ -2198,7 +2200,105 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect | |
public func withUnsafeBytes<ResultType>(_ body: (UnsafeRawBufferPointer) throws -> ResultType) rethrows -> ResultType { | ||
return try _representation.withUnsafeBytes(body) | ||
} | ||
|
||
|
||
#if compiler(>=6.2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we only support building this with the 6.2 compiler in the first place. |
||
@available(FoundationSpan 6.2, *) | ||
public var bytes: RawSpan { | ||
@lifetime(borrow self) | ||
borrowing get { | ||
let buffer: UnsafeRawBufferPointer | ||
switch _representation { | ||
case .empty: | ||
buffer = UnsafeRawBufferPointer(start: nil, count: 0) | ||
case .inline: | ||
buffer = unsafe UnsafeRawBufferPointer( | ||
start: UnsafeRawPointer(Builtin.addressOfBorrow(self)), | ||
count: _representation.count | ||
) | ||
case .large(let slice): | ||
buffer = unsafe UnsafeRawBufferPointer( | ||
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count | ||
) | ||
case .slice(let slice): | ||
buffer = unsafe UnsafeRawBufferPointer( | ||
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count | ||
) | ||
} | ||
let span = unsafe RawSpan(_unsafeBytes: buffer) | ||
return unsafe _overrideLifetime(span, borrowing: self) | ||
} | ||
} | ||
|
||
@available(FoundationSpan 6.2, *) | ||
public var span: Span<UInt8> { | ||
@lifetime(borrow self) | ||
borrowing get { | ||
let span = unsafe bytes._unsafeView(as: UInt8.self) | ||
return _overrideLifetime(span, borrowing: self) | ||
} | ||
} | ||
|
||
@available(FoundationSpan 6.2, *) | ||
public var mutableBytes: MutableRawSpan { | ||
@lifetime(&self) | ||
mutating get { | ||
let buffer: UnsafeMutableRawBufferPointer | ||
switch _representation { | ||
case .empty: | ||
buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0) | ||
case .inline: | ||
buffer = unsafe UnsafeMutableRawBufferPointer( | ||
start: UnsafeMutableRawPointer(Builtin.addressOfBorrow(self)), | ||
count: _representation.count | ||
) | ||
case .large(let slice): | ||
buffer = unsafe UnsafeMutableRawBufferPointer( | ||
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count | ||
) | ||
case .slice(let slice): | ||
buffer = unsafe UnsafeMutableRawBufferPointer( | ||
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count | ||
) | ||
} | ||
let span = unsafe MutableRawSpan(_unsafeBytes: buffer) | ||
return unsafe _overrideLifetime(span, mutating: &self) | ||
} | ||
} | ||
|
||
@available(FoundationSpan 6.2, *) | ||
public var mutableSpan: MutableSpan<UInt8> { | ||
@lifetime(&self) | ||
mutating get { | ||
#if false // see https://github.com/swiftlang/swift/issues/81218 | ||
var bytes = mutableBytes | ||
let span = unsafe bytes._unsafeMutableView(as: UInt8.self) | ||
return _overrideLifetime(span, mutating: &self) | ||
#else | ||
let buffer: UnsafeMutableRawBufferPointer | ||
switch _representation { | ||
case .empty: | ||
buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0) | ||
case .inline: | ||
buffer = unsafe UnsafeMutableRawBufferPointer( | ||
start: UnsafeMutableRawPointer(Builtin.addressOfBorrow(self)), | ||
count: _representation.count | ||
) | ||
case .large(let slice): | ||
buffer = unsafe UnsafeMutableRawBufferPointer( | ||
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count | ||
) | ||
case .slice(let slice): | ||
buffer = unsafe UnsafeMutableRawBufferPointer( | ||
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count | ||
) | ||
} | ||
let span = unsafe MutableSpan<UInt8>(_unsafeBytes: buffer) | ||
return unsafe _overrideLifetime(span, mutating: &self) | ||
#endif | ||
} | ||
} | ||
#endif | ||
|
||
@_alwaysEmitIntoClient | ||
public func withContiguousStorageIfAvailable<ResultType>(_ body: (_ buffer: UnsafeBufferPointer<UInt8>) throws -> ResultType) rethrows -> ResultType? { | ||
return try _representation.withUnsafeBytes { | ||
|
@@ -2848,3 +2948,62 @@ extension Data : Codable { | |
} | ||
} | ||
} | ||
|
||
#if compiler(>=6.2) | ||
// TODO: remove once _overrideLifetime is public in the standard library | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When will that happen? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the PR: swiftlang/swift#81225 |
||
|
||
/// Unsafely discard any lifetime dependency on the `dependent` argument. Return | ||
/// a value identical to `dependent` with a lifetime dependency on the caller's | ||
/// borrow scope of the `source` argument. | ||
@unsafe | ||
@_unsafeNonescapableResult | ||
@_alwaysEmitIntoClient | ||
@_transparent | ||
@lifetime(borrow source) | ||
internal func _overrideLifetime< | ||
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable | ||
>( | ||
_ dependent: consuming T, borrowing source: borrowing U | ||
) -> T { | ||
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence | ||
// should be expressed by a builtin that is hidden within the function body. | ||
dependent | ||
} | ||
|
||
/// Unsafely discard any lifetime dependency on the `dependent` argument. Return | ||
/// a value identical to `dependent` that inherits all lifetime dependencies from | ||
/// the `source` argument. | ||
@unsafe | ||
@_unsafeNonescapableResult | ||
@_alwaysEmitIntoClient | ||
@_transparent | ||
@lifetime(copy source) | ||
internal func _overrideLifetime< | ||
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable | ||
>( | ||
_ dependent: consuming T, copying source: borrowing U | ||
) -> T { | ||
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence | ||
// should be expressed by a builtin that is hidden within the function body. | ||
dependent | ||
} | ||
|
||
/// Unsafely discard any lifetime dependency on the `dependent` argument. | ||
/// Return a value identical to `dependent` with a lifetime dependency | ||
/// on the caller's exclusive borrow scope of the `source` argument. | ||
@unsafe | ||
@_unsafeNonescapableResult | ||
@_alwaysEmitIntoClient | ||
@_transparent | ||
@lifetime(&source) | ||
internal func _overrideLifetime< | ||
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable | ||
>( | ||
_ dependent: consuming T, | ||
mutating source: inout U | ||
) -> T { | ||
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence | ||
// should be expressed by a builtin that is hidden within the function body. | ||
dependent | ||
} | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need a new availability macro?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This requires a 6.2 toolchain to get the symbols, and won't have the same range of deployment targets as any of the others, as far as I can tell.