Files
2025-04-17 03:02:33 -07:00

55 lines
1.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2025 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
//
//===----------------------------------------------------------------------===//
import Builtin
@frozen
@safe
public struct Borrow<T: ~Copyable>: Copyable, ~Escapable {
@usableFromInline
package let _pointer: UnsafePointer<T>
@lifetime(borrow value)
@_alwaysEmitIntoClient
@_transparent
public init(_ value: borrowing @_addressable T) {
unsafe _pointer = UnsafePointer(Builtin.unprotectedAddressOfBorrow(value))
}
@lifetime(borrow owner)
@_alwaysEmitIntoClient
@_transparent
public init<Owner: ~Copyable & ~Escapable>(
unsafeAddress: UnsafePointer<T>,
borrowing owner: borrowing Owner
) {
unsafe _pointer = unsafeAddress
}
@lifetime(copy owner)
@_alwaysEmitIntoClient
@_transparent
public init<Owner: ~Copyable & ~Escapable>(
unsafeAddress: UnsafePointer<T>,
copying owner: borrowing Owner
) {
unsafe _pointer = unsafeAddress
}
@_alwaysEmitIntoClient
public subscript() -> T {
@_transparent
unsafeAddress {
unsafe _pointer
}
}
}