pub struct Ulid(pub u128);
Expand description
A Ulid is a unique 128-bit lexicographically sortable identifier
Canonically, it is represented as a 26 character Crockford Base32 encoded string.
Of the 128-bits, the first 48 are a unix timestamp in milliseconds. The remaining 80 are random. The first 48 provide for lexicographic sorting and the remaining 80 ensure that the identifier is unique.
Tuple Fields
0: u128
Implementations
sourceimpl Ulid
impl Ulid
sourcepub fn with_source<R: Rng>(source: &mut R) -> Ulid
pub fn with_source<R: Rng>(source: &mut R) -> Ulid
Creates a new Ulid using data from the given random number generator
Example
use rand::prelude::*;
use ulid::Ulid;
let mut rng = StdRng::from_entropy();
let ulid = Ulid::with_source(&mut rng);
sourcepub fn from_datetime(datetime: OffsetDateTime) -> Ulid
pub fn from_datetime(datetime: OffsetDateTime) -> Ulid
Creates a new Ulid with the given datetime
This can be useful when migrating data to use Ulid identifiers
Example
use time::OffsetDateTime;
use ulid::Ulid;
let ulid = Ulid::from_datetime(OffsetDateTime::now_utc());
sourcepub fn from_datetime_with_source<R>(
datetime: OffsetDateTime,
source: &mut R
) -> Ulid where
R: Rng,
pub fn from_datetime_with_source<R>(
datetime: OffsetDateTime,
source: &mut R
) -> Ulid where
R: Rng,
Creates a new Ulid with the given datetime and random number generator
Example
use time::OffsetDateTime;
use rand::prelude::*;
use ulid::Ulid;
let mut rng = StdRng::from_entropy();
let ulid = Ulid::from_datetime_with_source(OffsetDateTime::now_utc(), &mut rng);
sourcepub fn datetime(&self) -> OffsetDateTime
pub fn datetime(&self) -> OffsetDateTime
Gets the datetime of when this Ulid was created accurate to 1ms
Example
use time::Duration;
use time::OffsetDateTime;
use ulid::Ulid;
let dt = OffsetDateTime::now_utc();
let ulid = Ulid::from_datetime(dt);
assert!((dt - ulid.datetime()) < Duration::milliseconds(1));
sourceimpl Ulid
impl Ulid
sourcepub const fn from_parts(timestamp_ms: u64, random: u128) -> Ulid
pub const fn from_parts(timestamp_ms: u64, random: u128) -> Ulid
Create a Ulid from separated parts.
NOTE: Any overflow bits in the given args are discarded
Example
use ulid::Ulid;
let ulid = Ulid::from_string("01D39ZY06FGSCTVN4T2V9PKHFZ").unwrap();
let ulid2 = Ulid::from_parts(ulid.timestamp_ms(), ulid.random());
assert_eq!(ulid, ulid2);
sourcepub const fn from_string(encoded: &str) -> Result<Ulid, DecodeError>
pub const fn from_string(encoded: &str) -> Result<Ulid, DecodeError>
Creates a Ulid from a Crockford Base32 encoded string
An DecodeError will be returned when the given string is not formated properly.
Example
use ulid::Ulid;
let text = "01D39ZY06FGSCTVN4T2V9PKHFZ";
let result = Ulid::from_string(text);
assert!(result.is_ok());
assert_eq!(&result.unwrap().to_string(), text);
sourcepub const fn nil() -> Ulid
pub const fn nil() -> Ulid
The ‘nil Ulid’.
The nil Ulid is special form of Ulid that is specified to have all 128 bits set to zero.
Example
use ulid::Ulid;
let ulid = Ulid::nil();
assert_eq!(
ulid.to_string(),
"00000000000000000000000000"
);
sourcepub const fn timestamp_ms(&self) -> u64
pub const fn timestamp_ms(&self) -> u64
Gets the timestamp section of this ulid
Example
use ::time::OffsetDateTime;
use ulid::Ulid;
let dt = OffsetDateTime::now_utc();
let ulid = Ulid::from_datetime(dt);
assert_eq!(ulid.timestamp_ms(), (dt.unix_timestamp_nanos() / 1_000_000) as u64);
sourcepub const fn random(&self) -> u128
pub const fn random(&self) -> u128
Gets the random section of this ulid
Example
use ulid::Ulid;
let text = "01D39ZY06FGSCTVN4T2V9PKHFZ";
let ulid = Ulid::from_string(text).unwrap();
let ulid_next = ulid.increment().unwrap();
assert_eq!(ulid.random() + 1, ulid_next.random());
sourcepub fn to_str<'buf>(
&self,
buf: &'buf mut [u8]
) -> Result<&'buf mut str, EncodeError>
pub fn to_str<'buf>(
&self,
buf: &'buf mut [u8]
) -> Result<&'buf mut str, EncodeError>
Creates a Crockford Base32 encoded string that represents this Ulid
Example
use ulid::Ulid;
let text = "01D39ZY06FGSCTVN4T2V9PKHFZ";
let ulid = Ulid::from_string(text).unwrap();
let mut buf = [0; ulid::ULID_LEN];
let new_text = ulid.to_str(&mut buf).unwrap();
assert_eq!(new_text, text);
sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Creates a Crockford Base32 encoded string that represents this Ulid
Example
use ulid::Ulid;
let text = "01D39ZY06FGSCTVN4T2V9PKHFZ";
let ulid = Ulid::from_string(text).unwrap();
assert_eq!(&ulid.to_string(), text);
Trait Implementations
sourceimpl<'de> Deserialize<'de> for Ulid
impl<'de> Deserialize<'de> for Ulid
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Ord for Ulid
impl Ord for Ulid
sourceimpl PartialOrd<Ulid> for Ulid
impl PartialOrd<Ulid> for Ulid
sourcefn partial_cmp(&self, other: &Ulid) -> Option<Ordering>
fn partial_cmp(&self, other: &Ulid) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Copy for Ulid
impl Eq for Ulid
impl StructuralEq for Ulid
impl StructuralPartialEq for Ulid
Auto Trait Implementations
impl RefUnwindSafe for Ulid
impl Send for Ulid
impl Sync for Ulid
impl Unpin for Ulid
impl UnwindSafe for Ulid
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more