This commit is contained in:
Joe Ardent 2023-07-26 09:22:23 -07:00
parent 8a79e737fc
commit 98a8a26fe9
1 changed files with 14 additions and 5 deletions

View File

@ -17,6 +17,7 @@ pub const TIME_BITS: u8 = 48;
pub const COUNTER_BITS: u8 = 16;
/// The number of random bits + bits in the monotonic counter
pub const UNIQUE_BITS: u8 = 80;
/// The number of fully random bits
pub const RANDOM_BITS: u8 = 64;
macro_rules! bitmask {
@ -114,28 +115,31 @@ impl Julid {
Julid(u128::MAX)
}
/// Gets the timestamp section of this ulid
/// Gets the timestamp section of this julid
pub const fn timestamp(&self) -> u64 {
(self.0 >> UNIQUE_BITS) as u64
}
/// Gets the value of this julid's monotonic counter
pub const fn counter(&self) -> u16 {
let mask = bitmask!(COUNTER_BITS);
((self.0 >> RANDOM_BITS) & mask) as u16
}
/// Gets the 64-bit concatenation of the timestamp and counter
pub const fn sortable(&self) -> u64 {
let mask = bitmask!(TIME_BITS + COUNTER_BITS);
((self.0 >> RANDOM_BITS) & mask) as u64
}
pub const fn random(&self) -> u128 {
self.0 & bitmask!(RANDOM_BITS)
/// Gets the 64-bit random value
pub const fn random(&self) -> u64 {
(self.0 & bitmask!(RANDOM_BITS)) as u64
}
/// Gets the non-timestamp section of this Julid (random + counter bits).
pub const fn unique(&self) -> u128 {
self.0 & bitmask!(UNIQUE_BITS)
pub const fn unique(&self) -> u64 {
(self.0 & bitmask!(UNIQUE_BITS)) as u64
}
/// Creates a Crockford Base32 encoded string that represents this Julid
@ -157,6 +161,11 @@ impl Julid {
self.0 == 0u128
}
/// Test if the Julid is Omega
pub const fn is_omega(&self) -> bool {
self.0 == u128::MAX
}
/// Creates a Julid using the provided bytes array, assumed big-endian.
pub const fn from_bytes(bytes: [u8; 16]) -> Julid {
Self(u128::from_be_bytes(bytes))