From 43b0bb6a5e62a9262abcad63431bf9ac0c2d60cc Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 7 Oct 2024 19:19:53 +0000 Subject: [PATCH] add non-allocating fixed-size random string generator Signed-off-by: Jason Volk --- src/core/utils/rand.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/core/utils/rand.rs b/src/core/utils/rand.rs index b80671eb..d717c4bd 100644 --- a/src/core/utils/rand.rs +++ b/src/core/utils/rand.rs @@ -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() -> ArrayString { + let mut ret = ArrayString::::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) -> SystemTime {