@@ -73,15 +73,23 @@ extension UniqueID {
7373 ///
7474 @inlinable
7575 public static func random< RNG> ( using rng: inout RNG ) -> UniqueID where RNG: RandomNumberGenerator {
76- var randomBytes : ( UInt64 , UInt64 ) = ( rng. next ( ) , rng. next ( ) )
77- return withUnsafeMutableBytes ( of: & randomBytes) { bytes in
78- // octet 6 = time_hi_and_version (high octet).
79- // high 4 bits = version number.
80- bytes [ 6 ] = ( bytes [ 6 ] & 0xF ) | 0x40
81- // octet 8 = clock_seq_high_and_reserved.
82- // high 2 bits = variant (10 = standard).
83- bytes [ 8 ] = ( bytes [ 8 ] & 0x3F ) | 0x80
84- return UniqueID ( bytes: bytes. load ( as: UniqueID . Bytes. self) )
76+ var bytes = UniqueID . null. bytes
77+ withUnsafeMutableBytes ( of: & bytes) { dest in
78+ var random = rng. next ( )
79+ Swift . withUnsafePointer ( to: & random) {
80+ dest. baseAddress!. copyMemory ( from: UnsafeRawPointer ( $0) , byteCount: 8 )
81+ }
82+ random = rng. next ( )
83+ Swift . withUnsafePointer ( to: & random) {
84+ dest. baseAddress!. advanced ( by: 8 ) . copyMemory ( from: UnsafeRawPointer ( $0) , byteCount: 8 )
85+ }
8586 }
87+ // octet 6 = time_hi_and_version (high octet).
88+ // high 4 bits = version number.
89+ bytes. 6 = ( bytes. 6 & 0xF ) | 0x40
90+ // octet 8 = clock_seq_high_and_reserved.
91+ // high 2 bits = variant (10 = standard).
92+ bytes. 8 = ( bytes. 8 & 0x3F ) | 0x80
93+ return UniqueID ( bytes: bytes)
8694 }
8795}
0 commit comments