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