pub struct Upid(pub u128);Expand description
A Upid is a unique 128-bit identifier is sortable and has a useful prefix.
It is encoded as a 26 character string using a custom base32 alphabet based on Crockford’s, but with lower-case and prioritising letters over numerals. In the binary, the first 40 bits are a unix timestamp with 256ms precision, the next 64 are random bits, and the last 24 are the prefix and version identifier.
Tuple Fields§
§0: u128Implementations§
Source§impl Upid
impl Upid
Sourcepub fn new(prefix: &str) -> Upid
pub fn new(prefix: &str) -> Upid
Creates a new Upid with the provided prefix and current time (UTC)
The prefix should only contain lower-case latin alphabet characters.
§Example
use upid::Upid;
let my_upid = Upid::new("user");Sourcepub fn from_prefix(prefix: &str) -> Upid
pub fn from_prefix(prefix: &str) -> Upid
Creates a Upid with the provided prefix and current time (UTC)
The prefix should contain four lower-case latin alphabet characters.
§Example
use upid::Upid;
let my_upid = Upid::from_prefix("user");Sourcepub fn from_prefix_and_datetime(prefix: &str, datetime: SystemTime) -> Upid
pub fn from_prefix_and_datetime(prefix: &str, datetime: SystemTime) -> Upid
Creates a new Upid with the given prefix and datetime
The prefix should only contain lower-case latin alphabet characters.
This will take the maximum of the [SystemTime] argument and [SystemTime::UNIX_EPOCH]
as earlier times are not valid for a Upid timestamp
§Example
use std::time::{SystemTime, Duration};
use upid::Upid;
let upid = Upid::from_prefix_and_datetime("user", SystemTime::now());Sourcepub fn from_prefix_and_milliseconds(prefix: &str, milliseconds: u128) -> Upid
pub fn from_prefix_and_milliseconds(prefix: &str, milliseconds: u128) -> Upid
Creates a new Upid with the given prefix and timestamp in millisecons
The prefix should only contain lower-case latin alphabet characters.
§Example
use upid::Upid;
let ms: u128 = 1720568902000;
let upid = Upid::from_prefix_and_milliseconds("user", ms);Sourcepub fn from_string(encoded: &str) -> Result<Upid, DecodeError>
pub fn from_string(encoded: &str) -> Result<Upid, DecodeError>
Creates a Upid from a Base32 encoded string
§Example
use upid::Upid;
let text = "user_aaccvpp5guht4dts56je5a";
let result = Upid::from_string(text);
assert_eq!(&result.unwrap().to_string(), text);Sourcepub fn datetime(&self) -> SystemTime
pub fn datetime(&self) -> SystemTime
Gets the datetime of when this Upid was created accurate to around 256ms
§Example
use std::time::{SystemTime, Duration};
use upid::Upid;
let dt = SystemTime::now();
let upid = Upid::from_prefix_and_datetime("user", dt);
assert!(dt + Duration::from_millis(257) >= upid.datetime());Sourcepub fn prefix(&self) -> String
pub fn prefix(&self) -> String
Gets the prefix of this upid
§Example
use upid::Upid;
let prefix = "user";
let upid = Upid::from_prefix(prefix);
assert_eq!(upid.prefix(), prefix);Sourcepub const fn milliseconds(&self) -> u64
pub const fn milliseconds(&self) -> u64
Gets the timestamp section of this upid
§Example
use upid::Upid;
let ms: u128 = 1720568902000;
let upid = Upid::from_prefix_and_milliseconds("user", ms);
assert!(ms - u128::from(upid.milliseconds()) < 257);Sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Creates a Base32 encoded string that represents this Upid
§Example
use upid::Upid;
let text = "user_aaccvpp5guht4dts56je5a";
let upid = Upid::from_string(text).unwrap();
assert_eq!(&upid.to_string(), text);