use std::convert::TryFrom;
pub mod bool256;
pub mod hash;
pub mod rand;
pub use self::{bool256::Bool256, rand::Rand};
pub const SIZES: &[usize] = &[0, 8, 16, 32, 64, 128, 192, 256];
pub trait Cast<T> {
fn saturating_cast(self) -> T;
}
impl Cast<u8> for usize {
fn saturating_cast(self) -> u8 {
u8::try_from(self).unwrap_or(u8::max_value())
}
}
pub fn saturating_cast<T: Cast<U>, U>(value: T) -> U {
value.saturating_cast()
}
pub fn vec_remove_item<T>(vec: &mut Vec<T>, item: &T) -> Option<T>
where
T: PartialEq,
{
let pos = vec.iter().position(|x| *x == *item)?;
Some(vec.remove(pos))
}
pub fn vec_remove_item_binary_search<T>(vec: &mut Vec<T>, item: &T) -> Option<T>
where
T: Ord,
{
let pos = vec.binary_search(item).ok()?;
Some(vec.remove(pos))
}