add non-allocating fixed-size random string generator

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-07 19:19:53 +00:00 committed by strawberry
parent a5e85727b5
commit 43b0bb6a5e

View file

@ -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 {