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
impl Builder
sourcepub const fn new() -> Self
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);
sourcepub const fn instance(self, instance: u16) -> Self
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();
sourcepub const fn instance_checked(self, instance: u16) -> Option<Self>
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());
sourcepub const fn instance_unchecked(self, instance: u16) -> Self
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);
pub const fn epoch(self, epoch: SystemTime) -> Self
sourcepub fn build<T>(self) -> Twhere
T: From<Self>,
pub fn build<T>(self) -> Twhere
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();