diff --git a/src/types/range.rs b/src/types/range.rs index 8546f87c..1db88247 100644 --- a/src/types/range.rs +++ b/src/types/range.rs @@ -105,15 +105,13 @@ pub trait Normalizable { /// /// The logic here should match the logic performed by the equivalent /// Postgres type. - fn normalize(bound: RangeBound) - -> RangeBound; + fn normalize(bound: RangeBound) -> RangeBound where S: BoundSided; } macro_rules! bounded_normalizable( ($t:ident) => ( impl Normalizable for $t { - fn normalize(bound: RangeBound) - -> RangeBound { + fn normalize(bound: RangeBound) -> RangeBound { match (BoundSided::side(None::), bound.type_) { (Upper, Inclusive) => { assert!(bound.value != $t::MAX); @@ -134,8 +132,7 @@ bounded_normalizable!(i32) bounded_normalizable!(i64) impl Normalizable for Timespec { - fn normalize(bound: RangeBound) - -> RangeBound { + fn normalize(bound: RangeBound) -> RangeBound { bound } } @@ -182,14 +179,14 @@ pub enum BoundType { /// Represents a one-sided bound. /// /// The side is determined by the `S` phantom parameter. -pub struct RangeBound { +pub struct RangeBound { /// The value of the bound pub value: T, /// The type of the bound pub type_: BoundType } -impl Clone for RangeBound { +impl Clone for RangeBound where S: BoundSided, T: Clone { fn clone(&self) -> RangeBound { RangeBound { value: self.value.clone(), @@ -198,7 +195,7 @@ impl Clone for RangeBound { } } -impl fmt::Show for RangeBound { +impl fmt::Show for RangeBound where S: BoundSided, T: fmt::Show { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let chars = match self.type_ { Inclusive => ['[', ']'], @@ -212,7 +209,7 @@ impl fmt::Show for RangeBound { } } -impl PartialEq for RangeBound { +impl PartialEq for RangeBound where S: BoundSided, T: PartialEq { fn eq(&self, other: &RangeBound) -> bool { self.value == other.value && self.type_ == other.type_ } @@ -222,9 +219,9 @@ impl PartialEq for RangeBound { } } -impl Eq for RangeBound {} +impl Eq for RangeBound where S: BoundSided, T: Eq {} -impl PartialOrd for RangeBound { +impl PartialOrd for RangeBound where S: BoundSided, T: PartialOrd { fn partial_cmp(&self, other: &RangeBound) -> Option { match (BoundSided::side(None::), self.type_, other.type_, self.value.partial_cmp(&other.value)) { @@ -237,7 +234,7 @@ impl PartialOrd for RangeBound { } } -impl Ord for RangeBound { +impl Ord for RangeBound where S: BoundSided, T: Ord { fn cmp(&self, other: &RangeBound) -> Ordering { match (BoundSided::side(None::), self.type_, other.type_, self.value.cmp(&other.value)) { @@ -250,7 +247,7 @@ impl Ord for RangeBound { } } -impl RangeBound { +impl RangeBound where S: BoundSided, T: PartialOrd { /// Constructs a new range bound pub fn new(value: T, type_: BoundType) -> RangeBound { RangeBound { value: value, type_: type_ } @@ -267,9 +264,9 @@ impl RangeBound { } } -struct OptBound<'a, S, T:'a>(Option<&'a RangeBound>); +struct OptBound<'a, S: BoundSided, T:'a>(Option<&'a RangeBound>); -impl<'a, S: BoundSided, T: PartialEq> PartialEq for OptBound<'a, S, T> { +impl<'a, S, T> PartialEq for OptBound<'a, S, T> where S: BoundSided, T: PartialEq { fn eq(&self, &OptBound(ref other): &OptBound<'a, S, T>) -> bool { let &OptBound(ref self_) = self; self_ == other @@ -281,7 +278,7 @@ impl<'a, S: BoundSided, T: PartialEq> PartialEq for OptBound<'a, S, T> { } } -impl<'a, S: BoundSided, T: PartialOrd> PartialOrd for OptBound<'a, S, T> { +impl<'a, S, T> PartialOrd for OptBound<'a, S, T> where S: BoundSided, T: PartialOrd { fn partial_cmp(&self, other: &OptBound<'a, S, T>) -> Option { match (*self, *other, BoundSided::side(None::)) { (OptBound(None), OptBound(None), _) => Some(Equal), @@ -307,7 +304,7 @@ enum InnerRange { Option>) } -impl fmt::Show for Range { +impl fmt::Show for Range where T: fmt::Show { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { match self.inner { Empty => write!(fmt, "empty"), @@ -326,14 +323,14 @@ impl fmt::Show for Range { } } -impl Range { +impl Range where T: PartialOrd+Normalizable{ /// Creates a new range. /// /// If a bound is `None`, the range is unbounded in that direction. pub fn new(lower: Option>, upper: Option>) -> Range { - let lower = lower.map(|bound| Normalizable::normalize(bound)); - let upper = upper.map(|bound| Normalizable::normalize(bound)); + let lower = lower.map(Normalizable::normalize); + let upper = upper.map(Normalizable::normalize); match (&lower, &upper) { (&Some(ref lower), &Some(ref upper)) => { @@ -365,7 +362,7 @@ impl Range { } /// Returns the lower bound if it exists. - pub fn lower<'a>(&'a self) -> Option<&'a RangeBound> { + pub fn lower(&self) -> Option<&RangeBound> { match self.inner { Normal(Some(ref lower), _) => Some(lower), _ => None @@ -373,7 +370,7 @@ impl Range { } /// Returns the upper bound if it exists. - pub fn upper<'a>(&'a self) -> Option<&'a RangeBound> { + pub fn upper(&self) -> Option<&RangeBound> { match self.inner { Normal(_, Some(ref upper)) => Some(upper), _ => None @@ -406,7 +403,7 @@ impl Range { } } -fn order(a: T, b: T) -> (T, T) { +fn order(a: T, b: T) -> (T, T) where T: PartialOrd { if a < b { (a, b) } else { @@ -414,7 +411,7 @@ fn order(a: T, b: T) -> (T, T) { } } -impl Range { +impl Range where T: PartialOrd+Normalizable+Clone { /// Returns the intersection of this range with another pub fn intersect(&self, other: &Range) -> Range { if self.is_empty() || other.is_empty() { @@ -455,8 +452,7 @@ impl Range { if discontiguous { None } else { - Some(Range::new(l_lower.map(|v| v.clone()), - u_upper.map(|v| v.clone()))) + Some(Range::new(l_lower.map(|v| v.clone()), u_upper.map(|v| v.clone()))) } } }