Struct ulid::Generator[][src]

pub struct Generator { /* fields omitted */ }
Expand description

A Ulid generator that provides monotonically increasing Ulids

Implementations

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

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

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

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 chrono::Utc;
use rand::prelude::*;

let mut rng = StdRng::from_entropy();
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);

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 chrono::Utc;
use rand::prelude::*;

let dt = Utc::now();
let mut rng = StdRng::from_entropy();
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

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.