Generator

Struct Generator 

Source
pub struct Generator<F>
where F: FromIdGenerator,
{ /* private fields */ }
Expand description

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

if you want to wait for the next available id without calling the function again check out blocking_next_id_mut 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::Generator<MyFlake>;

const START_TIME: u64 = 1679587200000;

let mut 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> Generator<F>

Source

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

returns a new Generator

will return an error if the primary id is invalid, the timestamp is invalid, it failes 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(&mut 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 Generator<F>

Source§

fn clone(&self) -> Generator<F>

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> IdGeneratorMut for Generator<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<<Generator<F> as IdGeneratorMut>::Id, <Generator<F> as IdGeneratorMut>::Error>

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

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

mutating call to get the next available id

Auto Trait Implementations§

§

impl<F> Freeze for Generator<F>

§

impl<F> RefUnwindSafe for Generator<F>

§

impl<F> Send for Generator<F>

§

impl<F> Sync for Generator<F>

§

impl<F> Unpin for Generator<F>

§

impl<F> UnwindSafe for Generator<F>

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.