cast.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. use std::mem::size_of;
  2. use identities::Zero;
  3. use bounds::Bounded;
  4. /// A generic trait for converting a value to a number.
  5. pub trait ToPrimitive {
  6. /// Converts the value of `self` to an `isize`.
  7. #[inline]
  8. fn to_isize(&self) -> Option<isize> {
  9. self.to_i64().and_then(|x| x.to_isize())
  10. }
  11. /// Converts the value of `self` to an `i8`.
  12. #[inline]
  13. fn to_i8(&self) -> Option<i8> {
  14. self.to_i64().and_then(|x| x.to_i8())
  15. }
  16. /// Converts the value of `self` to an `i16`.
  17. #[inline]
  18. fn to_i16(&self) -> Option<i16> {
  19. self.to_i64().and_then(|x| x.to_i16())
  20. }
  21. /// Converts the value of `self` to an `i32`.
  22. #[inline]
  23. fn to_i32(&self) -> Option<i32> {
  24. self.to_i64().and_then(|x| x.to_i32())
  25. }
  26. /// Converts the value of `self` to an `i64`.
  27. fn to_i64(&self) -> Option<i64>;
  28. /// Converts the value of `self` to a `usize`.
  29. #[inline]
  30. fn to_usize(&self) -> Option<usize> {
  31. self.to_u64().and_then(|x| x.to_usize())
  32. }
  33. /// Converts the value of `self` to an `u8`.
  34. #[inline]
  35. fn to_u8(&self) -> Option<u8> {
  36. self.to_u64().and_then(|x| x.to_u8())
  37. }
  38. /// Converts the value of `self` to an `u16`.
  39. #[inline]
  40. fn to_u16(&self) -> Option<u16> {
  41. self.to_u64().and_then(|x| x.to_u16())
  42. }
  43. /// Converts the value of `self` to an `u32`.
  44. #[inline]
  45. fn to_u32(&self) -> Option<u32> {
  46. self.to_u64().and_then(|x| x.to_u32())
  47. }
  48. /// Converts the value of `self` to an `u64`.
  49. #[inline]
  50. fn to_u64(&self) -> Option<u64>;
  51. /// Converts the value of `self` to an `f32`.
  52. #[inline]
  53. fn to_f32(&self) -> Option<f32> {
  54. self.to_f64().and_then(|x| x.to_f32())
  55. }
  56. /// Converts the value of `self` to an `f64`.
  57. #[inline]
  58. fn to_f64(&self) -> Option<f64> {
  59. self.to_i64().and_then(|x| x.to_f64())
  60. }
  61. }
  62. macro_rules! impl_to_primitive_int_to_int {
  63. ($SrcT:ty, $DstT:ty, $slf:expr) => (
  64. {
  65. if size_of::<$SrcT>() <= size_of::<$DstT>() {
  66. Some($slf as $DstT)
  67. } else {
  68. let n = $slf as i64;
  69. let min_value: $DstT = Bounded::min_value();
  70. let max_value: $DstT = Bounded::max_value();
  71. if min_value as i64 <= n && n <= max_value as i64 {
  72. Some($slf as $DstT)
  73. } else {
  74. None
  75. }
  76. }
  77. }
  78. )
  79. }
  80. macro_rules! impl_to_primitive_int_to_uint {
  81. ($SrcT:ty, $DstT:ty, $slf:expr) => (
  82. {
  83. let zero: $SrcT = Zero::zero();
  84. let max_value: $DstT = Bounded::max_value();
  85. if zero <= $slf && $slf as u64 <= max_value as u64 {
  86. Some($slf as $DstT)
  87. } else {
  88. None
  89. }
  90. }
  91. )
  92. }
  93. macro_rules! impl_to_primitive_int {
  94. ($T:ty) => (
  95. impl ToPrimitive for $T {
  96. #[inline]
  97. fn to_isize(&self) -> Option<isize> { impl_to_primitive_int_to_int!($T, isize, *self) }
  98. #[inline]
  99. fn to_i8(&self) -> Option<i8> { impl_to_primitive_int_to_int!($T, i8, *self) }
  100. #[inline]
  101. fn to_i16(&self) -> Option<i16> { impl_to_primitive_int_to_int!($T, i16, *self) }
  102. #[inline]
  103. fn to_i32(&self) -> Option<i32> { impl_to_primitive_int_to_int!($T, i32, *self) }
  104. #[inline]
  105. fn to_i64(&self) -> Option<i64> { impl_to_primitive_int_to_int!($T, i64, *self) }
  106. #[inline]
  107. fn to_usize(&self) -> Option<usize> { impl_to_primitive_int_to_uint!($T, usize, *self) }
  108. #[inline]
  109. fn to_u8(&self) -> Option<u8> { impl_to_primitive_int_to_uint!($T, u8, *self) }
  110. #[inline]
  111. fn to_u16(&self) -> Option<u16> { impl_to_primitive_int_to_uint!($T, u16, *self) }
  112. #[inline]
  113. fn to_u32(&self) -> Option<u32> { impl_to_primitive_int_to_uint!($T, u32, *self) }
  114. #[inline]
  115. fn to_u64(&self) -> Option<u64> { impl_to_primitive_int_to_uint!($T, u64, *self) }
  116. #[inline]
  117. fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
  118. #[inline]
  119. fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
  120. }
  121. )
  122. }
  123. impl_to_primitive_int!(isize);
  124. impl_to_primitive_int!(i8);
  125. impl_to_primitive_int!(i16);
  126. impl_to_primitive_int!(i32);
  127. impl_to_primitive_int!(i64);
  128. macro_rules! impl_to_primitive_uint_to_int {
  129. ($DstT:ty, $slf:expr) => (
  130. {
  131. let max_value: $DstT = Bounded::max_value();
  132. if $slf as u64 <= max_value as u64 {
  133. Some($slf as $DstT)
  134. } else {
  135. None
  136. }
  137. }
  138. )
  139. }
  140. macro_rules! impl_to_primitive_uint_to_uint {
  141. ($SrcT:ty, $DstT:ty, $slf:expr) => (
  142. {
  143. if size_of::<$SrcT>() <= size_of::<$DstT>() {
  144. Some($slf as $DstT)
  145. } else {
  146. let zero: $SrcT = Zero::zero();
  147. let max_value: $DstT = Bounded::max_value();
  148. if zero <= $slf && $slf as u64 <= max_value as u64 {
  149. Some($slf as $DstT)
  150. } else {
  151. None
  152. }
  153. }
  154. }
  155. )
  156. }
  157. macro_rules! impl_to_primitive_uint {
  158. ($T:ty) => (
  159. impl ToPrimitive for $T {
  160. #[inline]
  161. fn to_isize(&self) -> Option<isize> { impl_to_primitive_uint_to_int!(isize, *self) }
  162. #[inline]
  163. fn to_i8(&self) -> Option<i8> { impl_to_primitive_uint_to_int!(i8, *self) }
  164. #[inline]
  165. fn to_i16(&self) -> Option<i16> { impl_to_primitive_uint_to_int!(i16, *self) }
  166. #[inline]
  167. fn to_i32(&self) -> Option<i32> { impl_to_primitive_uint_to_int!(i32, *self) }
  168. #[inline]
  169. fn to_i64(&self) -> Option<i64> { impl_to_primitive_uint_to_int!(i64, *self) }
  170. #[inline]
  171. fn to_usize(&self) -> Option<usize> {
  172. impl_to_primitive_uint_to_uint!($T, usize, *self)
  173. }
  174. #[inline]
  175. fn to_u8(&self) -> Option<u8> { impl_to_primitive_uint_to_uint!($T, u8, *self) }
  176. #[inline]
  177. fn to_u16(&self) -> Option<u16> { impl_to_primitive_uint_to_uint!($T, u16, *self) }
  178. #[inline]
  179. fn to_u32(&self) -> Option<u32> { impl_to_primitive_uint_to_uint!($T, u32, *self) }
  180. #[inline]
  181. fn to_u64(&self) -> Option<u64> { impl_to_primitive_uint_to_uint!($T, u64, *self) }
  182. #[inline]
  183. fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
  184. #[inline]
  185. fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
  186. }
  187. )
  188. }
  189. impl_to_primitive_uint!(usize);
  190. impl_to_primitive_uint!(u8);
  191. impl_to_primitive_uint!(u16);
  192. impl_to_primitive_uint!(u32);
  193. impl_to_primitive_uint!(u64);
  194. macro_rules! impl_to_primitive_float_to_float {
  195. ($SrcT:ident, $DstT:ident, $slf:expr) => (
  196. if size_of::<$SrcT>() <= size_of::<$DstT>() {
  197. Some($slf as $DstT)
  198. } else {
  199. let n = $slf as f64;
  200. let max_value: $SrcT = ::std::$SrcT::MAX;
  201. if -max_value as f64 <= n && n <= max_value as f64 {
  202. Some($slf as $DstT)
  203. } else {
  204. None
  205. }
  206. }
  207. )
  208. }
  209. macro_rules! impl_to_primitive_float {
  210. ($T:ident) => (
  211. impl ToPrimitive for $T {
  212. #[inline]
  213. fn to_isize(&self) -> Option<isize> { Some(*self as isize) }
  214. #[inline]
  215. fn to_i8(&self) -> Option<i8> { Some(*self as i8) }
  216. #[inline]
  217. fn to_i16(&self) -> Option<i16> { Some(*self as i16) }
  218. #[inline]
  219. fn to_i32(&self) -> Option<i32> { Some(*self as i32) }
  220. #[inline]
  221. fn to_i64(&self) -> Option<i64> { Some(*self as i64) }
  222. #[inline]
  223. fn to_usize(&self) -> Option<usize> { Some(*self as usize) }
  224. #[inline]
  225. fn to_u8(&self) -> Option<u8> { Some(*self as u8) }
  226. #[inline]
  227. fn to_u16(&self) -> Option<u16> { Some(*self as u16) }
  228. #[inline]
  229. fn to_u32(&self) -> Option<u32> { Some(*self as u32) }
  230. #[inline]
  231. fn to_u64(&self) -> Option<u64> { Some(*self as u64) }
  232. #[inline]
  233. fn to_f32(&self) -> Option<f32> { impl_to_primitive_float_to_float!($T, f32, *self) }
  234. #[inline]
  235. fn to_f64(&self) -> Option<f64> { impl_to_primitive_float_to_float!($T, f64, *self) }
  236. }
  237. )
  238. }
  239. impl_to_primitive_float!(f32);
  240. impl_to_primitive_float!(f64);
  241. /// A generic trait for converting a number to a value.
  242. pub trait FromPrimitive: Sized {
  243. /// Convert an `isize` to return an optional value of this type. If the
  244. /// value cannot be represented by this value, the `None` is returned.
  245. #[inline]
  246. fn from_isize(n: isize) -> Option<Self> {
  247. FromPrimitive::from_i64(n as i64)
  248. }
  249. /// Convert an `i8` to return an optional value of this type. If the
  250. /// type cannot be represented by this value, the `None` is returned.
  251. #[inline]
  252. fn from_i8(n: i8) -> Option<Self> {
  253. FromPrimitive::from_i64(n as i64)
  254. }
  255. /// Convert an `i16` to return an optional value of this type. If the
  256. /// type cannot be represented by this value, the `None` is returned.
  257. #[inline]
  258. fn from_i16(n: i16) -> Option<Self> {
  259. FromPrimitive::from_i64(n as i64)
  260. }
  261. /// Convert an `i32` to return an optional value of this type. If the
  262. /// type cannot be represented by this value, the `None` is returned.
  263. #[inline]
  264. fn from_i32(n: i32) -> Option<Self> {
  265. FromPrimitive::from_i64(n as i64)
  266. }
  267. /// Convert an `i64` to return an optional value of this type. If the
  268. /// type cannot be represented by this value, the `None` is returned.
  269. fn from_i64(n: i64) -> Option<Self>;
  270. /// Convert a `usize` to return an optional value of this type. If the
  271. /// type cannot be represented by this value, the `None` is returned.
  272. #[inline]
  273. fn from_usize(n: usize) -> Option<Self> {
  274. FromPrimitive::from_u64(n as u64)
  275. }
  276. /// Convert an `u8` to return an optional value of this type. If the
  277. /// type cannot be represented by this value, the `None` is returned.
  278. #[inline]
  279. fn from_u8(n: u8) -> Option<Self> {
  280. FromPrimitive::from_u64(n as u64)
  281. }
  282. /// Convert an `u16` to return an optional value of this type. If the
  283. /// type cannot be represented by this value, the `None` is returned.
  284. #[inline]
  285. fn from_u16(n: u16) -> Option<Self> {
  286. FromPrimitive::from_u64(n as u64)
  287. }
  288. /// Convert an `u32` to return an optional value of this type. If the
  289. /// type cannot be represented by this value, the `None` is returned.
  290. #[inline]
  291. fn from_u32(n: u32) -> Option<Self> {
  292. FromPrimitive::from_u64(n as u64)
  293. }
  294. /// Convert an `u64` to return an optional value of this type. If the
  295. /// type cannot be represented by this value, the `None` is returned.
  296. fn from_u64(n: u64) -> Option<Self>;
  297. /// Convert a `f32` to return an optional value of this type. If the
  298. /// type cannot be represented by this value, the `None` is returned.
  299. #[inline]
  300. fn from_f32(n: f32) -> Option<Self> {
  301. FromPrimitive::from_f64(n as f64)
  302. }
  303. /// Convert a `f64` to return an optional value of this type. If the
  304. /// type cannot be represented by this value, the `None` is returned.
  305. #[inline]
  306. fn from_f64(n: f64) -> Option<Self> {
  307. FromPrimitive::from_i64(n as i64)
  308. }
  309. }
  310. macro_rules! impl_from_primitive {
  311. ($T:ty, $to_ty:ident) => (
  312. #[allow(deprecated)]
  313. impl FromPrimitive for $T {
  314. #[inline] fn from_i8(n: i8) -> Option<$T> { n.$to_ty() }
  315. #[inline] fn from_i16(n: i16) -> Option<$T> { n.$to_ty() }
  316. #[inline] fn from_i32(n: i32) -> Option<$T> { n.$to_ty() }
  317. #[inline] fn from_i64(n: i64) -> Option<$T> { n.$to_ty() }
  318. #[inline] fn from_u8(n: u8) -> Option<$T> { n.$to_ty() }
  319. #[inline] fn from_u16(n: u16) -> Option<$T> { n.$to_ty() }
  320. #[inline] fn from_u32(n: u32) -> Option<$T> { n.$to_ty() }
  321. #[inline] fn from_u64(n: u64) -> Option<$T> { n.$to_ty() }
  322. #[inline] fn from_f32(n: f32) -> Option<$T> { n.$to_ty() }
  323. #[inline] fn from_f64(n: f64) -> Option<$T> { n.$to_ty() }
  324. }
  325. )
  326. }
  327. impl_from_primitive!(isize, to_isize);
  328. impl_from_primitive!(i8, to_i8);
  329. impl_from_primitive!(i16, to_i16);
  330. impl_from_primitive!(i32, to_i32);
  331. impl_from_primitive!(i64, to_i64);
  332. impl_from_primitive!(usize, to_usize);
  333. impl_from_primitive!(u8, to_u8);
  334. impl_from_primitive!(u16, to_u16);
  335. impl_from_primitive!(u32, to_u32);
  336. impl_from_primitive!(u64, to_u64);
  337. impl_from_primitive!(f32, to_f32);
  338. impl_from_primitive!(f64, to_f64);
  339. /// Cast from one machine scalar to another.
  340. ///
  341. /// # Examples
  342. ///
  343. /// ```
  344. /// # use num_traits as num;
  345. /// let twenty: f32 = num::cast(0x14).unwrap();
  346. /// assert_eq!(twenty, 20f32);
  347. /// ```
  348. ///
  349. #[inline]
  350. pub fn cast<T: NumCast, U: NumCast>(n: T) -> Option<U> {
  351. NumCast::from(n)
  352. }
  353. /// An interface for casting between machine scalars.
  354. pub trait NumCast: Sized + ToPrimitive {
  355. /// Creates a number from another value that can be converted into
  356. /// a primitive via the `ToPrimitive` trait.
  357. fn from<T: ToPrimitive>(n: T) -> Option<Self>;
  358. }
  359. macro_rules! impl_num_cast {
  360. ($T:ty, $conv:ident) => (
  361. impl NumCast for $T {
  362. #[inline]
  363. #[allow(deprecated)]
  364. fn from<N: ToPrimitive>(n: N) -> Option<$T> {
  365. // `$conv` could be generated using `concat_idents!`, but that
  366. // macro seems to be broken at the moment
  367. n.$conv()
  368. }
  369. }
  370. )
  371. }
  372. impl_num_cast!(u8, to_u8);
  373. impl_num_cast!(u16, to_u16);
  374. impl_num_cast!(u32, to_u32);
  375. impl_num_cast!(u64, to_u64);
  376. impl_num_cast!(usize, to_usize);
  377. impl_num_cast!(i8, to_i8);
  378. impl_num_cast!(i16, to_i16);
  379. impl_num_cast!(i32, to_i32);
  380. impl_num_cast!(i64, to_i64);
  381. impl_num_cast!(isize, to_isize);
  382. impl_num_cast!(f32, to_f32);
  383. impl_num_cast!(f64, to_f64);