123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- use std::mem::size_of;
- use identities::Zero;
- use bounds::Bounded;
- /// A generic trait for converting a value to a number.
- pub trait ToPrimitive {
- /// Converts the value of `self` to an `isize`.
- #[inline]
- fn to_isize(&self) -> Option<isize> {
- self.to_i64().and_then(|x| x.to_isize())
- }
- /// Converts the value of `self` to an `i8`.
- #[inline]
- fn to_i8(&self) -> Option<i8> {
- self.to_i64().and_then(|x| x.to_i8())
- }
- /// Converts the value of `self` to an `i16`.
- #[inline]
- fn to_i16(&self) -> Option<i16> {
- self.to_i64().and_then(|x| x.to_i16())
- }
- /// Converts the value of `self` to an `i32`.
- #[inline]
- 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`.
- #[inline]
- fn to_usize(&self) -> Option<usize> {
- self.to_u64().and_then(|x| x.to_usize())
- }
- /// Converts the value of `self` to an `u8`.
- #[inline]
- fn to_u8(&self) -> Option<u8> {
- self.to_u64().and_then(|x| x.to_u8())
- }
- /// Converts the value of `self` to an `u16`.
- #[inline]
- fn to_u16(&self) -> Option<u16> {
- self.to_u64().and_then(|x| x.to_u16())
- }
- /// Converts the value of `self` to an `u32`.
- #[inline]
- fn to_u32(&self) -> Option<u32> {
- self.to_u64().and_then(|x| x.to_u32())
- }
- /// Converts the value of `self` to an `u64`.
- #[inline]
- fn to_u64(&self) -> Option<u64>;
- /// Converts the value of `self` to an `f32`.
- #[inline]
- fn to_f32(&self) -> Option<f32> {
- self.to_f64().and_then(|x| x.to_f32())
- }
- /// Converts the value of `self` to an `f64`.
- #[inline]
- fn to_f64(&self) -> Option<f64> {
- self.to_i64().and_then(|x| x.to_f64())
- }
- }
- macro_rules! impl_to_primitive_int_to_int {
- ($SrcT:ty, $DstT:ty, $slf:expr) => (
- {
- if size_of::<$SrcT>() <= size_of::<$DstT>() {
- Some($slf as $DstT)
- } else {
- let n = $slf as i64;
- let min_value: $DstT = Bounded::min_value();
- let max_value: $DstT = Bounded::max_value();
- if min_value as i64 <= n && n <= max_value as i64 {
- Some($slf as $DstT)
- } else {
- None
- }
- }
- }
- )
- }
- macro_rules! impl_to_primitive_int_to_uint {
- ($SrcT:ty, $DstT:ty, $slf:expr) => (
- {
- let zero: $SrcT = Zero::zero();
- let max_value: $DstT = Bounded::max_value();
- if zero <= $slf && $slf as u64 <= max_value as u64 {
- Some($slf as $DstT)
- } else {
- None
- }
- }
- )
- }
- macro_rules! impl_to_primitive_int {
- ($T:ty) => (
- impl ToPrimitive for $T {
- #[inline]
- fn to_isize(&self) -> Option<isize> { impl_to_primitive_int_to_int!($T, isize, *self) }
- #[inline]
- fn to_i8(&self) -> Option<i8> { impl_to_primitive_int_to_int!($T, i8, *self) }
- #[inline]
- fn to_i16(&self) -> Option<i16> { impl_to_primitive_int_to_int!($T, i16, *self) }
- #[inline]
- fn to_i32(&self) -> Option<i32> { impl_to_primitive_int_to_int!($T, i32, *self) }
- #[inline]
- fn to_i64(&self) -> Option<i64> { impl_to_primitive_int_to_int!($T, i64, *self) }
- #[inline]
- fn to_usize(&self) -> Option<usize> { impl_to_primitive_int_to_uint!($T, usize, *self) }
- #[inline]
- fn to_u8(&self) -> Option<u8> { impl_to_primitive_int_to_uint!($T, u8, *self) }
- #[inline]
- fn to_u16(&self) -> Option<u16> { impl_to_primitive_int_to_uint!($T, u16, *self) }
- #[inline]
- fn to_u32(&self) -> Option<u32> { impl_to_primitive_int_to_uint!($T, u32, *self) }
- #[inline]
- fn to_u64(&self) -> Option<u64> { impl_to_primitive_int_to_uint!($T, u64, *self) }
- #[inline]
- fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
- #[inline]
- fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
- }
- )
- }
- impl_to_primitive_int!(isize);
- impl_to_primitive_int!(i8);
- impl_to_primitive_int!(i16);
- impl_to_primitive_int!(i32);
- impl_to_primitive_int!(i64);
- macro_rules! impl_to_primitive_uint_to_int {
- ($DstT:ty, $slf:expr) => (
- {
- let max_value: $DstT = Bounded::max_value();
- if $slf as u64 <= max_value as u64 {
- Some($slf as $DstT)
- } else {
- None
- }
- }
- )
- }
- macro_rules! impl_to_primitive_uint_to_uint {
- ($SrcT:ty, $DstT:ty, $slf:expr) => (
- {
- if size_of::<$SrcT>() <= size_of::<$DstT>() {
- Some($slf as $DstT)
- } else {
- let zero: $SrcT = Zero::zero();
- let max_value: $DstT = Bounded::max_value();
- if zero <= $slf && $slf as u64 <= max_value as u64 {
- Some($slf as $DstT)
- } else {
- None
- }
- }
- }
- )
- }
- macro_rules! impl_to_primitive_uint {
- ($T:ty) => (
- impl ToPrimitive for $T {
- #[inline]
- fn to_isize(&self) -> Option<isize> { impl_to_primitive_uint_to_int!(isize, *self) }
- #[inline]
- fn to_i8(&self) -> Option<i8> { impl_to_primitive_uint_to_int!(i8, *self) }
- #[inline]
- fn to_i16(&self) -> Option<i16> { impl_to_primitive_uint_to_int!(i16, *self) }
- #[inline]
- fn to_i32(&self) -> Option<i32> { impl_to_primitive_uint_to_int!(i32, *self) }
- #[inline]
- fn to_i64(&self) -> Option<i64> { impl_to_primitive_uint_to_int!(i64, *self) }
- #[inline]
- fn to_usize(&self) -> Option<usize> {
- impl_to_primitive_uint_to_uint!($T, usize, *self)
- }
- #[inline]
- fn to_u8(&self) -> Option<u8> { impl_to_primitive_uint_to_uint!($T, u8, *self) }
- #[inline]
- fn to_u16(&self) -> Option<u16> { impl_to_primitive_uint_to_uint!($T, u16, *self) }
- #[inline]
- fn to_u32(&self) -> Option<u32> { impl_to_primitive_uint_to_uint!($T, u32, *self) }
- #[inline]
- fn to_u64(&self) -> Option<u64> { impl_to_primitive_uint_to_uint!($T, u64, *self) }
- #[inline]
- fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
- #[inline]
- fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
- }
- )
- }
- impl_to_primitive_uint!(usize);
- impl_to_primitive_uint!(u8);
- impl_to_primitive_uint!(u16);
- impl_to_primitive_uint!(u32);
- impl_to_primitive_uint!(u64);
- macro_rules! impl_to_primitive_float_to_float {
- ($SrcT:ident, $DstT:ident, $slf:expr) => (
- if size_of::<$SrcT>() <= size_of::<$DstT>() {
- Some($slf as $DstT)
- } else {
- let n = $slf as f64;
- let max_value: $SrcT = ::std::$SrcT::MAX;
- if -max_value as f64 <= n && n <= max_value as f64 {
- Some($slf as $DstT)
- } else {
- None
- }
- }
- )
- }
- macro_rules! impl_to_primitive_float {
- ($T:ident) => (
- impl ToPrimitive for $T {
- #[inline]
- fn to_isize(&self) -> Option<isize> { Some(*self as isize) }
- #[inline]
- fn to_i8(&self) -> Option<i8> { Some(*self as i8) }
- #[inline]
- fn to_i16(&self) -> Option<i16> { Some(*self as i16) }
- #[inline]
- fn to_i32(&self) -> Option<i32> { Some(*self as i32) }
- #[inline]
- fn to_i64(&self) -> Option<i64> { Some(*self as i64) }
- #[inline]
- fn to_usize(&self) -> Option<usize> { Some(*self as usize) }
- #[inline]
- fn to_u8(&self) -> Option<u8> { Some(*self as u8) }
- #[inline]
- fn to_u16(&self) -> Option<u16> { Some(*self as u16) }
- #[inline]
- fn to_u32(&self) -> Option<u32> { Some(*self as u32) }
- #[inline]
- fn to_u64(&self) -> Option<u64> { Some(*self as u64) }
- #[inline]
- fn to_f32(&self) -> Option<f32> { impl_to_primitive_float_to_float!($T, f32, *self) }
- #[inline]
- fn to_f64(&self) -> Option<f64> { impl_to_primitive_float_to_float!($T, f64, *self) }
- }
- )
- }
- impl_to_primitive_float!(f32);
- impl_to_primitive_float!(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.
- #[inline]
- 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.
- #[inline]
- 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.
- #[inline]
- 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.
- #[inline]
- 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.
- #[inline]
- 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.
- #[inline]
- 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.
- #[inline]
- 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.
- #[inline]
- 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.
- #[inline]
- 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.
- #[inline]
- fn from_f64(n: f64) -> Option<Self> {
- FromPrimitive::from_i64(n as i64)
- }
- }
- macro_rules! impl_from_primitive {
- ($T:ty, $to_ty:ident) => (
- #[allow(deprecated)]
- impl FromPrimitive for $T {
- #[inline] fn from_i8(n: i8) -> Option<$T> { n.$to_ty() }
- #[inline] fn from_i16(n: i16) -> Option<$T> { n.$to_ty() }
- #[inline] fn from_i32(n: i32) -> Option<$T> { n.$to_ty() }
- #[inline] fn from_i64(n: i64) -> Option<$T> { n.$to_ty() }
- #[inline] fn from_u8(n: u8) -> Option<$T> { n.$to_ty() }
- #[inline] fn from_u16(n: u16) -> Option<$T> { n.$to_ty() }
- #[inline] fn from_u32(n: u32) -> Option<$T> { n.$to_ty() }
- #[inline] fn from_u64(n: u64) -> Option<$T> { n.$to_ty() }
- #[inline] fn from_f32(n: f32) -> Option<$T> { n.$to_ty() }
- #[inline] fn from_f64(n: f64) -> Option<$T> { n.$to_ty() }
- }
- )
- }
- impl_from_primitive!(isize, to_isize);
- impl_from_primitive!(i8, to_i8);
- impl_from_primitive!(i16, to_i16);
- impl_from_primitive!(i32, to_i32);
- impl_from_primitive!(i64, to_i64);
- impl_from_primitive!(usize, to_usize);
- impl_from_primitive!(u8, to_u8);
- impl_from_primitive!(u16, to_u16);
- impl_from_primitive!(u32, to_u32);
- impl_from_primitive!(u64, to_u64);
- impl_from_primitive!(f32, to_f32);
- impl_from_primitive!(f64, to_f64);
- /// Cast from one machine scalar to another.
- ///
- /// # Examples
- ///
- /// ```
- /// # use num_traits as num;
- /// let twenty: f32 = num::cast(0x14).unwrap();
- /// assert_eq!(twenty, 20f32);
- /// ```
- ///
- #[inline]
- 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>;
- }
- macro_rules! impl_num_cast {
- ($T:ty, $conv:ident) => (
- impl NumCast for $T {
- #[inline]
- #[allow(deprecated)]
- fn from<N: ToPrimitive>(n: N) -> Option<$T> {
- // `$conv` could be generated using `concat_idents!`, but that
- // macro seems to be broken at the moment
- n.$conv()
- }
- }
- )
- }
- impl_num_cast!(u8, to_u8);
- impl_num_cast!(u16, to_u16);
- impl_num_cast!(u32, to_u32);
- impl_num_cast!(u64, to_u64);
- impl_num_cast!(usize, to_usize);
- impl_num_cast!(i8, to_i8);
- impl_num_cast!(i16, to_i16);
- impl_num_cast!(i32, to_i32);
- impl_num_cast!(i64, to_i64);
- impl_num_cast!(isize, to_isize);
- impl_num_cast!(f32, to_f32);
- impl_num_cast!(f64, to_f64);
|