-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
ObjectIdentifier.swift
144 lines (128 loc) · 4.4 KB
/
ObjectIdentifier.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if !$Embedded
/// A unique identifier for a class instance or metatype.
///
/// This unique identifier is only valid for comparisons during the lifetime
/// of the instance.
///
/// In Swift, only class instances and metatypes have unique identities. There
/// is no notion of identity for structs, enums, functions, or tuples.
@frozen // trivial-implementation
public struct ObjectIdentifier: Sendable {
@usableFromInline // trivial-implementation
internal let _value: Builtin.RawPointer
/// Creates an instance that uniquely identifies the given class instance.
///
/// The following example creates an example class `IntegerRef` and compares
/// instances of the class using their object identifiers and the identical-to
/// operator (`===`):
///
/// class IntegerRef {
/// let value: Int
/// init(_ value: Int) {
/// self.value = value
/// }
/// }
///
/// let x = IntegerRef(10)
/// let y = x
///
/// print(ObjectIdentifier(x) == ObjectIdentifier(y))
/// // Prints "true"
/// print(x === y)
/// // Prints "true"
///
/// let z = IntegerRef(10)
/// print(ObjectIdentifier(x) == ObjectIdentifier(z))
/// // Prints "false"
/// print(x === z)
/// // Prints "false"
///
/// - Parameter x: An instance of a class.
@inlinable // trivial-implementation
public init(_ x: AnyObject) {
self._value = Builtin.bridgeToRawPointer(x)
}
/// Creates an instance that uniquely identifies the given metatype.
///
/// - Parameter: A metatype.
@inlinable // trivial-implementation
public init(_ x: Any.Type) {
self._value = unsafeBitCast(x, to: Builtin.RawPointer.self)
}
}
#else
@frozen // trivial-implementation
public struct ObjectIdentifier: Sendable {
@usableFromInline // trivial-implementation
internal let _value: Builtin.RawPointer
@inlinable // trivial-implementation
public init<Object: AnyObject>(_ x: Object) {
self._value = Builtin.bridgeToRawPointer(x)
}
@inlinable // trivial-implementation
public init<Object>(_ x: Object.Type) {
self._value = unsafeBitCast(x, to: Builtin.RawPointer.self)
}
}
#endif
@_unavailableInEmbedded
extension ObjectIdentifier: CustomDebugStringConvertible {
/// A textual representation of the identifier, suitable for debugging.
public var debugDescription: String {
return "ObjectIdentifier(\(_rawPointerToString(_value)))"
}
}
extension ObjectIdentifier: Equatable {
@inlinable // trivial-implementation
public static func == (x: ObjectIdentifier, y: ObjectIdentifier) -> Bool {
return Bool(Builtin.cmp_eq_RawPointer(x._value, y._value))
}
}
extension ObjectIdentifier: Comparable {
@inlinable // trivial-implementation
public static func < (lhs: ObjectIdentifier, rhs: ObjectIdentifier) -> Bool {
return UInt(bitPattern: lhs) < UInt(bitPattern: rhs)
}
}
extension ObjectIdentifier: Hashable {
/// Hashes the essential components of this value by feeding them into the
/// given hasher.
///
/// - Parameter hasher: The hasher to use when combining the components
/// of this instance.
@inlinable
public func hash(into hasher: inout Hasher) {
hasher.combine(Int(Builtin.ptrtoint_Word(_value)))
}
@_alwaysEmitIntoClient // For back deployment
public func _rawHashValue(seed: Int) -> Int {
Int(Builtin.ptrtoint_Word(_value))._rawHashValue(seed: seed)
}
}
extension UInt {
/// Creates an integer that captures the full value of the given object
/// identifier.
@inlinable // trivial-implementation
public init(bitPattern objectID: ObjectIdentifier) {
self.init(Builtin.ptrtoint_Word(objectID._value))
}
}
extension Int {
/// Creates an integer that captures the full value of the given object
/// identifier.
@inlinable // trivial-implementation
public init(bitPattern objectID: ObjectIdentifier) {
self.init(bitPattern: UInt(bitPattern: objectID))
}
}