[][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.

Implementations

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 Clone for Ulid[src]

impl Copy for Ulid[src]

impl Debug for Ulid[src]

impl Default for Ulid[src]

impl Display for Ulid[src]

impl Eq for Ulid[src]

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

impl From<u128> for Ulid[src]

impl FromStr for Ulid[src]

type Err = EncodingError

The associated error which can be returned from parsing.

impl Hash for Ulid[src]

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

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

impl Into<u128> for Ulid[src]

impl Ord for Ulid[src]

impl PartialEq<Ulid> for Ulid[src]

impl PartialOrd<Ulid> for Ulid[src]

impl StructuralEq for Ulid[src]

impl StructuralPartialEq for Ulid[src]

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

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

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

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

impl<T> From<T> for T[src]

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

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

type Owned = T

The resulting type after obtaining ownership.

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.