extern crate libc;
use std::ffi::OsString;
use std::str;
pub fn timestamp_cycles() -> u64 {
#[cfg(target_arch = "x86_64")]
unsafe {
std::arch::x86_64::_rdtsc() as u64
}
#[cfg(not(target_arch = "x86_64"))]
{
const MONOTONIC_CLOCK_MULTPIPLIER: u64 = 1_000_000_000;
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe {
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
}
(ts.tv_sec as u64) * MONOTONIC_CLOCK_MULTPIPLIER + (ts.tv_nsec as u64)
}
}
pub fn xor_psuedo_rng_u32() -> u32 {
let mut t: u32 = timestamp_cycles() as u32;
t ^= t << 13;
t ^= t >> 17;
t ^ (t << 5)
}
fn xor_psuedo_rng_u8_alphanumerics(rand_fn: &dyn Fn() -> u32) -> Vec<u8> {
let mut r = vec![];
fn between(lower: u8, upper: u8, to_check: u8) -> bool {
(to_check >= lower) && (to_check <= upper)
}
for n in &rand_fn().to_ne_bytes() {
if between(48, 57, *n) || between(65, 90, *n) || between(97, 122, *n) {
r.push(*n);
}
}
r
}
fn rand_alphanumerics_impl(rand_fn: &dyn Fn() -> u32, len: usize) -> OsString {
let mut buf = OsString::new();
let mut done = 0;
loop {
for n in xor_psuedo_rng_u8_alphanumerics(rand_fn) {
done += 1;
buf.push(str::from_utf8(&[n]).unwrap_or("_"));
if done >= len {
return buf;
}
}
}
}
pub fn rand_alphanumerics(len: usize) -> OsString {
rand_alphanumerics_impl(&xor_psuedo_rng_u32, len)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_timestamp_cycles() {
for _ in 0..1000 {
assert!(timestamp_cycles() < timestamp_cycles());
}
}
#[test]
fn test_xor_psuedo_rng_u32() {
for _ in 0..1000 {
assert_ne!(xor_psuedo_rng_u32(), xor_psuedo_rng_u32());
}
}
#[test]
fn test_xor_psuedo_rng_u8_alphas() {
let i = 3612982; let s = xor_psuedo_rng_u8_alphanumerics(&|| i);
assert_eq!(vec![54, 55], s);
}
#[test]
fn test_rand_alphanumerics_impl() {
let s = rand_alphanumerics_impl(&|| 14134, 5);
assert_eq!("67676", s);
}
#[test]
fn test_rand_alphanumerics() {
let s = rand_alphanumerics(5);
assert_eq!(5, s.len());
}
}