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
impl Timeflake
Sourcepub fn new_random<R: Rng>(rng: &mut R) -> Self
pub fn new_random<R: Rng>(rng: &mut R) -> Self
Sourcepub fn from_bytes(bytes: [u8; 16]) -> Result<Self>
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.
Sourcepub fn from_bytes_checked(bytes: [u8; 16]) -> Self
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);Sourcepub fn from_components(timestamp: u64, random: &BigUint) -> Result<Self>
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.
Sourcepub fn from_components_checked(timestamp: u64, random: &BigUint) -> Self
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
timestampexceeds the maximum allowed value (MAX_TIMESTAMP). - The
randomcomponent 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);Sourcepub fn from_base62<S: AsRef<str>>(s: S) -> Result<Self>
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.
Sourcepub fn from_base62_checked<S: AsRef<str>>(s: S) -> Self
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");Sourcepub fn from_bigint(value: BigUint) -> Result<Self>
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();Sourcepub fn from_bigint_checked(value: BigUint) -> Self
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);Sourcepub fn from_uuid(uuid: Uuid) -> Result<Self>
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.
Sourcepub fn from_uuid_checked(uuid: Uuid) -> Self
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);