pub struct Generator { /* private fields */ }Expand description
A Ulid generator that provides monotonically increasing Ulids
Implementations§
Source§impl Generator
impl Generator
Sourcepub const fn new() -> Generator
pub const fn new() -> Generator
Create a new ulid generator for monotonically ordered ulids
§Example
use ulid::Generator;
let mut gen = Generator::new();
let ulid1 = gen.generate().unwrap();
let ulid2 = gen.generate().unwrap();
assert!(ulid1 < ulid2);Sourcepub fn generate(&mut self) -> Result<Ulid, MonotonicError>
pub fn generate(&mut self) -> Result<Ulid, MonotonicError>
Generate a new Ulid. Each call is guaranteed to provide a Ulid with a larger value than the last call. If the random bits would overflow, this method will return an error.
use ulid::Generator;
let mut gen = Generator::new();
let ulid1 = gen.generate().unwrap();
let ulid2 = gen.generate().unwrap();
assert!(ulid1 < ulid2);Sourcepub fn generate_from_datetime(
&mut self,
datetime: SystemTime,
) -> Result<Ulid, MonotonicError>
pub fn generate_from_datetime( &mut self, datetime: SystemTime, ) -> Result<Ulid, MonotonicError>
Generate a new Ulid matching the given DateTime. Each call is guaranteed to provide a Ulid with a larger value than the last call. If the random bits would overflow, this method will return an error.
§Example
use ulid::Generator;
use std::time::SystemTime;
let dt = SystemTime::now();
let mut gen = Generator::new();
let ulid1 = gen.generate_from_datetime(dt).unwrap();
let ulid2 = gen.generate_from_datetime(dt).unwrap();
assert_eq!(ulid1.datetime(), ulid2.datetime());
assert!(ulid1 < ulid2);Sourcepub fn generate_with_source<R>(
&mut self,
source: &mut R,
) -> Result<Ulid, MonotonicError>
pub fn generate_with_source<R>( &mut self, source: &mut R, ) -> Result<Ulid, MonotonicError>
Generate a new monotonic increasing Ulid with the given source Each call is guaranteed to provide a Ulid with a larger value than the last call. If the random bits would overflow, this method will return an error.
§Example
use ulid::Generator;
use ulid::Ulid;
use std::time::SystemTime;
use rand::prelude::*;
let mut rng = StdRng::from_os_rng();
let mut gen = Generator::new();
let ulid1 = gen.generate_with_source(&mut rng).unwrap();
let ulid2 = gen.generate_with_source(&mut rng).unwrap();
assert!(ulid1 < ulid2);Sourcepub fn generate_from_datetime_with_source<R>(
&mut self,
datetime: SystemTime,
source: &mut R,
) -> Result<Ulid, MonotonicError>
pub fn generate_from_datetime_with_source<R>( &mut self, datetime: SystemTime, source: &mut R, ) -> Result<Ulid, MonotonicError>
Generate a new monotonic increasing Ulid with the given source matching the given DateTime Each call is guaranteed to provide a Ulid with a larger value than the last call. If the random bits would overflow, this method will return an error.
§Example
use ulid::Generator;
use std::time::SystemTime;
use rand::prelude::*;
let dt = SystemTime::now();
let mut rng = StdRng::from_os_rng();
let mut gen = Generator::new();
let ulid1 = gen.generate_from_datetime_with_source(dt, &mut rng).unwrap();
let ulid2 = gen.generate_from_datetime_with_source(dt, &mut rng).unwrap();
assert_eq!(ulid1.datetime(), ulid2.datetime());
assert!(ulid1 < ulid2);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Generator
impl RefUnwindSafe for Generator
impl Send for Generator
impl Sync for Generator
impl Unpin for Generator
impl UnwindSafe for Generator
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.