ext-php-rs/src/rc.rs
David Cole 466c1658e3
Refactor module paths (#101)
* Refactor module layout

* Fixed documentation tests

* Removed skel, moved macro crate

* Ignore folders for crate publish

* Fix builder for zts

* Add `rustfmt.toml`, wrap all comments #96

* Fixed up documentation links, tidied up

* Add `Zend` prefix to callable and hashtable

* Updated guide types

* Updated changelog
2021-10-10 17:51:55 +13:00

52 lines
1.3 KiB
Rust

//! Traits and types for interacting with reference counted PHP types.
use crate::{
ffi::{zend_refcounted_h, zend_string},
types::ZendObject,
};
/// Object used to store Zend reference counter.
pub type ZendRefcount = zend_refcounted_h;
/// 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);