use super::*; macro_rules! impl_ops { ( $($Add:ident fn $add:ident & $AddAssign:ident fn $add_assign:ident,)* ) => {$( impl ops::$Add for Unit where T: ops::$Add, { type Output = Self; #[inline] fn $add(self, rhs: Self) -> Self::Output { Self(ops::$Add::$add(self.0, rhs.0), PhantomData) } } impl ops::$Add for Unit where T: ops::$Add, { type Output = Self; #[inline] fn $add(self, rhs: T) -> Self::Output { Self(ops::$Add::$add(self.0, rhs), PhantomData) } } impl ops::$AddAssign for Unit where T: ops::$AddAssign, { #[inline] fn $add_assign(&mut self, rhs: Self) { ops::$AddAssign::$add_assign(&mut self.0, rhs.0); } } impl ops::$AddAssign for Unit where T: ops::$AddAssign, { #[inline] fn $add_assign(&mut self, rhs: T) { ops::$AddAssign::$add_assign(&mut self.0, rhs); } } )*}; } impl_ops! { Add fn add & AddAssign fn add_assign, Sub fn sub & SubAssign fn sub_assign, Mul fn mul & MulAssign fn mul_assign, Div fn div & DivAssign fn div_assign, BitAnd fn bitand & BitAndAssign fn bitand_assign, BitOr fn bitor & BitOrAssign fn bitor_assign, BitXor fn bitxor & BitXorAssign fn bitxor_assign, Rem fn rem & RemAssign fn rem_assign, Shl fn shl & ShlAssign fn shl_assign, Shr fn shr & ShrAssign fn shr_assign, } // FIXME: Neg Not impl PartialEq for Unit where T: PartialEq, { #[inline] fn eq(&self, other: &Self) -> bool { PartialEq::eq(&self.0, &other.0) } } impl PartialEq for Unit where T: PartialEq, { #[inline] fn eq(&self, other: &T) -> bool { PartialEq::eq(&self.0, other) } } impl Eq for Unit {} impl PartialOrd for Unit where T: PartialOrd { #[inline] fn partial_cmp(&self, other: &Self) -> Option { self.0.partial_cmp(&other.0) } } impl PartialOrd for Unit where T: PartialOrd { #[inline] fn partial_cmp(&self, other: &T) -> Option { self.0.partial_cmp(other) } } use std::hash::{Hash, Hasher}; impl Hash for Unit { fn hash(&self, state: &mut H) { self.0.hash(state) } } impl Default for Unit { fn default() -> Self { Unit(T::default(), PhantomData) } } impl ops::Deref for Unit { type Target = T; fn deref(&self) -> &Self::Target { &self.0 } } impl ops::DerefMut for Unit { fn deref_mut(&mut self) -> &mut T { &mut self.0 } } impl ops::Index> for Unit where T: ops::Index, I: self::into_index::IntoIndex, { type Output = >::Output; fn index(&self, index: Unit) -> &Self::Output { let index: usize = index.0.into(); &self.0[index] } } impl ops::IndexMut> for Unit where T: ops::IndexMut, I: self::into_index::IntoIndex, { fn index_mut(&mut self, index: Unit) -> &mut Self::Output { let index: usize = index.0.into(); &mut self.0[index] } } // FIXME: Ranged indexing // FIXME: Vec.len() ? // FIXME: IntoIterator/Iter impl From for Unit { fn from(t: T) -> Self { Unit(t, PhantomData) } } #[cfg(test)] #[test] fn from() { struct Meters; let _: Unit = 0.0.into(); }