mirror of
https://github.com/girlbossceo/conduwuit.git
synced 2024-12-04 20:08:10 +00:00
add non-allocating fixed-size random string generator
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
a3ba31144a
commit
930755d44a
|
@ -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 a new issue