add non-allocating fixed-size random string generator
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
a5e85727b5
commit
43b0bb6a5e
|
@ -3,6 +3,7 @@ use std::{
|
|||
time::{Duration, SystemTime},
|
||||
};
|
||||
|
||||
use arrayvec::ArrayString;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
pub fn string(length: usize) -> String {
|
||||
|
@ -13,6 +14,18 @@ pub fn string(length: usize) -> String {
|
|||
.collect()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn string_array<const LENGTH: usize>() -> ArrayString<LENGTH> {
|
||||
let mut ret = ArrayString::<LENGTH>::new();
|
||||
thread_rng()
|
||||
.sample_iter(&rand::distributions::Alphanumeric)
|
||||
.take(LENGTH)
|
||||
.map(char::from)
|
||||
.for_each(|c| ret.push(c));
|
||||
|
||||
ret
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn timepoint_secs(range: Range<u64>) -> SystemTime {
|
||||
|
|
Loading…
Reference in New Issue