1 // Copyright 2017 Serde Developers 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 9 use lib::*; 10 11 macro_rules! int_to_int { 12 ($dst:ident, $n:ident) => { 13 if $dst::min_value() as i64 <= $n as i64 && $n as i64 <= $dst::max_value() as i64 { 14 Some($n as $dst) 15 } else { 16 None 17 } 18 }; 19 } 20 21 macro_rules! int_to_uint { 22 ($dst:ident, $n:ident) => { 23 if 0 <= $n && $n as u64 <= $dst::max_value() as u64 { 24 Some($n as $dst) 25 } else { 26 None 27 } 28 }; 29 } 30 31 macro_rules! uint_to { 32 ($dst:ident, $n:ident) => { 33 if $n as u64 <= $dst::max_value() as u64 { 34 Some($n as $dst) 35 } else { 36 None 37 } 38 }; 39 } 40 41 pub trait FromPrimitive: Sized { from_isize(n: isize) -> Option<Self>42 fn from_isize(n: isize) -> Option<Self>; from_i8(n: i8) -> Option<Self>43 fn from_i8(n: i8) -> Option<Self>; from_i16(n: i16) -> Option<Self>44 fn from_i16(n: i16) -> Option<Self>; from_i32(n: i32) -> Option<Self>45 fn from_i32(n: i32) -> Option<Self>; from_i64(n: i64) -> Option<Self>46 fn from_i64(n: i64) -> Option<Self>; from_usize(n: usize) -> Option<Self>47 fn from_usize(n: usize) -> Option<Self>; from_u8(n: u8) -> Option<Self>48 fn from_u8(n: u8) -> Option<Self>; from_u16(n: u16) -> Option<Self>49 fn from_u16(n: u16) -> Option<Self>; from_u32(n: u32) -> Option<Self>50 fn from_u32(n: u32) -> Option<Self>; from_u64(n: u64) -> Option<Self>51 fn from_u64(n: u64) -> Option<Self>; 52 } 53 54 macro_rules! impl_from_primitive_for_int { 55 ($t:ident) => { 56 impl FromPrimitive for $t { 57 #[inline] 58 fn from_isize(n: isize) -> Option<Self> { 59 int_to_int!($t, n) 60 } 61 #[inline] 62 fn from_i8(n: i8) -> Option<Self> { 63 int_to_int!($t, n) 64 } 65 #[inline] 66 fn from_i16(n: i16) -> Option<Self> { 67 int_to_int!($t, n) 68 } 69 #[inline] 70 fn from_i32(n: i32) -> Option<Self> { 71 int_to_int!($t, n) 72 } 73 #[inline] 74 fn from_i64(n: i64) -> Option<Self> { 75 int_to_int!($t, n) 76 } 77 #[inline] 78 fn from_usize(n: usize) -> Option<Self> { 79 uint_to!($t, n) 80 } 81 #[inline] 82 fn from_u8(n: u8) -> Option<Self> { 83 uint_to!($t, n) 84 } 85 #[inline] 86 fn from_u16(n: u16) -> Option<Self> { 87 uint_to!($t, n) 88 } 89 #[inline] 90 fn from_u32(n: u32) -> Option<Self> { 91 uint_to!($t, n) 92 } 93 #[inline] 94 fn from_u64(n: u64) -> Option<Self> { 95 uint_to!($t, n) 96 } 97 } 98 }; 99 } 100 101 macro_rules! impl_from_primitive_for_uint { 102 ($t:ident) => { 103 impl FromPrimitive for $t { 104 #[inline] 105 fn from_isize(n: isize) -> Option<Self> { 106 int_to_uint!($t, n) 107 } 108 #[inline] 109 fn from_i8(n: i8) -> Option<Self> { 110 int_to_uint!($t, n) 111 } 112 #[inline] 113 fn from_i16(n: i16) -> Option<Self> { 114 int_to_uint!($t, n) 115 } 116 #[inline] 117 fn from_i32(n: i32) -> Option<Self> { 118 int_to_uint!($t, n) 119 } 120 #[inline] 121 fn from_i64(n: i64) -> Option<Self> { 122 int_to_uint!($t, n) 123 } 124 #[inline] 125 fn from_usize(n: usize) -> Option<Self> { 126 uint_to!($t, n) 127 } 128 #[inline] 129 fn from_u8(n: u8) -> Option<Self> { 130 uint_to!($t, n) 131 } 132 #[inline] 133 fn from_u16(n: u16) -> Option<Self> { 134 uint_to!($t, n) 135 } 136 #[inline] 137 fn from_u32(n: u32) -> Option<Self> { 138 uint_to!($t, n) 139 } 140 #[inline] 141 fn from_u64(n: u64) -> Option<Self> { 142 uint_to!($t, n) 143 } 144 } 145 }; 146 } 147 148 macro_rules! impl_from_primitive_for_float { 149 ($t:ident) => { 150 impl FromPrimitive for $t { 151 #[inline] 152 fn from_isize(n: isize) -> Option<Self> { 153 Some(n as Self) 154 } 155 #[inline] 156 fn from_i8(n: i8) -> Option<Self> { 157 Some(n as Self) 158 } 159 #[inline] 160 fn from_i16(n: i16) -> Option<Self> { 161 Some(n as Self) 162 } 163 #[inline] 164 fn from_i32(n: i32) -> Option<Self> { 165 Some(n as Self) 166 } 167 #[inline] 168 fn from_i64(n: i64) -> Option<Self> { 169 Some(n as Self) 170 } 171 #[inline] 172 fn from_usize(n: usize) -> Option<Self> { 173 Some(n as Self) 174 } 175 #[inline] 176 fn from_u8(n: u8) -> Option<Self> { 177 Some(n as Self) 178 } 179 #[inline] 180 fn from_u16(n: u16) -> Option<Self> { 181 Some(n as Self) 182 } 183 #[inline] 184 fn from_u32(n: u32) -> Option<Self> { 185 Some(n as Self) 186 } 187 #[inline] 188 fn from_u64(n: u64) -> Option<Self> { 189 Some(n as Self) 190 } 191 } 192 }; 193 } 194 195 impl_from_primitive_for_int!(isize); 196 impl_from_primitive_for_int!(i8); 197 impl_from_primitive_for_int!(i16); 198 impl_from_primitive_for_int!(i32); 199 impl_from_primitive_for_int!(i64); 200 impl_from_primitive_for_uint!(usize); 201 impl_from_primitive_for_uint!(u8); 202 impl_from_primitive_for_uint!(u16); 203 impl_from_primitive_for_uint!(u32); 204 impl_from_primitive_for_uint!(u64); 205 impl_from_primitive_for_float!(f32); 206 impl_from_primitive_for_float!(f64); 207