mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Allow lifetime depenendence on types that are BitwiseCopyable & Escapable.
This is unsafe in the sense that the compiler will not diagnose any use of the
dependent value outside of the lexcial scope of the source value. But, in
practice, dependence on an UnsafePointer is often needed. In that case, the
programmer should have already taken responsibility for ensuring the lifetime of the
pointer over all dependent uses. Typically, an unsafe pointer is valid for the
duration of a closure. Lifetime dependence prevents the dependent value from
being returned by the closure, so common usage is safe by default.
Typical example:
func decode(_ bufferRef: Span<Int>) { /*...*/ }
extension UnsafeBufferPointer {
// The client must ensure the lifetime of the buffer across the invocation of `body`.
// The client must ensure that no code modifies the buffer during the invocation of `body`.
func withUnsafeSpan<Result>(_ body: (Span<Element>) throws -> Result) rethrows -> Result {
// Construct Span using its internal, unsafe API.
try body(Span(unsafePointer: baseAddress!, count: count))
}
}
func decodeArrayAsUBP(array: [Int]) {
array.withUnsafeBufferPointer { buffer in
buffer.withUnsafeSpan {
decode($0)
}
}
}
In the future, we may add SILGen support for tracking the lexical scope of
BitwiseCopyable values. That would allow them to have the same dependence
behavior as other source values.
67 lines
1.4 KiB
Swift
67 lines
1.4 KiB
Swift
public struct BufferView : ~Escapable {
|
|
public let ptr: UnsafeRawBufferPointer
|
|
public let c: Int
|
|
public init(_ ptr: UnsafeRawBufferPointer, _ c: Int) -> dependsOn(ptr) Self {
|
|
self.ptr = ptr
|
|
self.c = c
|
|
}
|
|
@inlinable
|
|
public init(_ otherBV: borrowing BufferView) {
|
|
self.ptr = otherBV.ptr
|
|
self.c = otherBV.c
|
|
}
|
|
}
|
|
|
|
public struct MutableBufferView : ~Escapable, ~Copyable {
|
|
let ptr: UnsafeMutableRawBufferPointer
|
|
let c: Int
|
|
public init(_ ptr: UnsafeMutableRawBufferPointer, _ c: Int) -> dependsOn(ptr) Self {
|
|
self.ptr = ptr
|
|
self.c = c
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
public func derive(_ x: borrowing BufferView) -> BufferView {
|
|
return BufferView(x.ptr, x.c)
|
|
}
|
|
|
|
public func borrowAndCreate(_ view: borrowing BufferView) -> BufferView {
|
|
return BufferView(view.ptr, view.c )
|
|
}
|
|
|
|
public func consumeAndCreate(_ view: consuming BufferView) -> BufferView {
|
|
return BufferView(view.ptr, view.c)
|
|
}
|
|
|
|
public struct Container : ~Copyable {
|
|
let ptr: UnsafeRawBufferPointer
|
|
let c: Int
|
|
public init(_ ptr: UnsafeRawBufferPointer, _ c: Int) {
|
|
self.ptr = ptr
|
|
self.c = c
|
|
}
|
|
|
|
public var view: BufferView {
|
|
get {
|
|
return BufferView(ptr, c)
|
|
}
|
|
}
|
|
}
|
|
|
|
public struct Wrapper : ~Escapable {
|
|
var _view: BufferView
|
|
public var view: BufferView {
|
|
_read {
|
|
yield _view
|
|
}
|
|
_modify {
|
|
yield &_view
|
|
}
|
|
}
|
|
public init(_ view: consuming BufferView) {
|
|
self._view = view
|
|
}
|
|
}
|
|
|