Struct vtable::VRc

source ·
pub struct VRc<VTable: VTableMetaDropInPlace + 'static, X = Dyn> { /* private fields */ }
Expand description

A reference counted pointer to an object matching the virtual table T

Similar to alloc::rc::Rc where the VTable type parameter is a VTable struct annotated with #[vtable], and the X type parameter is the actual instance. When X is the Dyn type marker, this means that the X is not known and the only thing that can be done is to get a VRef<VTable> through the Self::borrow() function.

Other differences with the alloc::rc::Rc types are:

  • It does not allow to access mutable reference. (No get_mut or make_mut), meaning it is safe to get a Pin reference with borrow_pin.
  • It is safe to pass it across ffi boundaries.

Implementations§

source§

impl<VTable: VTableMetaDropInPlace, X: HasStaticVTable<VTable>> VRc<VTable, X>

source

pub fn new(data: X) -> Self

Create a new VRc from an instance of a type that can be associated with a VTable.

Will move the instance on the heap.

(the HasStaticVTable is implemented by the “MyTrait”VTable_static! macro generated by the #vtable macro)

source

pub fn into_dyn(this: Self) -> VRc<VTable, Dyn>
where Self: 'static,

Convert a VRc of a real instance to a VRc of a Dyn instance

source§

impl<VTable: VTableMetaDropInPlace + 'static, X: HasStaticVTable<VTable> + 'static> VRc<VTable, X>

source

pub fn map<MappedType: ?Sized>( this: Self, map_fn: impl for<'r> FnOnce(Pin<&'r X>) -> Pin<&'r MappedType> ) -> VRcMapped<VTable, MappedType>

This function allows safely holding a reference to a field inside the VRc. In order to accomplish that, you need to provide a mapping function map_fn in which you need to provide and return a pinned reference to the object you would like to map. The returned VRcMapped allows obtaining that pinned reference again using VRcMapped::as_pin_ref.

source§

impl<VTable: VTableMetaDropInPlace + 'static> VRc<VTable, Dyn>

source

pub fn map_dyn<MappedType: ?Sized>( this: Self, map_fn: impl for<'r> FnOnce(Pin<VRef<'r, VTable>>) -> Pin<&'r MappedType> ) -> VRcMapped<VTable, MappedType>

This function allows safely holding a reference to a field inside the VRc. In order to accomplish that, you need to provide a mapping function map_fn in which you need to provide and return a pinned reference to the object you would like to map. The returned VRcMapped allows obtaining that pinned reference again using VRcMapped::as_pin_ref. This works similar to VRc::map except that it works on a type-erased VRc.

source§

impl<VTable: VTableMetaDropInPlace, X> VRc<VTable, X>

source

pub fn as_pin_ref(&self) -> Pin<&X>

Create a Pinned reference to the inner.

This is safe because we don’t allow mutable reference to the inner

source

pub fn borrow(this: &Self) -> VRef<'_, VTable>

Gets a VRef pointing to this instance

source

pub fn borrow_pin(this: &Self) -> Pin<VRef<'_, VTable>>

Gets a Pin<VRef> pointing to this instance

This is safe because there is no way to access a mutable reference to the pointee. (There is no get_mut or make_mut),

source

pub fn downgrade(this: &Self) -> VWeak<VTable, X>

Construct a VWeak pointing to this instance.

source

pub fn strong_count(this: &Self) -> usize

Gets the number of strong (VRc) pointers to this allocation.

source

pub fn ptr_eq(this: &Self, other: &Self) -> bool

Returns true if the two VRc’s point to the same allocation

Trait Implementations§

source§

impl<VTable: VTableMetaDropInPlace + 'static, X> Clone for VRc<VTable, X>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<VTable: VTableMetaDropInPlace + 'static, X> Debug for VRc<VTable, X>

source§

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

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

impl<VTable: VTableMetaDropInPlace, X> Deref for VRc<VTable, X>

§

type Target = X

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<VTable: VTableMetaDropInPlace + 'static, X> Drop for VRc<VTable, X>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<VTable: VTableMetaDropInPlace + 'static, X> CloneStableDeref for VRc<VTable, X>

Safety: The data VRc manages is held by VRcInner, and a clone of a VRc merely clones the pointer to the VRcInner.

source§

impl<VTable: VTableMetaDropInPlace + Send + Sync + 'static, X: Send + Sync> Send for VRc<VTable, X>

source§

impl<VTable: VTableMetaDropInPlace + 'static, X> StableDeref for VRc<VTable, X>

Safety: The data VRc manages is held by VRcInner, which maintains its address when the VRc is moved.

source§

impl<VTable: VTableMetaDropInPlace + Send + Sync + 'static, X: Send + Sync> Sync for VRc<VTable, X>

Auto Trait Implementations§

§

impl<VTable, X> Freeze for VRc<VTable, X>

§

impl<VTable, X> RefUnwindSafe for VRc<VTable, X>
where X: RefUnwindSafe, <VTable as VTableMeta>::VTable: RefUnwindSafe,

§

impl<VTable, X> Unpin for VRc<VTable, X>

§

impl<VTable, X> UnwindSafe for VRc<VTable, X>
where X: RefUnwindSafe, <VTable as VTableMeta>::VTable: RefUnwindSafe,

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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,

§

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>,

§

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>,

§

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.