#[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_copiesin RefCountInnerRefCount::drop()decrementsnum_copiesand, if it reaches 0:- Frees the RefCountInner
- Calls the custom destructor on the data
- Deallocates the data memory
§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.
§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.
§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_copies is atomically incremented with SeqCst ordering
- This ensures the RefCountInner is 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