use std::fmt;
use std::ops::Bound;
#[derive(PartialEq, Debug, Clone)]
pub struct Bounds<T> {
pub lower: Bound<T>,
pub upper: Bound<T>,
}
impl<T: fmt::Debug> fmt::Display for Bounds<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.lower {
Bound::Included(n) => write!(f, "{:?}", n)?,
Bound::Excluded(n) => write!(f, "{:?}=", n)?, Bound::Unbounded => {},
}
write!(f, "..")?;
match &self.upper {
Bound::Included(n) => write!(f, "={:?}", n)?,
Bound::Excluded(n) => write!(f, "{:?}", n)?,
Bound::Unbounded => {},
}
Ok(())
}
}
impl<T> Bounds<T>
{
pub(crate) fn convert<U>(self) -> Bounds<U>
where U: From<T>
{
let lower = match self.lower {
Bound::Included(t) => Bound::Included(U::from(t)),
Bound::Excluded(t) => Bound::Excluded(U::from(t)),
Bound::Unbounded => Bound::Unbounded,
};
let upper = match self.upper {
Bound::Included(t) => Bound::Included(U::from(t)),
Bound::Excluded(t) => Bound::Excluded(U::from(t)),
Bound::Unbounded => Bound::Unbounded,
};
Bounds { lower, upper }
}
}
pub fn copy_bound<T: Copy>(bound: Bound<&T>) -> Bound<T> {
match bound {
Bound::Unbounded => Bound::Unbounded,
Bound::Included(n) => Bound::Included(*n),
Bound::Excluded(n) => Bound::Excluded(*n),
}
}