Skip to content

Commit 6199287

Browse files
committed
Cleanup in types::range
1 parent e0d8682 commit 6199287

1 file changed

Lines changed: 23 additions & 27 deletions

File tree

src/types/range.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,13 @@ pub trait Normalizable {
105105
///
106106
/// The logic here should match the logic performed by the equivalent
107107
/// Postgres type.
108-
fn normalize<S: BoundSided>(bound: RangeBound<S, Self>)
109-
-> RangeBound<S, Self>;
108+
fn normalize<S>(bound: RangeBound<S, Self>) -> RangeBound<S, Self> where S: BoundSided;
110109
}
111110

112111
macro_rules! bounded_normalizable(
113112
($t:ident) => (
114113
impl Normalizable for $t {
115-
fn normalize<S: BoundSided>(bound: RangeBound<S, $t>)
116-
-> RangeBound<S, $t> {
114+
fn normalize<S: BoundSided>(bound: RangeBound<S, $t>) -> RangeBound<S, $t> {
117115
match (BoundSided::side(None::<S>), bound.type_) {
118116
(Upper, Inclusive) => {
119117
assert!(bound.value != $t::MAX);
@@ -134,8 +132,7 @@ bounded_normalizable!(i32)
134132
bounded_normalizable!(i64)
135133

136134
impl Normalizable for Timespec {
137-
fn normalize<S: BoundSided>(bound: RangeBound<S, Timespec>)
138-
-> RangeBound<S, Timespec> {
135+
fn normalize<S: BoundSided>(bound: RangeBound<S, Timespec>) -> RangeBound<S, Timespec> {
139136
bound
140137
}
141138
}
@@ -182,14 +179,14 @@ pub enum BoundType {
182179
/// Represents a one-sided bound.
183180
///
184181
/// The side is determined by the `S` phantom parameter.
185-
pub struct RangeBound<S, T> {
182+
pub struct RangeBound<S: BoundSided, T> {
186183
/// The value of the bound
187184
pub value: T,
188185
/// The type of the bound
189186
pub type_: BoundType
190187
}
191188

192-
impl<S: BoundSided, T: Clone> Clone for RangeBound<S, T> {
189+
impl<S, T> Clone for RangeBound<S, T> where S: BoundSided, T: Clone {
193190
fn clone(&self) -> RangeBound<S, T> {
194191
RangeBound {
195192
value: self.value.clone(),
@@ -198,7 +195,7 @@ impl<S: BoundSided, T: Clone> Clone for RangeBound<S, T> {
198195
}
199196
}
200197

201-
impl<S: BoundSided, T: fmt::Show> fmt::Show for RangeBound<S, T> {
198+
impl<S, T> fmt::Show for RangeBound<S, T> where S: BoundSided, T: fmt::Show {
202199
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
203200
let chars = match self.type_ {
204201
Inclusive => ['[', ']'],
@@ -212,7 +209,7 @@ impl<S: BoundSided, T: fmt::Show> fmt::Show for RangeBound<S, T> {
212209
}
213210
}
214211

215-
impl<S: BoundSided, T: PartialEq> PartialEq for RangeBound<S, T> {
212+
impl<S, T> PartialEq for RangeBound<S, T> where S: BoundSided, T: PartialEq {
216213
fn eq(&self, other: &RangeBound<S, T>) -> bool {
217214
self.value == other.value && self.type_ == other.type_
218215
}
@@ -222,9 +219,9 @@ impl<S: BoundSided, T: PartialEq> PartialEq for RangeBound<S, T> {
222219
}
223220
}
224221

225-
impl<S: BoundSided, T: Eq> Eq for RangeBound<S, T> {}
222+
impl<S, T> Eq for RangeBound<S, T> where S: BoundSided, T: Eq {}
226223

227-
impl<S: BoundSided, T: PartialOrd> PartialOrd for RangeBound<S, T> {
224+
impl<S, T> PartialOrd for RangeBound<S, T> where S: BoundSided, T: PartialOrd {
228225
fn partial_cmp(&self, other: &RangeBound<S, T>) -> Option<Ordering> {
229226
match (BoundSided::side(None::<S>), self.type_, other.type_,
230227
self.value.partial_cmp(&other.value)) {
@@ -237,7 +234,7 @@ impl<S: BoundSided, T: PartialOrd> PartialOrd for RangeBound<S, T> {
237234
}
238235
}
239236

240-
impl<S: BoundSided, T: Ord> Ord for RangeBound<S, T> {
237+
impl<S, T> Ord for RangeBound<S, T> where S: BoundSided, T: Ord {
241238
fn cmp(&self, other: &RangeBound<S, T>) -> Ordering {
242239
match (BoundSided::side(None::<S>), self.type_, other.type_,
243240
self.value.cmp(&other.value)) {
@@ -250,7 +247,7 @@ impl<S: BoundSided, T: Ord> Ord for RangeBound<S, T> {
250247
}
251248
}
252249

253-
impl<S: BoundSided, T: PartialOrd> RangeBound<S, T> {
250+
impl<S, T> RangeBound<S, T> where S: BoundSided, T: PartialOrd {
254251
/// Constructs a new range bound
255252
pub fn new(value: T, type_: BoundType) -> RangeBound<S, T> {
256253
RangeBound { value: value, type_: type_ }
@@ -267,9 +264,9 @@ impl<S: BoundSided, T: PartialOrd> RangeBound<S, T> {
267264
}
268265
}
269266

270-
struct OptBound<'a, S, T:'a>(Option<&'a RangeBound<S, T>>);
267+
struct OptBound<'a, S: BoundSided, T:'a>(Option<&'a RangeBound<S, T>>);
271268

272-
impl<'a, S: BoundSided, T: PartialEq> PartialEq for OptBound<'a, S, T> {
269+
impl<'a, S, T> PartialEq for OptBound<'a, S, T> where S: BoundSided, T: PartialEq {
273270
fn eq(&self, &OptBound(ref other): &OptBound<'a, S, T>) -> bool {
274271
let &OptBound(ref self_) = self;
275272
self_ == other
@@ -281,7 +278,7 @@ impl<'a, S: BoundSided, T: PartialEq> PartialEq for OptBound<'a, S, T> {
281278
}
282279
}
283280

284-
impl<'a, S: BoundSided, T: PartialOrd> PartialOrd for OptBound<'a, S, T> {
281+
impl<'a, S, T> PartialOrd for OptBound<'a, S, T> where S: BoundSided, T: PartialOrd {
285282
fn partial_cmp(&self, other: &OptBound<'a, S, T>) -> Option<Ordering> {
286283
match (*self, *other, BoundSided::side(None::<S>)) {
287284
(OptBound(None), OptBound(None), _) => Some(Equal),
@@ -307,7 +304,7 @@ enum InnerRange<T> {
307304
Option<RangeBound<UpperBound, T>>)
308305
}
309306

310-
impl<T: fmt::Show> fmt::Show for Range<T> {
307+
impl<T> fmt::Show for Range<T> where T: fmt::Show {
311308
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
312309
match self.inner {
313310
Empty => write!(fmt, "empty"),
@@ -326,14 +323,14 @@ impl<T: fmt::Show> fmt::Show for Range<T> {
326323
}
327324
}
328325

329-
impl<T: PartialOrd+Normalizable> Range<T> {
326+
impl<T> Range<T> where T: PartialOrd+Normalizable{
330327
/// Creates a new range.
331328
///
332329
/// If a bound is `None`, the range is unbounded in that direction.
333330
pub fn new(lower: Option<RangeBound<LowerBound, T>>,
334331
upper: Option<RangeBound<UpperBound, T>>) -> Range<T> {
335-
let lower = lower.map(|bound| Normalizable::normalize(bound));
336-
let upper = upper.map(|bound| Normalizable::normalize(bound));
332+
let lower = lower.map(Normalizable::normalize);
333+
let upper = upper.map(Normalizable::normalize);
337334

338335
match (&lower, &upper) {
339336
(&Some(ref lower), &Some(ref upper)) => {
@@ -365,15 +362,15 @@ impl<T: PartialOrd+Normalizable> Range<T> {
365362
}
366363

367364
/// Returns the lower bound if it exists.
368-
pub fn lower<'a>(&'a self) -> Option<&'a RangeBound<LowerBound, T>> {
365+
pub fn lower(&self) -> Option<&RangeBound<LowerBound, T>> {
369366
match self.inner {
370367
Normal(Some(ref lower), _) => Some(lower),
371368
_ => None
372369
}
373370
}
374371

375372
/// Returns the upper bound if it exists.
376-
pub fn upper<'a>(&'a self) -> Option<&'a RangeBound<UpperBound, T>> {
373+
pub fn upper(&self) -> Option<&RangeBound<UpperBound, T>> {
377374
match self.inner {
378375
Normal(_, Some(ref upper)) => Some(upper),
379376
_ => None
@@ -406,15 +403,15 @@ impl<T: PartialOrd+Normalizable> Range<T> {
406403
}
407404
}
408405

409-
fn order<T:PartialOrd>(a: T, b: T) -> (T, T) {
406+
fn order<T>(a: T, b: T) -> (T, T) where T: PartialOrd {
410407
if a < b {
411408
(a, b)
412409
} else {
413410
(b, a)
414411
}
415412
}
416413

417-
impl<T: PartialOrd+Normalizable+Clone> Range<T> {
414+
impl<T> Range<T> where T: PartialOrd+Normalizable+Clone {
418415
/// Returns the intersection of this range with another
419416
pub fn intersect(&self, other: &Range<T>) -> Range<T> {
420417
if self.is_empty() || other.is_empty() {
@@ -455,8 +452,7 @@ impl<T: PartialOrd+Normalizable+Clone> Range<T> {
455452
if discontiguous {
456453
None
457454
} else {
458-
Some(Range::new(l_lower.map(|v| v.clone()),
459-
u_upper.map(|v| v.clone())))
455+
Some(Range::new(l_lower.map(|v| v.clone()), u_upper.map(|v| v.clone())))
460456
}
461457
}
462458
}

0 commit comments

Comments
 (0)