use crate::natural::InnerNatural::Small;
use crate::natural::Natural;
use crate::platform::Limb;
use malachite_base::num::basic::traits::Zero;
use malachite_base::num::conversion::traits::{ConvertibleFrom, SaturatingFrom, VecFromOtherType};
impl Natural {
pub const fn const_from(x: Limb) -> Self {
Self(Small(x))
}
}
macro_rules! impl_from_limb {
($t: ident) => {
impl From<$t> for Natural {
#[inline]
fn from(u: $t) -> Natural {
Natural(Small(u))
}
}
};
}
macro_rules! impl_from_smaller_than_limb {
($t: ident) => {
impl From<$t> for Natural {
#[inline]
fn from(u: $t) -> Natural {
Natural(Small(Limb::from(u)))
}
}
};
}
macro_rules! impl_from_larger_than_limb_or_usize {
($t: ident) => {
impl From<$t> for Natural {
#[inline]
fn from(u: $t) -> Natural {
Natural::from_owned_limbs_asc(Limb::vec_from_other_type(u))
}
}
};
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct NaturalFromSignedError;
macro_rules! impl_signed {
($t: ident) => {
impl TryFrom<$t> for Natural {
type Error = NaturalFromSignedError;
#[inline]
fn try_from(i: $t) -> Result<Natural, Self::Error> {
if i >= 0 {
Ok(Natural::from(i.unsigned_abs()))
} else {
Err(NaturalFromSignedError)
}
}
}
impl ConvertibleFrom<$t> for Natural {
#[inline]
fn convertible_from(i: $t) -> bool {
i >= 0
}
}
impl SaturatingFrom<$t> for Natural {
#[inline]
fn saturating_from(i: $t) -> Natural {
if i >= 0 {
Natural::from(i.unsigned_abs())
} else {
Natural::ZERO
}
}
}
};
}
impl_from_smaller_than_limb!(u8);
impl_from_smaller_than_limb!(u16);
#[cfg(feature = "32_bit_limbs")]
impl_from_limb!(u32);
#[cfg(not(feature = "32_bit_limbs"))]
impl_from_smaller_than_limb!(u32);
#[cfg(feature = "32_bit_limbs")]
impl_from_larger_than_limb_or_usize!(u64);
#[cfg(not(feature = "32_bit_limbs"))]
impl_from_limb!(u64);
impl_from_larger_than_limb_or_usize!(u128);
impl_from_larger_than_limb_or_usize!(usize);
apply_to_signeds!(impl_signed);