MutexGenerator

Struct MutexGenerator 

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

Source

pub fn new<I>(epoch: u64, ids: I) -> Result<Self, Error>
where I: Into<F::IdSegType>,

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

Source

pub fn epoch(&self) -> &SystemTime

returns epoch

Source

pub fn ids(&self) -> &F::IdSegType

returns ids

type is determined by the provided snowflake

Source

pub fn next_id(&self) -> Result<F, Error>

retrieves the next available id

if the current timestamp reaches max, the max sequence value is reached, or if it fails to get the current timestamp this will return an error.

Trait Implementations§

Source§

impl<F> Clone for MutexGenerator<F>

Source§

fn clone(&self) -> Self

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<F> IdGenerator for MutexGenerator<F>
where F: FromIdGenerator,

Source§

type Error = Error

the potential error that could be returned from next_id
Source§

type Id = F

the actual Id type that is returned from next_id
Source§

type Output = Result<<MutexGenerator<F> as IdGenerator>::Id, <MutexGenerator<F> as IdGenerator>::Error>

to help with allowing for different situations, Output can what ever is needed. a Result or if used in an async context then an impl of Future
Source§

fn next_id(&self) -> Self::Output

call to get the next available id

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