mirror of
https://github.com/apple/swift-collections
synced 2026-11-23 04:24:23 +00:00
- UniqueBox is public API, so we cannot remove it in favor of the stdlib’s - Remove Ref and MutableRef. Keep an internal _MutableRef to work around stdlib limitations.
60 lines
1.8 KiB
Swift
60 lines
1.8 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2025 - 2026 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
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if compiler(>=6.4) && UnstableContainersPreview
|
|
import Builtin
|
|
|
|
extension Optional where Wrapped: ~Copyable /* FIXME: ~Escapable */ {
|
|
@available(SwiftStdlib 6.4, *)
|
|
@_lifetime(borrow self)
|
|
@_addressableSelf
|
|
public func borrow() -> Ref<Wrapped>? {
|
|
if self == nil {
|
|
return nil
|
|
}
|
|
|
|
// FIXME: This assumes that `Optional<T>.some` is guaranteed to have
|
|
// `T` at the beginning of its layout. Is that true?
|
|
let pointer = unsafe UnsafePointer<Wrapped>(
|
|
Builtin.unprotectedAddressOfBorrow(self)
|
|
)
|
|
|
|
return unsafe Ref(unsafeAddress: pointer, borrowing: self)
|
|
}
|
|
}
|
|
|
|
extension Optional where Wrapped: ~Copyable /* FIXME: ~Escapable */ {
|
|
@available(SwiftStdlib 6.4, *)
|
|
@_lifetime(&self)
|
|
@_alwaysEmitIntoClient
|
|
package mutating func mutate() -> MutableRef<Wrapped>? {
|
|
if self == nil {
|
|
return nil
|
|
}
|
|
let pointer = unsafe UnsafeMutablePointer<Wrapped>(
|
|
Builtin.unprotectedAddressOf(&self))
|
|
return unsafe MutableRef(unsafeAddress: pointer, mutating: &self)
|
|
}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
@_lifetime(&self)
|
|
@_alwaysEmitIntoClient
|
|
@_transparent
|
|
package mutating func insert(_ value: consuming Wrapped) -> MutableRef<Wrapped> {
|
|
self = .some(value)
|
|
return mutate()._consumingUnsafelyUnwrap()
|
|
}
|
|
}
|
|
|
|
#endif
|