Echo Writes Code

traits.rs

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Utility traits and re-exports of traits from other modules.

#![forbid(unsafe_code)]

/// Conversion trait for primitive scalar types for use in generic contexts.
///
/// Note that this conversion is a no-questions-asked cast using the `as` operator; truncations etc.
/// are to be expected if you use this on arbitrary values. See [the documentation on numeric casts].
///
/// [the documentation on numeric casts]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#r-expr.as.numeric
pub trait FromScalar<T: Scalar> {
  fn from_scalar(scalar: T) -> Self;
}

// We have to do this with a macro, because `x as T` doesn't work in a generic context (since
// there's no way to prove to the compiler that T is a scalar type)
macro_rules! impl_from_scalar {
  ($T: ty : $($U: ty),*) => {
    $(impl FromScalar<$U> for $T {
      fn from_scalar(scalar: $U) -> $T {
        scalar as $T
      }
    })*
  }
}

impl_from_scalar!(u8 : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(u16 : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(u32 : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(u64 : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(usize : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(i8 : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(i16 : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(i32 : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(i64 : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(isize : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(f32 : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_from_scalar!(f64 : u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);

/// Marker trait for primitive scalar types.
pub trait Scalar : 'static
  + Copy + Clone + Default + PartialEq + PartialOrd + Sized
  + std::fmt::Debug
  + std::ops::Add<Output = Self> + std::ops::Sub<Output = Self>
  + std::ops::Mul<Output = Self> + std::ops::Div<Output = Self>
  + FromScalar<u8> + FromScalar<u16> + FromScalar<u32> + FromScalar<u64>
  + FromScalar<i8> + FromScalar<i16> + FromScalar<i32> + FromScalar<i64>
  + FromScalar<f32> + FromScalar<f64>
{}

impl Scalar for u8 {}
impl Scalar for u16 {}
impl Scalar for u32 {}
impl Scalar for u64 {}
impl Scalar for usize {}
impl Scalar for i8 {}
impl Scalar for i16 {}
impl Scalar for i32 {}
impl Scalar for i64 {}
impl Scalar for isize {}
impl Scalar for f32 {}
impl Scalar for f64 {}

/// Marker trait for primitive types which can be signed.
pub trait Signed : Scalar + std::ops::Neg<Output = Self> {}

impl Signed for i8 {}
impl Signed for i16 {}
impl Signed for i32 {}
impl Signed for i64 {}
impl Signed for isize {}
impl Signed for f32 {}
impl Signed for f64 {}

/// Marker trait for primitive fixed-point types.
pub trait Fixed : Scalar + Eq + Ord {}

impl Fixed for u8 {}
impl Fixed for u16 {}
impl Fixed for u32 {}
impl Fixed for u64 {}
impl Fixed for usize {}
impl Fixed for i8 {}
impl Fixed for i16 {}
impl Fixed for i32 {}
impl Fixed for i64 {}
impl Fixed for isize {}

/// Marker trait for primitive floating-point types.
///
/// Also includes dispatchers for the various native float methods.
pub trait Float : Scalar + Signed {
  fn acos(self) -> Self;

  fn cos(self) -> Self;

  fn sin(self) -> Self;

  fn sqrt(self) -> Self;
}

macro_rules! impl_float {
  ($T: ty) => {
    impl Float for $T {
      fn acos(self) -> $T {
        self.acos()
      }

      fn cos(self) -> $T {
        self.cos()
      }

      fn sin(self) -> $T {
        self.sin()
      }

      fn sqrt(self) -> $T {
        self.sqrt()
      }
    }
  }
}

impl_float!(f32);
impl_float!(f64);