gcollections/ops/
bounded.rs

1// Copyright 2016 Pierre Talbot (IRCAM)
2
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use kind::*;
10use ops::constructor::*;
11use num_integer::Integer;
12
13pub trait Bounded: Collection
14{
15  fn lower(&self) -> Self::Item;
16  fn upper(&self) -> Self::Item;
17}
18
19pub trait ShrinkLeft: Bounded {
20  fn shrink_left(&self, lb: Self::Item) -> Self;
21}
22
23pub trait ShrinkRight: Bounded {
24  fn shrink_right(&self, ub: Self::Item) -> Self;
25}
26
27pub trait StrictShrinkLeft: Bounded {
28  fn strict_shrink_left(&self, lb: Self::Item) -> Self;
29}
30
31pub trait StrictShrinkRight: Bounded {
32  fn strict_shrink_right(&self, ub: Self::Item) -> Self;
33}
34
35macro_rules! strict_shrink_impl
36{
37  ( $( $keyword:tt ),*) =>
38  {
39    impl<B, R> StrictShrinkLeft for R where
40      R: ShrinkLeft + Empty + IntervalKind + Bounded<Item=B>,
41      B: Integer + num_traits::Bounded
42    {
43      $($keyword)* fn strict_shrink_left(&self, lb: B) -> R {
44        if lb == B::max_value() {
45          R::empty()
46        } else {
47          self.shrink_left(lb + B::one())
48        }
49      }
50    }
51    impl<B, R> StrictShrinkRight for R where
52      R: ShrinkRight + Empty + IntervalKind + Bounded<Item=B>,
53      B: Integer + num_traits::Bounded
54    {
55      $($keyword)* fn strict_shrink_right(&self, ub: B) -> R {
56        if ub == B::min_value() {
57          R::empty()
58        } else {
59          self.shrink_right(ub - B::one())
60        }
61      }
62    }
63  }
64}
65
66#[cfg(feature = "nightly")]
67strict_shrink_impl!(default);
68#[cfg(not(feature = "nightly"))]
69strict_shrink_impl!();