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);Trait Implementations§
source§impl Ord for Upid
impl Ord for Upid
source§impl PartialEq for Upid
impl PartialEq for Upid
source§impl PartialOrd for Upid
impl PartialOrd for Upid
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moreimpl Copy for Upid
impl Eq for Upid
impl StructuralPartialEq for Upid
Auto Trait Implementations§
impl Freeze for Upid
impl RefUnwindSafe for Upid
impl Send for Upid
impl Sync for Upid
impl Unpin for Upid
impl UnwindSafe for Upid
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)