Skip to content

Commit

Permalink
Add Comparison operator to UUID (#11352)
Browse files Browse the repository at this point in the history
Co-authored-by: Oleh Prypin <[email protected]>
  • Loading branch information
darkstego and oprypin authored Oct 29, 2021
1 parent c573015 commit 20b958b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/std/uuid_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ describe "UUID" do
end
end

describe "#<=>" do
it "correctly compares two UUIDs" do
uuid_1 = UUID.new("00330000-0000-0000-0000-000000000000")
uuid_2 = UUID.new("00000011-0000-0000-5500-000099000000")
(uuid_1 <=> uuid_2).should be > 0
(uuid_2 <=> uuid_1).should be < 0
(uuid_1 <=> uuid_1).should eq 0
end
end

describe "random initialize" do
it "works with no options" do
subject = UUID.random
Expand Down
6 changes: 6 additions & 0 deletions src/uuid.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Represents a UUID (Universally Unique IDentifier).
struct UUID
include Comparable(UUID)

# Variants with 16 bytes.
enum Variant
# Unknown (i.e. custom, your own).
Expand Down Expand Up @@ -207,6 +209,10 @@ struct UUID
end
end

def <=>(other : UUID) : Int32
@bytes <=> other.bytes
end

class Error < Exception
end

Expand Down

0 comments on commit 20b958b

Please sign in to comment.