Struct snowflaked::Builder

source ·
pub struct Builder { /* private fields */ }
Expand description

A builder for a snowflake Generator. This builder can be used for both Generator and sync::Generator.

Implementations§

source§

impl Builder

source

pub const fn new() -> Self

Creates a new Builder with the instance set to 0 and epoch set to UNIX_EPOCH.

§Examples
let generator: Generator = Builder::new().build();

assert_eq!(generator.instance(), 0);
assert_eq!(generator.epoch(), UNIX_EPOCH);
source

pub const fn instance(self, instance: u16) -> Self

Sets the instance value of the Builder.

§Panics

Panics if the provided instance exceeds the maximum value (2^10 -1).

§Examples
let generator: Generator = Builder::new().instance(400).build();

assert_eq!(generator.instance(), 400);

Providing an invalid instance panics:

let generator: Generator = Builder::new().instance(5000).build();
source

pub const fn instance_checked(self, instance: u16) -> Option<Self>

Sets the instance value of the Builder. Returns None if the provided instance exceeds the maximum value (2^10 -1).

§Examples
let builder = Builder::new().instance_checked(400);
assert!(builder.is_some());

Providing an invalid instance returns None:

let builder = Builder::new().instance_checked(5000);
assert!(builder.is_none());
source

pub const fn instance_unchecked(self, instance: u16) -> Self

Sets the instance value of the Builder without validating whether instance exceeds the maximum value (2^10 - 1).

§Examples
let generator: Generator = Builder::new().instance_unchecked(400).build();

assert_eq!(generator.instance(), 400);
source

pub const fn epoch(self, epoch: SystemTime) -> Self

source

pub fn build<T>(self) -> T
where T: From<Self>,

Consumes this Builder, returning the constructed generator. This function works with both Generator and sync::Generator.

§Examples
use snowflaked::{Builder, Generator};

let mut generator = Builder::new().build::<Generator>();

let id: u64 = generator.generate();

A sync::Generator can be constructed in the same way:

use snowflaked::Builder;
use snowflaked::sync::Generator;

let generator = Builder::new().build::<Generator>();

let id: u64 = generator.generate();

Trait Implementations§

source§

impl Clone for Builder

source§

fn clone(&self) -> Builder

Returns a copy 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 Debug for Builder

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<Builder> for Generator

Available on crate feature sync only.
source§

fn from(builder: Builder) -> Self

Converts to this type from the input type.
source§

impl From<Builder> for Generator

source§

fn from(builder: Builder) -> Self

Converts to this type from the input type.
source§

impl Copy for Builder

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

§

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

§

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

§

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.