Skip to main content

RefCount

Struct RefCount 

Source
#[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() increments num_copies in RefCountInner
  • RefCount::drop() decrements num_copies and, if it reaches 0:
    1. Frees the RefCountInner
    2. Calls the custom destructor on the data
    3. 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: bool

Implementations§

Source§

impl RefCount

Source

pub fn can_be_shared(&self) -> bool

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.

Source

pub fn can_be_shared_mut(&self) -> bool

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.

Source

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).

Source

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.

Source

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).

Source

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

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for RefCount

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for RefCount

Source§

fn drop(&mut self)

Decrements the reference count when a RefCount clone is dropped.

If this was the last reference (num_copies reaches 0), this will also free the RefCountInner and call the custom destructor.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for RefCount

Source§

impl Hash for RefCount

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for RefCount

Source§

fn cmp(&self, other: &RefCount) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for RefCount

Source§

fn eq(&self, other: &RefCount) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for RefCount

Source§

fn partial_cmp(&self, other: &RefCount) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for RefCount

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> GetHash for T
where T: Hash,

Source§

fn get_hash(&self) -> u64

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.