Timeflake

Struct Timeflake 

Source
pub struct Timeflake { /* private fields */ }
Expand description

Represents a Timeflake, a unique identifier combining timestamp and random data.

A Timeflake is a 128-bit, roughly-ordered, URL-safe UUID compatible with the existing UUID ecosystem.

§Example

use timeflake::Timeflake;

fn main() {
    let mut rng = rand::rng();
    let flake = Timeflake::new_random(&mut rng);
    println!("{flake}");
}

Implementations§

Source§

impl Timeflake

Source

pub fn new_random<R: Rng>(rng: &mut R) -> Self

Create a new Timeflake with generated random component and current UNIX timestamp.

§Examples
use timeflake::Timeflake;

let mut rng = rand::rng();
let flake = Timeflake::new_random(&mut rng);
Source

pub fn from_bytes(bytes: [u8; 16]) -> Result<Self>

Create a new Timeflake from full 16 bytes.

§Errors

Returns Error::InvalidFlake if the bytes represent a value outside the valid range.

Source

pub fn from_bytes_checked(bytes: [u8; 16]) -> Self

Create a new Timeflake from 16 bytes, panicking if the value is invalid.

This function behaves similarly to Timeflake::from_bytes, but will panic if the value exceeds the maximum allowed range.

§Panics

Panics if the bytes represent a value outside the valid range.

§Examples
use timeflake::Timeflake;

let bytes: [u8; 16] = [0x00; 16];
let flake = Timeflake::from_bytes_checked(bytes);
Source

pub fn from_components(timestamp: u64, random: &BigUint) -> Result<Self>

Create a new Timeflake from UNIX timestamp and random components.

§Errors

Returns Error::InvalidTimestamp if the timestamp exceeds the maximum allowed value. Returns Error::InvalidRandom if the random component exceeds the maximum allowed value.

Source

pub fn from_components_checked(timestamp: u64, random: &BigUint) -> Self

Create a new Timeflake from timestamp and random components, panicking if the values are invalid.

This function behaves similarly to Timeflake::from_components, but will panic if either the timestamp or the random component exceeds the maximum allowed value.

§Panics

Panics if:

  • The timestamp exceeds the maximum allowed value (MAX_TIMESTAMP).
  • The random component exceeds the maximum allowed value.
§Examples
use num_bigint::BigUint;
use timeflake::Timeflake;

let timestamp: u64 = 1_674_354_800; // Valid timestamp
let random = BigUint::from(12345u64); // Valid random component
let flake = Timeflake::from_components_checked(timestamp, &random);
Source

pub fn from_base62<S: AsRef<str>>(s: S) -> Result<Self>

Create a new Timeflake from a base62-encoded string.

§Errors

Returns Error::ParseError if the input string is not a valid base62 encoding. Returns Error::InvalidFlake if the decoded value exceeds the maximum allowed value.

Source

pub fn from_base62_checked<S: AsRef<str>>(s: S) -> Self

Create a new Timeflake from a base62-encoded string, panicking if the value is invalid.

This function behaves similarly to Timeflake::from_base62, but will panic if the value exceeds the maximum allowed range or if the input is not a valid base62 encoding.

§Panics

Panics if the input string is not valid base62 or if the decoded value exceeds the maximum allowed range.

§Examples
use timeflake::Timeflake;

let flake = Timeflake::from_base62_checked("00000000000000000000");
Source

pub fn from_bigint(value: BigUint) -> Result<Self>

Create a new Timeflake from a BigUint.

§Errors

Returns Error::InvalidFlake if the value exceeds the maximum allowed range.

§Examples
use timeflake::Timeflake;
use num_bigint::BigUint;

let value = BigUint::from(12345u64);
let flake = Timeflake::from_bigint(value).unwrap();
Source

pub fn from_bigint_checked(value: BigUint) -> Self

Create a new Timeflake from a BigUint, panicking if the value is invalid.

This function behaves similarly to Timeflake::from_bigint, but will panic if the value exceeds the maximum allowed range.

§Panics

Panics if the value exceeds the maximum allowed range.

§Examples
use timeflake::Timeflake;
use num_bigint::BigUint;

let value = BigUint::from(12345u64);
let flake = Timeflake::from_bigint_checked(value);
Source

pub fn from_uuid(uuid: Uuid) -> Result<Self>

Create a new Timeflake from a UUID.

§Errors

Returns Error::InvalidFlake if the UUID represents a value outside the valid range.

Source

pub fn from_uuid_checked(uuid: Uuid) -> Self

Create a new Timeflake from a UUID, panicking if the value is invalid.

This function behaves similarly to Timeflake::from_uuid, but will panic if the UUID represents a value outside the valid range.

§Panics

Panics if the UUID represents a value outside the valid range.

§Examples
use timeflake::Timeflake;
use uuid::Uuid;

let uuid = Uuid::parse_str("00000000-0000-4000-8000-000000000000").unwrap();
let flake = Timeflake::from_uuid_checked(uuid);
Source

pub fn to_uuid(&self) -> Uuid

Returns the UUID representation of this Timeflake.

Source

pub fn to_base62(&self) -> String

Returns the base62 string representation of this Timeflake.

Source

pub fn timestamp(&self) -> u64

Returns the timestamp component of this Timeflake.

Source

pub fn random(&self) -> BigUint

Returns the random component of this Timeflake.

Source

pub fn to_hex(&self) -> String

Returns the hexadecimal string representation of this Timeflake.

Source

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

Returns the raw bytes of this Timeflake.

Source

pub fn to_bigint(&self) -> &BigUint

Returns the integer value of this Timeflake.

Trait Implementations§

Source§

impl Clone for Timeflake

Source§

fn clone(&self) -> Timeflake

Returns a duplicate 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 Timeflake

Source§

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

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

impl Display for Timeflake

Source§

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

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

impl FromStr for Timeflake

Source§

fn from_str(s: &str) -> Result<Self>

Parse a string as a Timeflake accepting both hexadecimal and base62 encodings.

Source§

type Err = Error

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

impl Hash for Timeflake

Source§

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

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 Timeflake

Source§

fn cmp(&self, other: &Self) -> 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,

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

impl PartialEq for Timeflake

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Timeflake

Source§

fn partial_cmp(&self, other: &Self) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for Timeflake

Auto Trait Implementations§

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> 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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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,

Source§

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§

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>,

Source§

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>,

Source§

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