brutils/
lib.rs

1//! Various utilities for Rust
2
3use std::io;
4use std::io::{Write};
5use std::cmp::PartialOrd;
6
7pub mod random;
8pub mod math;
9
10mod color;
11pub use color::Color;
12
13mod thread_pool;
14pub use thread_pool::ThreadPool;
15
16/// Clamps given value between min and max
17pub fn clamp<T: PartialOrd>(value: T, min: T, max: T) -> T {
18    if value < min {
19        min
20    } else if value > max {
21        max
22    } else {
23        value
24    }
25}
26
27/// Prints message to console and the retrieves user input as string
28pub fn input(msg: &str) -> io::Result<String> {
29    print!("{}", msg);
30    io::stdout().flush()?;
31    let mut buffer = String::new();
32    io::stdin().read_line(&mut buffer)?;
33    let buffer = buffer.trim().to_owned();
34    Ok(buffer)
35}