Generator

Struct Generator 

Source
pub struct Generator<'a, R, const N: usize = 64> { /* private fields */ }
Expand description

Implementations§

Source§

impl<'a, const N: usize> Generator<'a, ThreadRng, N>

Source

pub fn with_alphabet(alphabet: &'a Alphabet<N>) -> Self

Create a new randoid generator using a specific alphabet

And using the default size and rand::thread_rng() as the RNG.

Source§

impl<'a> Generator<'a, ThreadRng>

Source

pub fn with_size(size: usize) -> Self

Create a new randoid generator that generates ids of a specific size

But use the default alphabet and rand::thread_rng() as the RNG.

Source§

impl<'a, R: Rng, const N: usize> Generator<'a, R, N>

Source

pub fn new(size: usize, alphabet: &'a Alphabet<N>, random: R) -> Self

Create a new, fully specified id generator

Create a new generator that genartes ids composed of size characters chosen at random from alphabet, using random as a source of random data.

§Examples
use randoid::{Generator, alphabet::HEX};

let rand = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(0x04040404);
let mut gen = Generator::new(8, &HEX, rand);
assert_eq!(gen.gen(), "905c2761");
assert_eq!(gen.gen(), "304ec655");
Source

pub fn size(self, size: usize) -> Self

Update the size of an existing generator

§Example

let id = Generator::default().size(32).gen();
assert_eq!(id.len(), 32);
Source

pub fn alphabet<const M: usize>( self, alphabet: &Alphabet<M>, ) -> Generator<'_, R, M>

Update the alphabet of an existing generator

§Example

let id = Generator::default().alphabet(&Alphabet::new(['a', 'b', 'c', 'd'])).gen();
assert!(id.chars().all(|c| matches!(c, 'a'..='d')));
Source

pub fn write_to<W: Write>(&mut self, out: &mut W) -> Result

Generate a new id, and write the result to out

This allows you to avoid creating a new string if you would simply be adding that string to something else.

§Examples
let mut ids = String::new();

let mut id_gen = randoid::Generator::default();
id_gen.write_to(&mut ids);
ids.push('\n');
id_gen.write_to(&mut ids);

assert_eq!(ids.len(), 21 * 2 + 1);
§See Also
Source

pub fn fmt(&mut self) -> Fmt<'_, 'a, R, N>

Return an object which implements std::fmt::Display

This allows you to pass a new generated id to write!(), format!(), etc. without having to create an intermediate string.

§Warning

The returned object will generate a unique id, each time it’s Display implementation is used. It uses interior mutability in order to avoid having to store the actual id. Similarly, the random data isn’t actually generated until it is written somewhere. In general I would advise against using it except as a temporary to a formatting macro.

§Examples
use randoid::Generator;

let mut generator = Generator::with_random(rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(1));

println!("Your new id is: {}", generator.fmt());

assert_eq!(format!("uid-{}", generator.fmt()), "uid-kkb3tf6ZyJm49m5J3xuB8");
let f = generator.fmt();

assert_eq!(f.to_string(), "5jO6j5xWvMx17zY3e9NbN");
assert_eq!(f.to_string(), "kGAK7hvw7AdqTcsFNZGtr");
Source

pub fn gen(&mut self) -> String

Generate a random id as a string

§Examples
let random_id = randoid::Generator::default().gen();
Source§

impl<'a, R: Rng> Generator<'a, R>

Source

pub fn with_random(random: R) -> Self

Create a new randoid generator from an Rng

Using the default size and alphabet

Trait Implementations§

Source§

impl<'a, R: Clone, const N: usize> Clone for Generator<'a, R, N>

Source§

fn clone(&self) -> Generator<'a, R, N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Generator<'static, ThreadRng>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'a, R, const N: usize> Freeze for Generator<'a, R, N>
where R: Freeze,

§

impl<'a, R, const N: usize> RefUnwindSafe for Generator<'a, R, N>
where R: RefUnwindSafe,

§

impl<'a, R, const N: usize> Send for Generator<'a, R, N>
where R: Send,

§

impl<'a, R, const N: usize> Sync for Generator<'a, R, N>
where R: Sync,

§

impl<'a, R, const N: usize> Unpin for Generator<'a, R, N>
where R: Unpin,

§

impl<'a, R, const N: usize> UnwindSafe for Generator<'a, R, N>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V