1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use rand::Rng;
use std::ffi::CString;
use std::path::PathBuf;
use std::time::Duration;

pub fn to_vec32(vecin: Vec<u8>) -> Vec<u32> {
    unsafe { vecin.align_to::<u32>().1.to_vec() }
}

pub fn load_file(file: &PathBuf) -> Option<Vec<u8>> {
    let contents = std::fs::read(file);
    match contents {
        Ok(file_str) => Some(file_str),
        Err(err) => {
            eprintln!("[ERR] Impossible to read file {} : {}", file.display(), err);

            None
        }
    }
}

pub fn print_tick(val: bool) {
    if val {
        println!("✅");
    } else {
        println!("❌");
    }
}

pub fn cstr2string(mut cstr: Vec<i8>) -> String {
    let string = unsafe { CString::from_raw(cstr.as_mut_ptr()) };
    std::mem::forget(cstr);
    String::from(string.to_string_lossy())
}

pub fn get_fract_s(date: Duration) -> String {
    let millis = date.subsec_millis() as u64;
    let sec = date.as_secs();
    let tot = sec * 1000 + millis;
    format!("{}", tot)
}

// Bad f32 comparison with a epsilon
pub fn f32_cmp(a: f32, b: f32, epsilon: f32) -> bool {
    (a + epsilon) > b && b > (a - epsilon)
}

pub fn rand_vec<T>(len: usize, low: T, high: T) -> Vec<T>
where
    T: rand::distributions::uniform::SampleUniform + Copy,
{
    let mut rng = rand::thread_rng();
    let mut output: Vec<T> = Vec::with_capacity(len);

    for _ in 0..len {
        output.push(rng.gen_range(low, high))
    }

    output
}