Struct ambient_api::global::Ulid

source ·
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§

source§

impl Ulid

source

pub fn new() -> Ulid

Creates a new Ulid with the current time (UTC)

Example
use ulid::Ulid;

let my_ulid = Ulid::new();
source

pub fn with_source<R>(source: &mut R) -> Ulid
where R: Rng,

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

pub fn from_datetime(datetime: SystemTime) -> Ulid

Creates a new Ulid with the given datetime

This can be useful when migrating data to use Ulid identifiers.

This will take the maximum of the [SystemTime] argument and [SystemTime::UNIX_EPOCH] as earlier times are not valid for a Ulid timestamp

Example
use std::time::{SystemTime, Duration};
use ulid::Ulid;

let ulid = Ulid::from_datetime(SystemTime::now());
source

pub fn from_datetime_with_source<R>( datetime: SystemTime, source: &mut R ) -> Ulid
where R: Rng + ?Sized,

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

This will take the maximum of the [SystemTime] argument and [SystemTime::UNIX_EPOCH] as earlier times are not valid for a Ulid timestamp

Example
use std::time::{SystemTime, Duration};
use rand::prelude::*;
use ulid::Ulid;

let mut rng = StdRng::from_entropy();
let ulid = Ulid::from_datetime_with_source(SystemTime::now(), &mut rng);
source

pub fn datetime(&self) -> SystemTime

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

Example
use std::time::{SystemTime, Duration};
use ulid::Ulid;

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

assert!(
    dt + Duration::from_millis(1) >= ulid.datetime()
    && dt - Duration::from_millis(1) <= ulid.datetime()
);
source§

impl Ulid

source

pub const TIME_BITS: u8 = 48u8

The number of bits in a Ulid’s time portion

source

pub const RAND_BITS: u8 = 80u8

The number of bits in a Ulid’s random portion

source

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

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

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

pub const fn timestamp_ms(&self) -> u64

Gets the timestamp section of this ulid

Example
use std::time::{SystemTime, Duration};
use ulid::Ulid;

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

assert_eq!(u128::from(ulid.timestamp_ms()), dt.duration_since(SystemTime::UNIX_EPOCH).unwrap_or(Duration::ZERO).as_millis());
source

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

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

pub fn to_string(&self) -> String

Available on crate feature std only.

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

pub const fn is_nil(&self) -> bool

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

pub const fn increment(&self) -> Option<Ulid>

Increment the random number, make sure that the ts millis stays the same

source

pub const fn from_bytes(bytes: [u8; 16]) -> Ulid

Creates a Ulid using the provided bytes array.

Example
use ulid::Ulid;
let bytes = [0xFF; 16];

let ulid = Ulid::from_bytes(bytes);

assert_eq!(
    ulid.to_string(),
    "7ZZZZZZZZZZZZZZZZZZZZZZZZZ"
);
source

pub const fn to_bytes(&self) -> [u8; 16]

Returns the bytes of the Ulid in big-endian order.

Example
use ulid::Ulid;

let text = "7ZZZZZZZZZZZZZZZZZZZZZZZZZ";
let ulid = Ulid::from_string(text).unwrap();

assert_eq!(ulid.to_bytes(), [0xFF; 16]);

Trait Implementations§

source§

impl Clone for Ulid

source§

fn clone(&self) -> Ulid

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Ulid

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Ulid

source§

fn default() -> Ulid

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Ulid

source§

fn deserialize<D>( deserializer: D ) -> Result<Ulid, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Ulid

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl From<[u8; 16]> for Ulid

source§

fn from(bytes: [u8; 16]) -> Ulid

Converts to this type from the input type.
source§

impl From<(u64, u64)> for Ulid

source§

fn from(_: (u64, u64)) -> Ulid

Converts to this type from the input type.
source§

impl From<ProceduralMaterialHandle> for Ulid

source§

fn from(handle: ProceduralMaterialHandle) -> Ulid

Converts to this type from the input type.
source§

impl From<ProceduralMeshHandle> for Ulid

source§

fn from(handle: ProceduralMeshHandle) -> Ulid

Converts to this type from the input type.
source§

impl From<ProceduralSamplerHandle> for Ulid

source§

fn from(handle: ProceduralSamplerHandle) -> Ulid

Converts to this type from the input type.
source§

impl From<ProceduralTextureHandle> for Ulid

source§

fn from(handle: ProceduralTextureHandle) -> Ulid

Converts to this type from the input type.
source§

impl From<Ulid> for [u8; 16]

source§

fn from(ulid: Ulid) -> [u8; 16]

Converts to this type from the input type.
source§

impl From<Ulid> for (u64, u64)

source§

fn from(ulid: Ulid) -> (u64, u64)

Converts to this type from the input type.
source§

impl From<Ulid> for ProceduralMaterialHandle

source§

fn from(ulid: Ulid) -> ProceduralMaterialHandle

Converts to this type from the input type.
source§

impl From<Ulid> for ProceduralMeshHandle

source§

fn from(ulid: Ulid) -> ProceduralMeshHandle

Converts to this type from the input type.
source§

impl From<Ulid> for ProceduralSamplerHandle

source§

fn from(ulid: Ulid) -> ProceduralSamplerHandle

Converts to this type from the input type.
source§

impl From<Ulid> for ProceduralTextureHandle

source§

fn from(ulid: Ulid) -> ProceduralTextureHandle

Converts to this type from the input type.
source§

impl From<Ulid> for String

Available on crate feature std only.
source§

fn from(ulid: Ulid) -> String

Converts to this type from the input type.
source§

impl From<Ulid> for u128

source§

fn from(ulid: Ulid) -> u128

Converts to this type from the input type.
source§

impl From<u128> for Ulid

source§

fn from(value: u128) -> Ulid

Converts to this type from the input type.
source§

impl FromStr for Ulid

§

type Err = DecodeError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Ulid, <Ulid as FromStr>::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Ulid

source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

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

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Ulid

source§

fn cmp(&self, other: &Ulid) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Ulid

source§

fn eq(&self, other: &Ulid) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Ulid

source§

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 · source§

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 · source§

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
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Ulid

source§

fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Copy for Ulid

source§

impl Eq for Ulid

source§

impl StructuralEq for Ulid

source§

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AsAny for T
where T: Any,

source§

fn as_any(&self) -> &(dyn Any + 'static)

source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

source§

fn type_name(&self) -> &'static str

Gets the type name of self
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
source§

impl<T> Downcast for T
where T: AsAny + ?Sized,

source§

fn is<T>(&self) -> bool
where T: AsAny,

Returns true if the boxed type is the same as T. Read more
source§

fn downcast_ref<T>(&self) -> Option<&T>
where T: AsAny,

Forward to the method defined on the type Any.
source§

fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: AsAny,

Forward to the method defined on the type Any.
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> ElementComponentName for T

source§

fn element_component_name(&self) -> &'static str

Returns the name of the type implementing ElementComponent.
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> AnyCloneable for T
where T: Clone + Debug + Any + 'static,

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,