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