@@ -17,35 +17,89 @@ extension UUID {
1717 }
1818}
1919
20- extension UUID {
21- internal struct UUIDv1 {
22- public init ( ) {
23- self . init ( nodeID: . default)
24- }
25-
26- public init ( nodeID: NodeID ) {
27- let ( timestamp, clockSequence) = Clock . default. next ( )
28- self . init ( timestamp: timestamp, clockSequence: clockSequence, nodeID: nodeID)
20+ public struct UUIDv1 : Codable , Hashable , LosslessStringConvertible , RawRepresentable {
21+ public init ( ) {
22+ self . init ( node: . current)
23+ }
24+
25+ public init ( node: Node ) {
26+ let ( timestamp, clockSequence) = Clock . default. next ( )
27+ self . init ( timestamp: timestamp, clockSequence: clockSequence, node: node)
28+ }
29+
30+ public init ( timestamp: Timestamp , clockSequence: ClockSequence , node: Node ) {
31+ var bytes = UUID . null. uuid
32+ bytes. 0 = UInt8 ( truncatingIfNeeded: timestamp. rawValue >> 24 )
33+ bytes. 1 = UInt8 ( truncatingIfNeeded: timestamp. rawValue >> 16 )
34+ bytes. 2 = UInt8 ( truncatingIfNeeded: timestamp. rawValue >> 8 )
35+ bytes. 3 = UInt8 ( truncatingIfNeeded: timestamp. rawValue)
36+ bytes. 4 = UInt8 ( truncatingIfNeeded: timestamp. rawValue >> 40 )
37+ bytes. 5 = UInt8 ( truncatingIfNeeded: timestamp. rawValue >> 32 )
38+ bytes. 6 = UInt8 ( truncatingIfNeeded: timestamp. rawValue >> 56 ) & 0x0f | 0x10
39+ bytes. 7 = UInt8 ( truncatingIfNeeded: timestamp. rawValue >> 48 )
40+ bytes. 8 = UInt8 ( truncatingIfNeeded: clockSequence. rawValue >> 8 ) & 0x3f | 0x80
41+ bytes. 9 = UInt8 ( truncatingIfNeeded: clockSequence. rawValue)
42+ bytes. 10 = UInt8 ( truncatingIfNeeded: node. rawValue >> 40 )
43+ bytes. 11 = UInt8 ( truncatingIfNeeded: node. rawValue >> 32 )
44+ bytes. 12 = UInt8 ( truncatingIfNeeded: node. rawValue >> 24 )
45+ bytes. 13 = UInt8 ( truncatingIfNeeded: node. rawValue >> 16 )
46+ bytes. 14 = UInt8 ( truncatingIfNeeded: node. rawValue >> 8 )
47+ bytes. 15 = UInt8 ( truncatingIfNeeded: node. rawValue)
48+ rawValue = UUID ( uuid: bytes)
49+ assert ( rawValue. version == 1 )
50+ }
51+
52+ public init ? ( rawValue: UUID ) {
53+ guard rawValue. version == 1 else {
54+ return nil
2955 }
30-
31- public init ( timestamp: Timestamp , clockSequence: ClockSequence , nodeID: NodeID ) {
32- let time = timestamp. rawValue
33- var bytes = UUID . null. uuid
34- bytes. 0 = UInt8 ( truncatingIfNeeded: time >> 24 )
35- bytes. 1 = UInt8 ( truncatingIfNeeded: time >> 16 )
36- bytes. 2 = UInt8 ( truncatingIfNeeded: time >> 8 )
37- bytes. 3 = UInt8 ( truncatingIfNeeded: time)
38- bytes. 4 = UInt8 ( truncatingIfNeeded: time >> 40 )
39- bytes. 5 = UInt8 ( truncatingIfNeeded: time >> 32 )
40- bytes. 6 = UInt8 ( truncatingIfNeeded: time >> 56 ) & 0x0f | 0x10
41- bytes. 7 = UInt8 ( truncatingIfNeeded: time >> 48 )
42- bytes. 8 = UInt8 ( truncatingIfNeeded: clockSequence >> 8 ) & 0x3f | 0x80
43- bytes. 9 = UInt8 ( truncatingIfNeeded: clockSequence)
44- ( bytes. 10 , bytes. 11 , bytes. 12 , bytes. 13 , bytes. 14 , bytes. 15 ) = nodeID. bytes
45- rawValue = UUID ( uuid: bytes)
46- assert ( rawValue. version == 1 )
56+ self . rawValue = rawValue
57+ }
58+
59+ public init ? ( _ description: String ) {
60+ guard let rawValue = UUID ( uuidString: description) , rawValue. version == 1 else {
61+ return nil
4762 }
48-
49- public let rawValue : UUID
63+ self . rawValue = rawValue
64+ }
65+
66+ public let rawValue : UUID
67+
68+ public var timestamp : Timestamp {
69+ let bytes = rawValue. uuid
70+ var rawValue : UInt64 = 0
71+ rawValue |= UInt64 ( bytes. 0 ) << 24
72+ rawValue |= UInt64 ( bytes. 1 ) << 16
73+ rawValue |= UInt64 ( bytes. 2 ) << 8
74+ rawValue |= UInt64 ( bytes. 3 )
75+ rawValue |= UInt64 ( bytes. 4 ) << 40
76+ rawValue |= UInt64 ( bytes. 5 ) << 32
77+ rawValue |= UInt64 ( bytes. 6 & 0x0f ) << 56
78+ rawValue |= UInt64 ( bytes. 7 ) << 48
79+ return Timestamp ( truncatingIfNeeded: rawValue)
80+ }
81+
82+ public var clockSequence : ClockSequence {
83+ let bytes = rawValue. uuid
84+ var rawValue : UInt16 = 0
85+ rawValue |= UInt16 ( bytes. 8 ) << 8
86+ rawValue |= UInt16 ( bytes. 9 )
87+ return ClockSequence ( truncatingIfNeeded: rawValue)
88+ }
89+
90+ public var node : Node {
91+ let bytes = rawValue. uuid
92+ var rawValue : UInt64 = 0
93+ rawValue |= UInt64 ( bytes. 10 ) << 40
94+ rawValue |= UInt64 ( bytes. 11 ) << 32
95+ rawValue |= UInt64 ( bytes. 12 ) << 24
96+ rawValue |= UInt64 ( bytes. 13 ) << 16
97+ rawValue |= UInt64 ( bytes. 14 ) << 8
98+ rawValue |= UInt64 ( bytes. 15 )
99+ return Node ( truncatingIfNeeded: rawValue)
100+ }
101+
102+ public var description : String {
103+ rawValue. description
50104 }
51105}
0 commit comments