|
| 1 | +@_addressableForDependencies |
| 2 | +public struct Something { |
| 3 | + public struct View<T: BitwiseCopyable> : ~Copyable, ~Escapable { |
| 4 | + var ptr: UnsafeBufferPointer<T> |
| 5 | + |
| 6 | + @lifetime(borrow ptr) |
| 7 | + public init(ptr: borrowing UnsafeBufferPointer<T>) { |
| 8 | + self.ptr = copy ptr |
| 9 | + } |
| 10 | + |
| 11 | + public var span: Span<T> { |
| 12 | + @lifetime(borrow self) |
| 13 | + borrowing get { |
| 14 | + Span(_unsafeElements: ptr) |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + public struct MutableView<T: BitwiseCopyable> : ~Copyable, ~Escapable { |
| 20 | + var ptr: UnsafeMutableBufferPointer<T> |
| 21 | + |
| 22 | + @lifetime(borrow ptr) |
| 23 | + public init(ptr: borrowing UnsafeMutableBufferPointer<T>) { |
| 24 | + self.ptr = copy ptr |
| 25 | + } |
| 26 | + |
| 27 | + public var mutableSpan: MutableSpan<T> { |
| 28 | + @lifetime(&self) |
| 29 | + mutating get { |
| 30 | + MutableSpan(_unsafeElements: ptr) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + var ptr: UnsafeMutableRawBufferPointer |
| 36 | + public init(ptr: UnsafeMutableRawBufferPointer) { |
| 37 | + self.ptr = ptr |
| 38 | + } |
| 39 | + |
| 40 | + @lifetime(borrow self) |
| 41 | + public func view<T>(of type: T.Type = T.self) -> View<T> { |
| 42 | + let tp = ptr.assumingMemoryBound(to: T.self) |
| 43 | + return __overrideLifetime(View(ptr: .init(tp)), borrowing: self) |
| 44 | + } |
| 45 | + |
| 46 | + @lifetime(&self) |
| 47 | + public mutating func mutableView<T>(of type: T.Type = T.self) -> MutableView<T> { |
| 48 | + let tp = ptr.assumingMemoryBound(to: T.self) |
| 49 | + return __overrideLifetime(MutableView(ptr: tp), mutating: &self) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +@unsafe |
| 54 | +@_unsafeNonescapableResult |
| 55 | +@_alwaysEmitIntoClient |
| 56 | +@_transparent |
| 57 | +@lifetime(borrow source) |
| 58 | +public func __overrideLifetime< |
| 59 | + T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable |
| 60 | +>( |
| 61 | + _ dependent: consuming T, borrowing source: borrowing U |
| 62 | +) -> T { |
| 63 | + dependent |
| 64 | +} |
| 65 | + |
| 66 | +/// Unsafely discard any lifetime dependency on the `dependent` argument. Return |
| 67 | +/// a value identical to `dependent` that inherits all lifetime dependencies from |
| 68 | +/// the `source` argument. |
| 69 | +@unsafe |
| 70 | +@_unsafeNonescapableResult |
| 71 | +@_alwaysEmitIntoClient |
| 72 | +@_transparent |
| 73 | +@lifetime(copy source) |
| 74 | +public func __overrideLifetime< |
| 75 | + T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable |
| 76 | +>( |
| 77 | + _ dependent: consuming T, copying source: borrowing U |
| 78 | +) -> T { |
| 79 | + dependent |
| 80 | +} |
| 81 | + |
| 82 | +/// Unsafely discard any lifetime dependency on the `dependent` argument. |
| 83 | +/// Return a value identical to `dependent` with a lifetime dependency |
| 84 | +/// on the caller's exclusive borrow scope of the `source` argument. |
| 85 | +@unsafe |
| 86 | +@_unsafeNonescapableResult |
| 87 | +@_alwaysEmitIntoClient |
| 88 | +@_transparent |
| 89 | +@lifetime(&source) |
| 90 | +public func __overrideLifetime< |
| 91 | + T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable |
| 92 | +>( |
| 93 | + _ dependent: consuming T, |
| 94 | + mutating source: inout U |
| 95 | +) -> T { |
| 96 | + dependent |
| 97 | +} |
| 98 | + |
0 commit comments