use crate::Integer;
use core::fmt::{
Binary, Debug, Display, Formatter, LowerHex, Octal, Pointer, Result as FmtResult, UpperHex,
};
#[cfg(feature = "float")]
use core::fmt::{LowerExp, UpperExp};
use core::marker::PhantomData;
use core::ops::Deref;
use gmp_mpfr_sys::gmp::mpz_t;
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct BorrowInteger<'a> {
inner: mpz_t,
phantom: PhantomData<&'a Integer>,
}
impl<'a> BorrowInteger<'a> {
#[inline]
pub const unsafe fn from_raw(raw: mpz_t) -> BorrowInteger<'a> {
BorrowInteger {
inner: raw,
phantom: PhantomData,
}
}
#[inline]
pub const fn const_deref<'b>(b: &'b BorrowInteger<'a>) -> &'b Integer {
let ptr = cast_ptr!(&b.inner, Integer);
unsafe { &*ptr }
}
}
impl Deref for BorrowInteger<'_> {
type Target = Integer;
#[inline]
fn deref(&self) -> &Integer {
BorrowInteger::const_deref(self)
}
}
impl Pointer for BorrowInteger<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
Pointer::fmt(&&**self, f)
}
}
impl Display for BorrowInteger<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
Display::fmt(&**self, f)
}
}
impl Debug for BorrowInteger<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
Debug::fmt(&**self, f)
}
}
impl Binary for BorrowInteger<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
Binary::fmt(&**self, f)
}
}
impl Octal for BorrowInteger<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
Octal::fmt(&**self, f)
}
}
impl LowerHex for BorrowInteger<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
LowerHex::fmt(&**self, f)
}
}
impl UpperHex for BorrowInteger<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
UpperHex::fmt(&**self, f)
}
}
#[cfg(feature = "float")]
impl LowerExp for BorrowInteger<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
LowerExp::fmt(&**self, f)
}
}
#[cfg(feature = "float")]
impl UpperExp for BorrowInteger<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
UpperExp::fmt(&**self, f)
}
}
unsafe impl Send for BorrowInteger<'_> {}
unsafe impl Sync for BorrowInteger<'_> {}