#![allow(deprecated)]
use crate::integer::{MiniInteger, ToMini};
use crate::{Assign, Integer};
use core::fmt::{Debug, Formatter, Result as FmtResult};
use core::marker::PhantomData;
use core::ops::Deref;
use gmp_mpfr_sys::gmp::limb_t;
#[deprecated(since = "1.23.0", note = "use `MiniInteger` instead")]
#[repr(transparent)]
#[derive(Clone)]
pub struct SmallInteger {
inner: Integer,
phantom: PhantomData<*const limb_t>,
}
unsafe impl Send for SmallInteger {}
impl Default for SmallInteger {
#[inline]
fn default() -> Self {
SmallInteger::new()
}
}
impl Debug for SmallInteger {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
Debug::fmt(&self.inner, f)
}
}
impl SmallInteger {
#[inline]
pub const fn new() -> Self {
SmallInteger {
inner: Integer::new(),
phantom: PhantomData,
}
}
#[inline]
pub unsafe fn as_nonreallocating_integer(&mut self) -> &mut Integer {
&mut self.inner
}
}
impl Deref for SmallInteger {
type Target = Integer;
#[inline]
fn deref(&self) -> &Integer {
&self.inner
}
}
#[deprecated(since = "1.23.0", note = "`ToMini` instead")]
pub trait ToSmall: ToMini {}
impl<T: ToMini> ToSmall for T {}
impl<T: ToSmall> Assign<T> for SmallInteger {
#[inline]
fn assign(&mut self, src: T) {
let mut mini = MiniInteger::from(src);
self.inner.assign(mini.borrow_excl())
}
}
impl<T: ToSmall> From<T> for SmallInteger {
#[inline]
fn from(src: T) -> Self {
let mut mini = MiniInteger::from(src);
SmallInteger {
inner: Integer::from(mini.borrow_excl()),
phantom: PhantomData,
}
}
}
impl Assign<&Self> for SmallInteger {
#[inline]
fn assign(&mut self, other: &Self) {
self.clone_from(other);
}
}
impl Assign for SmallInteger {
#[inline]
fn assign(&mut self, other: Self) {
*self = other;
}
}
#[cfg(test)]
mod tests {
use crate::Assign;
use crate::integer::SmallInteger;
#[test]
fn check_assign() {
let mut i = SmallInteger::from(-1i32);
assert_eq!(*i, -1);
let other = SmallInteger::from(2i32);
i.assign(&other);
assert_eq!(*i, 2);
i.assign(6u8);
assert_eq!(*i, 6);
i.assign(-6i8);
assert_eq!(*i, -6);
i.assign(other);
assert_eq!(*i, 2);
i.assign(6u16);
assert_eq!(*i, 6);
i.assign(-6i16);
assert_eq!(*i, -6);
i.assign(6u32);
assert_eq!(*i, 6);
i.assign(-6i32);
assert_eq!(*i, -6);
i.assign(0xf_0000_0006u64);
assert_eq!(*i, 0xf_0000_0006u64);
i.assign(-0xf_0000_0006i64);
assert_eq!(*i, -0xf_0000_0006i64);
i.assign((6u128 << 64) | 7u128);
assert_eq!(*i, (6u128 << 64) | 7u128);
i.assign((-6i128 << 64) | 7i128);
assert_eq!(*i, (-6i128 << 64) | 7i128);
i.assign(6usize);
assert_eq!(*i, 6);
i.assign(-6isize);
assert_eq!(*i, -6);
i.assign(0u32);
assert_eq!(*i, 0);
}
}