pub struct MutexGenerator<F>where
F: FromIdGenerator,{ /* private fields */ }Expand description
thread safe snowflake generator
generates a given snowflake with the provided epoch and id value. epoch is
a specified date that can be in the future of
UNIX_EPOCH but not in the future of
now. the sequence value will always start at 1 when created.
this guards the previous time and sequence count behind an
Arc Mutex. the critical section
is small and will not block if its unable to get a valid snowflake.
if you want to wait for the next available id without calling the function
again check out blocking_next_id or
other waiting methods depending on how you want to wait for the next
available id.
type MyFlake = snowcloud::i64::SingleIdFlake<43, 8, 12>;
type MyCloud = snowcloud::sync::MutexGenerator<MyFlake>;
const START_TIME: u64 = 1679587200000;
let cloud = MyCloud::new(START_TIME, 1)
.expect("failed to create MyCloud");
println!("epoch: {:?}", cloud.epoch());
println!("ids: {}", cloud.ids());
println!("{:?}", cloud.next_id());Implementations§
Source§impl<F> MutexGenerator<F>where
F: FromIdGenerator,
impl<F> MutexGenerator<F>where
F: FromIdGenerator,
Sourcepub fn new<I>(epoch: u64, ids: I) -> Result<Self, Error>
pub fn new<I>(epoch: u64, ids: I) -> Result<Self, Error>
returns a new MutexGenerator
will return an error if ids is invalid, the timestamp is invalid, it fails to retrieve the current timestamp, or if the epoch is ahead of the current timestamp
Sourcepub fn epoch(&self) -> &SystemTime
pub fn epoch(&self) -> &SystemTime
returns epoch