pub struct Generator<'a, R, const N: usize = 64> { /* private fields */ }Expand description
Implementations§
Source§impl<'a, const N: usize> Generator<'a, ThreadRng, N>
impl<'a, const N: usize> Generator<'a, ThreadRng, N>
Sourcepub fn with_alphabet(alphabet: &'a Alphabet<N>) -> Self
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>
impl<'a> Generator<'a, ThreadRng>
Sourcepub fn with_size(size: usize) -> Self
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>
impl<'a, R: Rng, const N: usize> Generator<'a, R, N>
Sourcepub fn new(size: usize, alphabet: &'a Alphabet<N>, random: R) -> Self
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");Sourcepub fn size(self, size: usize) -> Self
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);Sourcepub fn alphabet<const M: usize>(
self,
alphabet: &Alphabet<M>,
) -> Generator<'_, R, M>
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')));Sourcepub fn write_to<W: Write>(&mut self, out: &mut W) -> Result
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
Generator::fmtGenerator::gen- [
Generator::gen_smartstring] Generator::fmt
Sourcepub fn fmt(&mut self) -> Fmt<'_, 'a, R, N>
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");