#[repr(C)]pub struct RefCount {
pub ptr: *const RefCountInner,
pub run_destructor: bool,
}Expand description
Wrapper around a heap-allocated RefCountInner.
This is the shared metadata that all RefAny clones point to.
The RefCount is responsible for all memory management:
RefCount::clone()incrementsnum_copiesinRefCountInnerRefCount::drop()decrementsnum_copiesand, if it reaches 0:- Frees the
RefCountInner - Calls the custom destructor on the data
- Deallocates the data memory
- Frees the
§Why run_destructor: bool
This flag tracks whether this RefCount instance should decrement
num_copies when dropped. Set to true for all clones (including
those created by RefAny::clone() and AZ_REFLECT macros).
Set to false after the decrement has been performed to prevent
double-decrement.
Fields§
§ptr: *const RefCountInner§run_destructor: boolImplementations§
Source§impl RefCount
impl RefCount
Runtime check: can we create a shared borrow?
Returns true if there are no active mutable borrows.
Multiple shared borrows can coexist (like &T in Rust).
§Memory Ordering
Uses SeqCst to ensure we see the most recent state from all threads.
If another thread just released a mutable borrow, we’ll see it.
Runtime check: can we create a mutable borrow?
Returns true only if there are ZERO active borrows of any kind.
This enforces Rust’s exclusive mutability rule (like &mut T).
§Memory Ordering
Uses SeqCst to ensure we see all recent borrows from all threads.
Both counters must be checked atomically to prevent races.
Sourcepub fn increase_ref(&self)
pub fn increase_ref(&self)
Increments the shared borrow counter.
Called when a Ref<T> is created. The Ref::drop will decrement it.
§Memory Ordering
SeqCst ensures this increment is visible to all threads before they
try to acquire a mutable borrow (which checks this counter).
Sourcepub fn decrease_ref(&self)
pub fn decrease_ref(&self)
Decrements the shared borrow counter.
Called when a Ref<T> is dropped, indicating the borrow is released.
§Underflow guard
Saturates at 0: an unmatched decrement — e.g. a C caller running
FooRef_delete after a FAILED downcast with a pre-0.2.1 copy of
azul.h (whose macro did not skip the decrease), or a plain
double-delete — must not wrap num_refs to usize::MAX, which
would make can_be_shared_mut() return false for the rest of
the process (callbacks silently stop mutating state).
§Memory Ordering
SeqCst ensures this decrement is immediately visible to other threads
waiting to acquire a mutable borrow.
Sourcepub fn increase_refmut(&self)
pub fn increase_refmut(&self)
Increments the mutable borrow counter.
Called when a RefMut<T> is created. Should only succeed when this
counter and num_refs are both 0.
§Memory Ordering
SeqCst ensures this increment is visible to all other threads,
blocking them from acquiring any borrow (shared or mutable).
Sourcepub fn decrease_refmut(&self)
pub fn decrease_refmut(&self)
Decrements the mutable borrow counter.
Called when a RefMut<T> is dropped, releasing exclusive access.
§Underflow guard
Saturates at 0 (see Self::decrease_ref): a double
FooRefMut_delete from C must not wrap num_mutable_refs, which
would corrupt the runtime borrow checker and let a second thread
or timer callback obtain an aliasing mutable borrow.
§Memory Ordering
SeqCst ensures this decrement is immediately visible, allowing
other threads to acquire borrows.
Trait Implementations§
Source§impl Clone for RefCount
impl Clone for RefCount
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the RefCount and increments the reference count.
§Safety
This is safe because:
- The ptr is valid (created from
Box::into_raw) num_copiesis atomically incremented withSeqCstordering- This ensures the
RefCountInneris not freed while clones exist
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more