[][src]Struct ulid::Ulid

pub struct Ulid(pub u128);

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.

Methods

impl Ulid[src]

pub fn new() -> Ulid[src]

Creates a new Ulid with the current time (UTC)

Example

use ulid::Ulid;

let my_ulid = Ulid::new();

pub fn with_source<R: Rng>(source: &mut R) -> Ulid[src]

Creates a new Ulid using data from the given random number generator

Example

use rand::FromEntropy;
use rand::rngs::SmallRng;
use ulid::Ulid;

let mut rng = SmallRng::from_entropy();
let ulid = Ulid::with_source(&mut rng);

pub fn from_datetime<T: TimeZone>(datetime: DateTime<T>) -> Ulid[src]

Creates a new Ulid with the given datetime

This can be useful when migrating data to use Ulid identifiers

Example

use chrono::offset::Utc;
use ulid::Ulid;

let ulid = Ulid::from_datetime(Utc::now());

pub fn from_datetime_with_source<T, R>(
    datetime: DateTime<T>,
    source: &mut R
) -> Ulid where
    T: TimeZone,
    R: Rng
[src]

Creates a new Ulid with the given datetime and random number generator

Example

use chrono::offset::Utc;
use rand::FromEntropy;
use rand::rngs::SmallRng;
use ulid::Ulid;

let mut rng = SmallRng::from_entropy();
let ulid = Ulid::from_datetime_with_source(Utc::now(), &mut rng);

pub fn from_string(encoded: &str) -> Result<Ulid, EncodingError>[src]

Creates a Ulid from a Crockford Base32 encoded string

An EncodingError 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);

pub fn nil() -> Ulid[src]

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"
);

pub fn datetime(&self) -> DateTime<Utc>[src]

Gets the datetime of when this Ulid was created accurate to 1ms

Example

use chrono::Duration;
use chrono::offset::Utc;
use ulid::Ulid;

let dt = Utc::now();
let ulid = Ulid::from_datetime(dt);

assert!((dt - ulid.datetime()) < Duration::milliseconds(1));

pub fn timestamp_ms(&self) -> u64[src]

Gets the timestamp section of this ulid

Example

use chrono::offset::Utc;
use ulid::Ulid;

let dt = Utc::now();
let ulid = Ulid::from_datetime(dt);

assert_eq!(ulid.timestamp_ms(), dt.timestamp_millis() as u64);

pub fn to_string(&self) -> String[src]

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);

pub fn is_nil(&self) -> bool[src]

Test if the Ulid is nil

Example

use ulid::Ulid;

let ulid = Ulid::new();
assert!(!ulid.is_nil());

let nil = Ulid::nil();
assert!(nil.is_nil());

Trait Implementations

impl Eq for Ulid[src]

impl Clone for Ulid[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<'a> Into<String> for &'a Ulid[src]

impl Into<(u64, u64)> for Ulid[src]

impl Into<u128> for Ulid[src]

impl Copy for Ulid[src]

impl PartialOrd<Ulid> for Ulid[src]

impl PartialEq<Ulid> for Ulid[src]

impl Default for Ulid[src]

impl Ord for Ulid[src]

fn max(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the minimum of two values. Read more

impl From<(u64, u64)> for Ulid[src]

impl From<u128> for Ulid[src]

impl Hash for Ulid[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl Debug for Ulid[src]

impl Display for Ulid[src]

impl FromStr for Ulid[src]

type Err = EncodingError

The associated error which can be returned from parsing.

Auto Trait Implementations

impl Send for Ulid

impl Sync for Ulid

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]