math_ops/
conversion.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
126
127
128
129
130
131
//! Module for type conversions between integers and floating-point types,
//! and between `f32` and `f64`.

/// Trait to convert integer types to floating-point types.
pub trait IntToFloat {
  fn to_f32(&self) -> f32;
  fn to_f64(&self) -> f64;
}

/// Trait to convert floating-point types to integer types.
pub trait FloatToInt {
  fn to_i8(&self) -> i8;
  fn to_i16(&self) -> i16;
  fn to_i32(&self) -> i32;
  fn to_i64(&self) -> i64;
  fn to_u8(&self) -> u8;
  fn to_u16(&self) -> u16;
  fn to_u32(&self) -> u32;
  fn to_u64(&self) -> u64;
}

/// Implement `IntToFloat` for all integer types.
macro_rules! impl_int_to_float {
  ($($t:ty)*) => ($(
    impl IntToFloat for $t {
      fn to_f32(&self) -> f32 {
        *self as f32
      }
      fn to_f64(&self) -> f64 {
        *self as f64
      }
    }
  )*)
}

impl_int_to_float!(i8 i16 i32 i64 isize u8 u16 u32 u64 usize);

/// Implement `FloatToInt` for `f32` and `f64`.
impl FloatToInt for f32 {
  fn to_i8(&self) -> i8 {
    *self as i8
  }

  fn to_i16(&self) -> i16 {
    *self as i16
  }

  fn to_i32(&self) -> i32 {
    *self as i32
  }

  fn to_i64(&self) -> i64 {
    *self as i64
  }

  fn to_u8(&self) -> u8 {
    *self as u8
  }

  fn to_u16(&self) -> u16 {
    *self as u16
  }

  fn to_u32(&self) -> u32 {
    *self as u32
  }

  fn to_u64(&self) -> u64 {
    *self as u64
  }
}

impl FloatToInt for f64 {
  fn to_i8(&self) -> i8 {
    *self as i8
  }

  fn to_i16(&self) -> i16 {
    *self as i16
  }

  fn to_i32(&self) -> i32 {
    *self as i32
  }

  fn to_i64(&self) -> i64 {
    *self as i64
  }

  fn to_u8(&self) -> u8 {
    *self as u8
  }

  fn to_u16(&self) -> u16 {
    *self as u16
  }

  fn to_u32(&self) -> u32 {
    *self as u32
  }

  fn to_u64(&self) -> u64 {
    *self as u64
  }
}

/// Trait to convert between `f32` and `f64`.
pub trait FloatCast {
  fn to_f32_cast(&self) -> f32;
  fn to_f64_cast(&self) -> f64;
}

impl FloatCast for f32 {
  fn to_f32_cast(&self) -> f32 {
    *self
  }

  fn to_f64_cast(&self) -> f64 {
    *self as f64
  }
}

impl FloatCast for f64 {
  fn to_f32_cast(&self) -> f32 {
    *self as f32
  }

  fn to_f64_cast(&self) -> f64 {
    *self
  }
}