use crate::{sys::*, values::ZVal};
use std::fmt::{self, Debug};
#[repr(transparent)]
pub struct ZRef {
inner: zend_reference,
}
impl ZRef {
pub unsafe fn from_ptr<'a>(ptr: *const zend_reference) -> &'a Self {
unsafe {
(ptr as *const Self)
.as_ref()
.expect("ptr should not be null")
}
}
pub unsafe fn try_from_ptr<'a>(ptr: *const zend_reference) -> Option<&'a Self> {
unsafe { (ptr as *const Self).as_ref() }
}
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_reference) -> &'a mut Self {
unsafe { (ptr as *mut Self).as_mut().expect("ptr should not be null") }
}
pub unsafe fn try_from_mut_ptr<'a>(ptr: *mut zend_reference) -> Option<&'a mut Self> {
unsafe { (ptr as *mut Self).as_mut() }
}
pub const fn as_ptr(&self) -> *const zend_reference {
&self.inner
}
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut zend_reference {
&mut self.inner
}
pub fn val(&self) -> &ZVal {
unsafe { ZVal::from_ptr(&self.inner.val) }
}
pub fn val_mut(&mut self) -> &mut ZVal {
unsafe { ZVal::from_mut_ptr(&mut self.inner.val) }
}
}
impl Debug for ZRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ZRef").field("val", &self.val()).finish()
}
}