Skip to content

Commit 3d7ae5f

Browse files
committed
Add a getter to access the raw bytes of the CSUUID.
1 parent d1931d6 commit 3d7ae5f

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

benchmark.cr

+16-16
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ require "benchmark"
99
# standard library UUID implementation.
1010

1111
Benchmark.ips do |bench|
12-
bench.report("CSUUID.new -- generate random, chronologically sortable UUID") do
13-
CSUUID.new
14-
end
15-
bench.report("UUID.random -- generate random UUID") do
16-
UUID.random
17-
end
12+
bench.report("CSUUID.new -- generate random, chronologically sortable UUID") do
13+
CSUUID.new
14+
end
15+
bench.report("UUID.random -- generate random UUID") do
16+
UUID.random
17+
end
1818
end
1919

2020
Benchmark.ips do |bench|
21-
bench.report("CSUUID.unique -- generate 100 guaranteed unique, sortable IDs") do
22-
100.times do
23-
CSUUID.unique
24-
end
25-
end
26-
bench.report("UUID.new -- generate 100 random UUIDs") do
27-
100.times do
28-
UUID.random
29-
end
30-
end
21+
bench.report("CSUUID.unique -- generate 100 guaranteed unique, sortable IDs") do
22+
100.times do
23+
CSUUID.unique
24+
end
25+
end
26+
bench.report("UUID.new -- generate 100 random UUIDs") do
27+
100.times do
28+
UUID.random
29+
end
30+
end
3131
end

shard.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: csuuid
2-
version: 0.3.0
2+
version: 0.3.1
33

44
authors:
55
- Kirk Haines <[email protected]>

src/csuuid.cr

+11-10
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ require "crystal/spin_lock"
4040
# ```
4141
#
4242
struct CSUUID
43-
VERSION = "0.3.0"
43+
VERSION = "0.3.1"
4444

4545
@@mutex = Crystal::SpinLock.new
4646
@@prng = Random::ISAAC.new
4747
@@unique_identifier : Slice(UInt8) = Slice(UInt8).new(6, 0)
4848
@@unique_seconds_and_nanoseconds : Tuple(Int64, Int32) = {0_i64, 0_i32}
4949

50-
@bytes : Slice(UInt8) = Slice(UInt8).new(16)
50+
# A getter to allow access to the raw byte buffer for the CSUUID.
51+
getter bytes : Slice(UInt8) = Slice(UInt8).new(16)
5152
@seconds_and_nanoseconds : Tuple(Int64, Int32)?
5253
@timestamp : Time?
5354
@utc : Time?
@@ -74,7 +75,7 @@ struct CSUUID
7475

7576
def self.generate(count)
7677
result = [] of CSUUID
77-
count.times {result << unique}
78+
count.times { result << unique }
7879

7980
result
8081
end
@@ -113,13 +114,13 @@ struct CSUUID
113114

114115
private def initialize_impl(seconds : Int64, nanoseconds : Int32, identifier : Slice(UInt8) | String | Nil)
115116
id = if identifier.is_a?(String)
116-
buf = Slice(UInt8).new(6)
117-
number_of_bytes = identifier.size < 6 ? identifier.size : 6
118-
buf[0, number_of_bytes].copy_from(identifier.hexbytes[0, number_of_bytes])
119-
buf
120-
else
121-
identifier
122-
end
117+
buf = Slice(UInt8).new(6)
118+
number_of_bytes = identifier.size < 6 ? identifier.size : 6
119+
buf[0, number_of_bytes].copy_from(identifier.hexbytes[0, number_of_bytes])
120+
buf
121+
else
122+
identifier
123+
end
123124

124125
IO::ByteFormat::BigEndian.encode(seconds, @bytes[2, 8])
125126
IO::ByteFormat::BigEndian.encode(nanoseconds, @bytes[0, 4])

0 commit comments

Comments
 (0)