Skip to main content

IdGenerator

Trait IdGenerator 

Source
pub trait IdGenerator<T> {
    type Error: Error;

    // Required method
    fn next_id(&self) -> Result<T, Self::Error>;

    // Provided methods
    fn format_id(&self, id: &T) -> String
       where T: Display { ... }
    fn next_string(&self) -> Result<String, Self::Error>
       where T: Display { ... }
}
Expand description

Generates IDs of type T.

The trait keeps the generated representation generic while still providing a string-producing helper. Numeric generators normally use the default Display based formatting. Generators with specialized textual forms can override IdGenerator::format_id.

Required Associated Types§

Source

type Error: Error

Error returned when generation fails.

Required Methods§

Source

fn next_id(&self) -> Result<T, Self::Error>

Generates the next ID value.

§Returns

The next generated ID.

§Errors

Returns Self::Error when the generator cannot allocate a unique value, for example because the clock moved backwards too far, the configured time range overflowed, or the random source failed.

Provided Methods§

Source

fn format_id(&self, id: &T) -> String
where T: Display,

Formats an already generated ID.

§Parameters
  • id: ID value to format.
§Returns

String representation of id.

Source

fn next_string(&self) -> Result<String, Self::Error>
where T: Display,

Generates the next ID and formats it as a string.

§Returns

String representation of the next ID.

§Errors

Returns the same error as IdGenerator::next_id.

Implementors§