struct KSUID
- KSUID
- Struct
- Value
- Object
Included Modules
- Comparable(KSUID)
Defined in:
ksuid.crksuid/base62.cr
ksuid/error.cr
ksuid/utils.cr
Constant Summary
-
EPOCH =
14e8.to_u32
-
KSUID's epoch starts more recently so that the 32-bit number space gives a significantly higher useful lifetime of around 136 years from May 2014. This number (
14e8
) was picked to be easy to remember. -
MAX =
from(StaticArray(UInt8, TOTAL_SIZE).new(255_u8))
-
KSUID
with maximum valid value (aWgEPTl1tmebfsQzFP4bxwgy80V
). -
MIN =
from(StaticArray(UInt8, TOTAL_SIZE).new(0_u8))
-
KSUID
with minimum valid value (000000000000000000000000000
). -
PAYLOAD_SIZE =
16
-
Payload is 16 bytes.
-
STRING_ENCODED_SIZE =
27
-
The length of a
KSUID
when string (base62) encoded. -
TIMESTAMP_SIZE =
4
-
Timestamp is an
UInt32
. -
TOTAL_SIZE =
TIMESTAMP_SIZE + PAYLOAD_SIZE
-
KSUID
s are 20 bytes:- 00-03 byte:
UInt32
BE UTC#timestamp
with custom epoch - 04-19 byte: random
#payload
- 00-03 byte:
-
VERSION =
"0.2.0"
Constructors
-
.from(string : String) : KSUID
Converts a base62-encoded
String
into aKSUID
. -
.from(bytes : Bytes) : KSUID
Converts
Bytes
into aKSUID
. -
.from(array : StaticArray(UInt8, TOTAL_SIZE)) : KSUID
Converts
StaticArray
into aKSUID
. -
.new(time : Time, payload : Bytes)
Generates a new
KSUID
with given time and payload. -
.new(timestamp : Int, payload : Bytes)
Generates a new
KSUID
with given timestamp and payload. - .new(time : Time = Time.now)
Instance Method Summary
-
#<=>(other : KSUID)
Compares the
KSUID
against other. - #==(other : self)
-
#hash(hasher)
See
Object#hash(hasher)
-
#inspect(io : IO) : Nil
Appends this struct's name and instance variables names and values to the given IO.
-
#payload : StaticArray(UInt8, PAYLOAD_SIZE)
The 16-byte random payload without the timestamp.
-
#raw : String
Returns the
KSUID
as a hex-encodedString
. - #timestamp : UInt32
-
#to_s(io : IO) : Nil
Writes the
KSUID
as a base62-encodedString
to io. -
#to_slice : Bytes
Returns the
KSUID
asBytes
. -
#to_time : Time
Returns the timestamp portion of the
KSUID
as aTime
object.
Constructor Detail
Converts a base62-encoded String
into a KSUID
.
Converts StaticArray
into a KSUID
.
Generates a new KSUID
with given timestamp and payload.
Instance Method Detail
Appends this struct's name and instance variables names and values to the given IO.
struct Point
def initialize(@x : Int32, @y : Int32)
end
end
p1 = Point.new 1, 2
p1.to_s # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"