//===----------------------------------------------------------------------===// // // This source file is part of the Swift Collections open source project // // Copyright (c) 2024 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // //===----------------------------------------------------------------------===// @frozen public struct Box: ~Copyable { @usableFromInline internal let _pointer: UnsafeMutablePointer @_alwaysEmitIntoClient @_transparent public init(_ value: consuming T) { _pointer = UnsafeMutablePointer.allocate(capacity: 1) _pointer.initialize(to: value) } @_alwaysEmitIntoClient @_transparent public init(_ fromInout: consuming Inout) { _pointer = fromInout._pointer } @_alwaysEmitIntoClient @inlinable deinit { _pointer.deinitialize(count: 1) _pointer.deallocate() } } extension Box where T: ~Copyable { @_alwaysEmitIntoClient @_transparent public consuming func consume() -> T { let result = _pointer.move() _pointer.deallocate() discard self return result } @_alwaysEmitIntoClient @_transparent public consuming func leak() -> dependsOn(immortal) Inout { let result = Inout(unsafeImmortalAddress: _pointer) discard self return result } @_alwaysEmitIntoClient public subscript() -> T { @_transparent unsafeAddress { UnsafePointer(_pointer) } @_transparent nonmutating unsafeMutableAddress { _pointer } } } extension Box where T: Copyable { @_alwaysEmitIntoClient @_transparent public borrowing func copy() -> T { _pointer.pointee } }