Skip to main content

gcollections/ops/
cardinality.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 num_traits::{One, Zero, Unsigned};
10use num_integer::{Integer};
11
12pub trait Cardinality
13{
14  type Size: Unsigned + Integer;
15  fn size(&self) -> Self::Size;
16}
17
18pub trait IsSingleton
19{
20  fn is_singleton(&self) -> bool;
21}
22
23pub trait IsEmpty
24{
25  fn is_empty(&self) -> bool;
26}
27
28/// For an explanation on the macros, see `lib.rs`.
29
30macro_rules! is_singleton_impl
31{
32  ( $( $keyword:tt ),*) =>
33  {
34    impl<R> IsSingleton for R where
35     R: Cardinality
36    {
37      $($keyword)* fn is_singleton(&self) -> bool {
38        self.size() == <<Self as Cardinality>::Size as One>::one()
39      }
40    }
41  }
42}
43
44#[cfg(feature = "nightly")]
45is_singleton_impl!(default);
46#[cfg(not(feature = "nightly"))]
47is_singleton_impl!();
48
49macro_rules! is_empty_impl
50{
51  ( $( $keyword:tt ),*) =>
52  {
53    impl <R> IsEmpty for R where
54     R: Cardinality
55    {
56      $($keyword)* fn is_empty(&self) -> bool {
57        self.size().is_zero()
58      }
59    }
60  }
61}
62
63#[cfg(feature = "nightly")]
64is_empty_impl!(default);
65#[cfg(not(feature = "nightly"))]
66is_empty_impl!();