1use std::fmt::Debug;
4
5use crate::{
6    ffi::{zend_refcounted_h, zend_string},
7    types::ZendObject,
8};
9
10pub type ZendRefcount = zend_refcounted_h;
12
13impl Debug for ZendRefcount {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        f.debug_struct("ZendRefcount")
16            .field("refcount", &self.refcount)
17            .finish()
18    }
19}
20
21pub trait PhpRc {
23    fn get_rc(&self) -> &ZendRefcount;
25
26    fn get_rc_mut(&mut self) -> &mut ZendRefcount;
28
29    fn get_count(&self) -> u32 {
31        self.get_rc().refcount
32    }
33
34    fn inc_count(&mut self) {
36        self.get_rc_mut().refcount += 1;
37    }
38
39    fn dec_count(&mut self) {
41        self.get_rc_mut().refcount -= 1;
42    }
43}
44
45macro_rules! rc {
46    ($($t: ty),*) => {
47        $(
48            impl PhpRc for $t {
49                fn get_rc(&self) -> &ZendRefcount {
50                    &self.gc
51                }
52
53                fn get_rc_mut(&mut self) -> &mut ZendRefcount {
54                    &mut self.gc
55                }
56            }
57        )*
58    };
59}
60
61rc!(ZendObject, zend_string);