Struct rusty_ulid::Ulid
source · pub struct Ulid { /* private fields */ }
Expand description
The ULID data type.
Implementations
sourceimpl Ulid
impl Ulid
sourcepub fn next_monotonic(previous_ulid: Ulid) -> Ulid
pub fn next_monotonic(previous_ulid: Ulid) -> Ulid
Creates the next monotonic ULID for the given previous_ulid
.
If the random part of previous_ulid
would overflow, this function returns a ULID with
the random part set to zero.
Examples
let previous_ulid = Ulid::from(0);
let ulid = Ulid::next_monotonic(previous_ulid);
assert_ne!(0, ulid.timestamp());
Panics
Panics if called after +10889-08-02T05:31:50.655Z
.
sourcepub fn next_strictly_monotonic(previous_ulid: Ulid) -> Option<Ulid>
pub fn next_strictly_monotonic(previous_ulid: Ulid) -> Option<Ulid>
Creates the next strictly monotonic ULID for the given previous_ulid
.
If the random part of previous_ulid
would overflow, this function returns None
.
Examples
let previous_ulid = Ulid::from(0);
let ulid = Ulid::next_strictly_monotonic(previous_ulid);
if let Some(ulid) = ulid {
assert_ne!(0, ulid.timestamp());
}
Panics
Panics if called after +10889-08-02T05:31:50.655Z
.
sourcepub fn next_monotonic_from_timestamp_with_rng<R>(
previous_ulid: Ulid,
timestamp: u64,
rng: &mut R
) -> Ulidwhere
R: Rng,
pub fn next_monotonic_from_timestamp_with_rng<R>(
previous_ulid: Ulid,
timestamp: u64,
rng: &mut R
) -> Ulidwhere
R: Rng,
Creates the next monotonic ULID with the given previous_ulid
, timestamp
obtaining randomness from rng
.
If the random part of previous_ulid
would overflow, this function returns a ULID with
the random part set to zero.
Examples
extern crate rand;
let previous_ulid = Ulid::from(0);
let ulid = Ulid::next_monotonic_from_timestamp_with_rng(previous_ulid, 0, &mut rand::thread_rng());
assert_eq!(ulid, Ulid::from(1));
extern crate rand;
let previous_ulid = Ulid::from(0x0000_0000_0000_FFFF_FFFF_FFFF_FFFF_FFFE);
let ulid = Ulid::next_monotonic_from_timestamp_with_rng(previous_ulid, 0, &mut rand::thread_rng());
assert_eq!(ulid, Ulid::from(0x0000_0000_0000_FFFF_FFFF_FFFF_FFFF_FFFF));
extern crate rand;
let previous_ulid = Ulid::from(0x0000_0000_0000_FFFF_FFFF_FFFF_FFFF_FFFF);
let ulid = Ulid::next_monotonic_from_timestamp_with_rng(previous_ulid, 0, &mut rand::thread_rng());
// overflow results in zero random part
assert_eq!(ulid, Ulid::from(0));
Panics
Panics if timestamp
is larger than 0xFFFF_FFFF_FFFF
.
sourcepub fn next_strictly_monotonic_from_timestamp_with_rng<R>(
previous_ulid: Ulid,
timestamp: u64,
rng: &mut R
) -> Option<Ulid>where
R: Rng,
pub fn next_strictly_monotonic_from_timestamp_with_rng<R>(
previous_ulid: Ulid,
timestamp: u64,
rng: &mut R
) -> Option<Ulid>where
R: Rng,
Creates the next strictly monotonic ULID with the given previous_ulid
, timestamp
obtaining randomness from rng
.
If the random part of previous_ulid
would overflow, this function returns None
.
Examples
extern crate rand;
let previous_ulid = Ulid::from(0);
let ulid = Ulid::next_strictly_monotonic_from_timestamp_with_rng(previous_ulid, 0, &mut rand::thread_rng());
assert_eq!(ulid, Some(Ulid::from(1)));
extern crate rand;
let previous_ulid = Ulid::from(0x0000_0000_0000_FFFF_FFFF_FFFF_FFFF_FFFE);
let ulid = Ulid::next_strictly_monotonic_from_timestamp_with_rng(previous_ulid, 0, &mut rand::thread_rng());
assert_eq!(ulid, Some(Ulid::from(0x0000_0000_0000_FFFF_FFFF_FFFF_FFFF_FFFF)));
extern crate rand;
let previous_ulid = Ulid::from(0x0000_0000_0000_FFFF_FFFF_FFFF_FFFF_FFFF);
let ulid = Ulid::next_strictly_monotonic_from_timestamp_with_rng(previous_ulid, 0, &mut rand::thread_rng());
// overflow results in None
assert_eq!(ulid, None);
Panics
Panics if timestamp
is larger than 0xFFFF_FFFF_FFFF
.
sourcepub fn timestamp(&self) -> u64
pub fn timestamp(&self) -> u64
Returns the timestamp of this ULID as number of non-leap milliseconds since January 1, 1970 0:00:00 UTC (aka “UNIX timestamp”).
Examples
let ulid = Ulid::from_str("01CAH7NXGRDJNE9B1NY7PQGYV7");
let timestamp = ulid?.timestamp();
assert_eq!(timestamp, 1523144390168);
sourcepub fn datetime(&self) -> DateTime<Utc>
pub fn datetime(&self) -> DateTime<Utc>
Returns the timestamp of this ULID as a DateTime<Utc>
.
Examples
let ulid = Ulid::from_str("01CAH7NXGRDJNE9B1NY7PQGYV7");
let datetime = ulid?.datetime();
assert_eq!(datetime.to_string(), "2018-04-07 23:39:50.168 UTC");
sourcepub fn increment(self) -> Ulid
pub fn increment(self) -> Ulid
Returns a new ULID with the random part incremented by one.
Overflowing the random part resets it to zero without influencing the timestamp.
Examples
let ulid = Ulid::from(0);
let incremented = ulid.increment();
assert_eq!(incremented, Ulid::from(1));
let ulid = Ulid::from(0x0000_0000_0000_FFFF_FFFF_FFFF_FFFF_FFFE);
let incremented = ulid.increment();
assert_eq!(incremented, Ulid::from(0x0000_0000_0000_FFFF_FFFF_FFFF_FFFF_FFFF));
let ulid = Ulid::from(0x0000_0000_0000_FFFF_FFFF_FFFF_FFFF_FFFF);
let incremented = ulid.increment();
assert_eq!(incremented, Ulid::from(0));
sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Returns the string representaton of this ULID.
Examples
let ulid = Ulid::from(0);
assert_eq!(ulid.to_string(), "00000000000000000000000000");
let ulid = Ulid::from(0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF);
assert_eq!(ulid.to_string(), "7ZZZZZZZZZZZZZZZZZZZZZZZZZ");
Trait Implementations
sourceimpl From<[u8; 16]> for Ulid
impl From<[u8; 16]> for Ulid
sourcefn from(bytes: [u8; 16]) -> Self
fn from(bytes: [u8; 16]) -> Self
Examples
let bytes: [u8; 16] = [
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xF0, 0x0F,
];
let ulid = Ulid::from(bytes);
let expected_ulid = Ulid::from(0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F);
assert_eq!(ulid, expected_ulid);
let bytes: [u8; 16] = [
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xF0, 0x0F,
];
let ulid : Ulid = bytes.into();
let expected_ulid = Ulid::from(0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F);
assert_eq!(ulid, expected_ulid);
sourceimpl From<(u64, u64)> for Ulid
impl From<(u64, u64)> for Ulid
sourcefn from(value: (u64, u64)) -> Self
fn from(value: (u64, u64)) -> Self
Examples
let tuple = (0x1122_3344_5566_7788, 0x99AA_BBCC_DDEE_F00F);
let ulid = Ulid::from(tuple);
let expected_ulid = Ulid::from(0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F);
assert_eq!(ulid, expected_ulid);
let tuple = (0x1122_3344_5566_7788, 0x99AA_BBCC_DDEE_F00F);
let ulid : Ulid = tuple.into();
let expected_ulid = Ulid::from(0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F);
assert_eq!(ulid, expected_ulid);
sourceimpl From<Ulid> for [u8; 16]
impl From<Ulid> for [u8; 16]
sourcefn from(ulid: Ulid) -> Self
fn from(ulid: Ulid) -> Self
Examples
let ulid = Ulid::from(0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F);
let bytes = <[u8; 16]>::from(ulid);
let expected_bytes: [u8; 16] = [
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xF0, 0x0F,
];
assert_eq!(bytes, expected_bytes);
let ulid = Ulid::from(0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F);
let bytes: [u8; 16] = ulid.into();
let expected_bytes: [u8; 16] = [
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xF0, 0x0F,
];
assert_eq!(bytes, expected_bytes);
sourceimpl From<Ulid> for (u64, u64)
impl From<Ulid> for (u64, u64)
sourcefn from(ulid: Ulid) -> Self
fn from(ulid: Ulid) -> Self
Examples
let ulid = Ulid::from(0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F);
let tuple = <(u64, u64)>::from(ulid);
let expected_tuple = (0x1122_3344_5566_7788, 0x99AA_BBCC_DDEE_F00F);
assert_eq!(tuple, expected_tuple);
let ulid = Ulid::from(0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F);
let tuple : (u64, u64) = ulid.into();
let expected_tuple = (0x1122_3344_5566_7788, 0x99AA_BBCC_DDEE_F00F);
assert_eq!(tuple, expected_tuple);
sourceimpl From<Ulid> for u128
impl From<Ulid> for u128
sourcefn from(ulid: Ulid) -> Self
fn from(ulid: Ulid) -> Self
Examples
let ulid = Ulid::from((0x1122_3344_5566_7788, 0x99AA_BBCC_DDEE_F00F));
let value = <u128>::from(ulid);
let expected_value = 0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F;
assert_eq!(value, expected_value);
let ulid = Ulid::from((0x1122_3344_5566_7788, 0x99AA_BBCC_DDEE_F00F));
let value : u128 = ulid.into();
let expected_value = 0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F;
assert_eq!(value, expected_value);
sourceimpl From<u128> for Ulid
impl From<u128> for Ulid
sourcefn from(value: u128) -> Self
fn from(value: u128) -> Self
Examples
let value = 0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F;
let ulid = Ulid::from(value);
let expected_ulid = Ulid::from((0x1122_3344_5566_7788, 0x99AA_BBCC_DDEE_F00F));
assert_eq!(ulid, expected_ulid);
let value = 0x1122_3344_5566_7788_99AA_BBCC_DDEE_F00F;
let ulid : Ulid = value.into();
let expected_ulid = Ulid::from((0x1122_3344_5566_7788, 0x99AA_BBCC_DDEE_F00F));
assert_eq!(ulid, expected_ulid);
sourceimpl Ord for Ulid
impl Ord for Ulid
1.21.0 · sourcefn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 · sourcefn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized + PartialOrd<Self>,
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>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more