pub trait IndexOf {
type Item: PartialEq;
fn index_of(&self, item: &Self::Item) -> Option<usize>;
}
impl<T: PartialEq> IndexOf for [T] {
type Item = T;
#[inline]
fn index_of(&self, item: &T) -> Option<usize> {
for i in 0..self.len() {
if &self[i] == item {
return Some(i);
}
}
None
}
}
#[inline]
pub fn err_string<T, E: ToString>(err: Result<T, E>) -> Result<T, String> {
match err {
Ok(o) => Ok(o),
Err(e) => Err(e.to_string()),
}
}
pub trait PrecisionRound {
type Item;
fn round(&self, precision: i32) -> Self::Item;
}
impl PrecisionRound for f32 {
type Item = f32;
#[inline]
fn round(&self, precision: i32) -> f32 {
let p = 10.0f32.powi(precision);
(self * p + 0.5).floor() / p
}
}
impl PrecisionRound for f64 {
type Item = f64;
#[inline]
fn round(&self, precision: i32) -> f64 {
let p = 10.0f64.powi(precision);
(self * p + 0.5).floor() / p
}
}
pub trait FetchDefault {
type Item: Default;
fn fetch_default(self) -> Self::Item;
}
impl<T: Default> FetchDefault for Option<T> {
type Item = T;
#[inline]
fn fetch_default(self) -> Self::Item {
match self {
Some(t) => t,
_ => Self::Item::default(),
}
}
}
pub trait FetchCloneDefault {
type Item: Default + Clone;
fn fetch_clone_default(&self) -> Self::Item;
}
impl<T: Default + Clone> FetchCloneDefault for Option<T> {
type Item = T;
#[inline]
fn fetch_clone_default(&self) -> Self::Item {
match self {
Some(t) => t.clone(),
_ => Self::Item::default(),
}
}
}