1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/// Trait to represent a succeedable type.
///
/// This trait is used to implement the `succ` function.
pub trait Succ {
  /// Method which returns the logical successor (i.e. `++`) of self.
  fn succ(self) -> Self;
}

impl Succ for char {
  fn succ(self) -> char {
    ((self as u8) + 1) as char
  }
}

macro_rules! impl_succ_int {
  ($x:ty) => {
    impl Succ for $x {
      fn succ(self) -> $x {
        self + 1
      }
    }
  };
}

macro_rules! impl_succ_float {
  ($x:ty) => {
    impl Succ for $x {
      fn succ(self) -> $x {
        self + 1.0
      }
    }
  };
}

impl_succ_int!(i8);
impl_succ_int!(i16);
impl_succ_int!(i32);
impl_succ_int!(i64);

impl_succ_int!(u8);
impl_succ_int!(u16);
impl_succ_int!(u32);
impl_succ_int!(u64);

impl_succ_int!(usize);
impl_succ_int!(isize);

impl_succ_float!(f32);
impl_succ_float!(f64);

/// Returns the successor of its argument.
///
/// # Example
/// ```
/// use succ::succ;
///
/// let five = 5;
/// assert_eq!(6, succ(five));
///
/// let a = 'a';
/// assert_eq!('b', succ(a));
/// ```
pub fn succ<T: Succ>(thing: T) -> T {
  thing.succ()
}

#[cfg(test)]
mod tests {
  use succ;

  #[test]
  fn ints() {
    assert!(succ(1) == 2);
  }

  #[test]
  fn uints() {
    assert!(succ(-10) == -9);
  }

  #[test]
  fn floats() {
    assert!(succ(2.0) == 3.0);
  }

  #[test]
  fn chars() {
    assert!(succ('a') == 'b');
  }
}