pub struct Ulid(/* private fields */);Expand description
A ULID: 128 bits made of a 48-bit millisecond timestamp and 80 random bits, written as 26
characters of Crockford’s base32 (01ARYZ6S41TSV4RRFFQ69G5FAV).
Ulids sort by creation time, both as numbers and as text, and are shorter than a UUID. Time
and randomness are parameters of from_parts; with the
random-entropy feature, new takes them from the system. Ids made in the
same millisecond are not ordered among themselves (there is no monotonic counter).
§Examples
use helpers4::id::Ulid;
let id = Ulid::from_parts(1_469_918_176_385, [0; 10]);
assert_eq!(id.to_string(), "01ARYZ6S410000000000000000");
assert_eq!(id.timestamp_millis(), 1_469_918_176_385);
assert_eq!(Ulid::parse("01ARYZ6S41TSV4RRFFQ69G5FAV").map(|u| u.timestamp_millis()), Ok(1_469_918_176_385));Implementations§
Source§impl Ulid
impl Ulid
Sourcepub const fn from_parts(unix_millis: u64, random: [u8; 10]) -> Self
pub const fn from_parts(unix_millis: u64, random: [u8; 10]) -> Self
Builds a ULID from a timestamp and 80 random bits. Only the low 48 bits of
unix_millis are kept, enough until the year 10889.
§Arguments
unix_millis- Milliseconds since 1970-01-01 UTC.random- 10 random bytes, from a secure source for an id that must be unpredictable.
§Returns
The ULID.
§Examples
use helpers4::id::Ulid;
assert_eq!(Ulid::from_parts(0, [0xff; 10]).to_string(), "0000000000ZZZZZZZZZZZZZZZZ");Sourcepub const fn timestamp_millis(&self) -> u64
pub const fn timestamp_millis(&self) -> u64
Sourcepub fn parse(text: &str) -> Result<Self, ParseUlidError>
pub fn parse(text: &str) -> Result<Self, ParseUlidError>
Reads a ULID from its 26-character form, in upper or lower case. Only the 32 symbols of
Crockford’s base32 are accepted (I, L, O and U are not).
§Arguments
text- The text to read.
§Returns
The ULID.
§Errors
Returns a ParseUlidError for a length other than 26, a character outside the
alphabet, or a first character above 7 (the value would not fit in 128 bits).
§Examples
use helpers4::id::Ulid;
let id = Ulid::parse("01aryz6s41tsv4rrffq69g5fav")?;
assert_eq!(id.to_string(), "01ARYZ6S41TSV4RRFFQ69G5FAV");