[][src]Struct ulid::Generator

pub struct Generator { /* fields omitted */ }

A Ulid generator that provides monotonically increasing Ulids

Methods

impl Generator[src]

pub fn new() -> Generator[src]

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);

pub fn with_source<R: RngCore + 'static>(source: R) -> Generator[src]

Create a new ulid generator for monotonically ordered ulids using the given rng

Example

use ulid::Generator;
use rand::rngs::SmallRng;
use rand::FromEntropy;

let mut gen = Generator::with_source(rand::rngs::SmallRng::from_entropy());

let ulid1 = gen.generate().unwrap();
let ulid2 = gen.generate().unwrap();

assert!(ulid1 < ulid2);

pub fn generate(&mut self) -> Result<Ulid, MonotonicError>[src]

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);

pub fn generate_from_datetime<T: TimeZone>(
    &mut self,
    datetime: DateTime<T>
) -> Result<Ulid, MonotonicError>
[src]

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 chrono::Utc;

let dt = Utc::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);

Trait Implementations

impl Default for Generator[src]

Auto Trait Implementations

impl !Send for Generator

impl !Sync for Generator

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.