pub unsafe trait AlwaysRefCounted {
    // Required methods
    fn inc_ref(&self);
    unsafe fn dec_ref(obj: NonNull<Self>);
Expand description

Types that are always reference counted.

It allows such types to define their own custom ref increment and decrement functions. Additionally, it allows users to convert from a shared reference &T to an owned reference ARef<T>.

This is usually implemented by wrappers to existing structures on the C side of the code. For Rust code, the recommendation is to use Arc to create reference-counted instances of a type.


Implementers must ensure that increments to the reference count keep the object alive in memory at least until matching decrements are performed.

Implementers must also ensure that all instances are reference-counted. (Otherwise they won’t be able to honour the requirement that AlwaysRefCounted::inc_ref keep the object alive.)

Required Methods§


fn inc_ref(&self)

Increments the reference count on the object.


unsafe fn dec_ref(obj: NonNull<Self>)

Decrements the reference count on the object.

Frees the object when the count reaches zero.


Callers must ensure that there was a previous matching increment to the reference count, and that the object is no longer used after its reference count is decremented (as it may result in the object being freed), unless the caller owns another increment on the refcount (e.g., it calls AlwaysRefCounted::inc_ref twice, then calls AlwaysRefCounted::dec_ref once).