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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
//! Cast module //Copied from https://github.com/rust-num/num-traits/blob/master/src/cast.rs /// A generic trait for converting a value to a number. pub trait ToPrimitive { /// Converts the value of `self` to an `isize`. fn to_isize(&self) -> Option<isize> { self.to_i64().and_then(|x| x.to_isize()) } /// Converts the value of `self` to an `i8`. fn to_i8(&self) -> Option<i8> { self.to_i64().and_then(|x| x.to_i8()) } /// Converts the value of `self` to an `i16`. fn to_i16(&self) -> Option<i16> { self.to_i64().and_then(|x| x.to_i16()) } /// Converts the value of `self` to an `i32`. fn to_i32(&self) -> Option<i32> { self.to_i64().and_then(|x| x.to_i32()) } /// Converts the value of `self` to an `i64`. fn to_i64(&self) -> Option<i64>; /// Converts the value of `self` to a `usize`. fn to_usize(&self) -> Option<usize> { self.to_u64().and_then(|x| x.to_usize()) } /// Converts the value of `self` to an `u8`. fn to_u8(&self) -> Option<u8> { self.to_u64().and_then(|x| x.to_u8()) } /// Converts the value of `self` to an `u16`. fn to_u16(&self) -> Option<u16> { self.to_u64().and_then(|x| x.to_u16()) } /// Converts the value of `self` to an `u32`. fn to_u32(&self) -> Option<u32> { self.to_u64().and_then(|x : u64 | x.to_u32()) } /// Converts the value of `self` to an `u64`. fn to_u64(&self) -> Option<u64>; /// Converts the value of `self` to an `f32`. fn to_f32(&self) -> Option<f32> { self.to_f64().and_then(|x| x.to_f32()) } /// Converts the value of `self` to an `f64`. fn to_f64(&self) -> Option<f64>; // { // self.to_f64().and_then(|x: f64| x.to_f64()) // } } /// A generic trait for converting a number to a value. pub trait FromPrimitive: Sized { /// Convert an `isize` to return an optional value of this type. If the /// value cannot be represented by this value, the `None` is returned. fn from_isize(n: isize) -> Option<Self> { FromPrimitive::from_i64(n as i64) } /// Convert an `i8` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_i8(n: i8) -> Option<Self> { FromPrimitive::from_i64(n as i64) } /// Convert an `i16` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_i16(n: i16) -> Option<Self> { FromPrimitive::from_i64(n as i64) } /// Convert an `i32` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_i32(n: i32) -> Option<Self> { FromPrimitive::from_i64(n as i64) } /// Convert an `i64` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_i64(n: i64) -> Option<Self>; /// Convert a `usize` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_usize(n: usize) -> Option<Self> { FromPrimitive::from_u64(n as u64) } /// Convert an `u8` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_u8(n: u8) -> Option<Self> { FromPrimitive::from_u64(n as u64) } /// Convert an `u16` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_u16(n: u16) -> Option<Self> { FromPrimitive::from_u64(n as u64) } /// Convert an `u32` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_u32(n: u32) -> Option<Self> { FromPrimitive::from_u64(n as u64) } /// Convert an `u64` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_u64(n: u64) -> Option<Self>; /// Convert a `f32` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_f32(n: f32) -> Option<Self> { FromPrimitive::from_f64(n as f64) } /// Convert a `f64` to return an optional value of this type. If the /// type cannot be represented by this value, the `None` is returned. fn from_f64(n: f64) -> Option<Self>; // { // FromPrimitive::from_f64(n as f64) // } } /// Cast from one machine scalar to another. /// pub fn cast<T: NumCast, U: NumCast>(n: T) -> Option<U> { NumCast::from(n) } /// An interface for casting between machine scalars. pub trait NumCast: Sized + ToPrimitive { /// Creates a number from another value that can be converted into /// a primitive via the `ToPrimitive` trait. fn from<T: ToPrimitive>(n: T) -> Option<Self>; } pub trait AsPrimitive<T>: 'static + Copy where T: 'static + Copy, { /// Convert a value to another, using the `as` operator. fn as_(self) -> T; }