2021-10-10 06:51:55 +02:00
|
|
|
//! Traits and types for interacting with reference counted PHP types.
|
2021-09-21 11:22:05 +02:00
|
|
|
|
2021-10-10 11:46:26 +02:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2021-10-10 06:51:55 +02:00
|
|
|
use crate::{
|
|
|
|
ffi::{zend_refcounted_h, zend_string},
|
|
|
|
types::ZendObject,
|
|
|
|
};
|
2021-09-21 11:22:05 +02:00
|
|
|
|
|
|
|
/// Object used to store Zend reference counter.
|
|
|
|
pub type ZendRefcount = zend_refcounted_h;
|
|
|
|
|
2021-10-10 11:46:26 +02:00
|
|
|
impl Debug for ZendRefcount {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("ZendRefcount")
|
|
|
|
.field("refcount", &self.refcount)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 11:22:05 +02:00
|
|
|
/// Implemented on refcounted types.
|
|
|
|
pub trait PhpRc {
|
|
|
|
/// Returns an immutable reference to the corresponding refcount object.
|
|
|
|
fn get_rc(&self) -> &ZendRefcount;
|
|
|
|
|
|
|
|
/// Returns a mutable reference to the corresponding refcount object.
|
|
|
|
fn get_rc_mut(&mut self) -> &mut ZendRefcount;
|
|
|
|
|
|
|
|
/// Returns the number of references to the object.
|
|
|
|
fn get_count(&self) -> u32 {
|
|
|
|
self.get_rc().refcount
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Increments the reference counter by 1.
|
|
|
|
fn inc_count(&mut self) {
|
|
|
|
self.get_rc_mut().refcount += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decrements the reference counter by 1.
|
|
|
|
fn dec_count(&mut self) {
|
|
|
|
self.get_rc_mut().refcount -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! rc {
|
|
|
|
($($t: ty),*) => {
|
|
|
|
$(
|
|
|
|
impl PhpRc for $t {
|
|
|
|
fn get_rc(&self) -> &ZendRefcount {
|
|
|
|
&self.gc
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_rc_mut(&mut self) -> &mut ZendRefcount {
|
|
|
|
&mut self.gc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
rc!(ZendObject, zend_string);
|