RustObjAccess

Struct RustObjAccess 

Source
pub struct RustObjAccess<T: ?Sized + 'static> { /* private fields */ }
Expand description

A structure that provides controlled access to a Rust object by invoking functors (potentially recursively) on the object while it is borrowed immutably or mutably.

Supports recursive borrowing under the following rules:

  • If the object is initially borrowed mutably, both mutable and immutable recursive calls are allowed.
  • If the object is initially borrowed immutably, only immutable recursive calls are allowed.

The object is held using Rc<RefCell<T>> or Weak<RefCell<T>>.

The need for recursive borrowing (even mutable) appears from the separation between Rust struct and C++ class we have to live with. The group of the objects consists of:

  • the Rust object itself.
  • the C++ proxy implementing needed C++ interface, forwarding calls to the Rust Proxy.
  • the Rust proxy that connects C++ proxy and Rust object via ‘CXX’. In pure C++ implementation that would typically be a single class inheriting from a needed base class / interface. However, because Rust lacks inheritance, we have to deal with Rust/C++ objects Frankenstein.

In summary, this struct prevents borrow errors and panics in scenarios involving recursive call chains such as: Rust object -> Base implementation in C++ proxy -> Rust object even though this requires bending standard Rust borrowing principles. From the user’s perspective, borrowing semantics remain equivalent to working with a normal Rc<RefCell<T>>.

Implementations§

Source§

impl<T: ?Sized> RustObjAccess<T>

Source

pub fn new_strong(ptr: Rc<RefCell<T>>) -> Self

Source

pub fn new_weak(ptr: Weak<RefCell<T>>) -> Self

Source

pub fn try_with_borrow<F, R>(&self, f: F) -> Result<R, RustObjAccessError>
where F: FnOnce(&T) -> R,

Source

pub fn try_with_borrow_mut<F, R>(&self, f: F) -> Result<R, RustObjAccessError>
where F: FnOnce(&mut T) -> R,

Source

pub fn try_store_handle_and_call_qml<F, R>( &self, f: F, ) -> Result<R, RustObjAccessError>
where F: FnOnce(&T) -> R,

Executes f on the inner T assuming that a valid RefCell borrow already exists somewhere in the current call stack.

This is intended for Rust -> C++/QML -> Rust re-entry scenarios: Rust code borrows from Rc<RefCell<T>> and calls into C++/QML code. That code may call back into Rust. Borrowing the RefCell again when re-entering Rust would normally fail, because the original borrow is still active.

To support this case, the function stores access to the object and invokes f using a raw pointer obtained via Rc::as_ptr(), bypassing the RefCell borrow mechanism.

This must only be used when the caller can guarantee that a valid borrow of the RefCell already exists externally. Rust code that directly accesses the object should use the normal borrow() or borrow_mut() APIs instead.

The function performs runtime checks to validate the assumption that the borrow originates outside this call.

Source

pub fn try_store_handle_and_call_qml_mut<F, R>( &self, f: F, ) -> Result<R, RustObjAccessError>
where F: FnOnce(&mut T) -> R,

Similar to [try_store_handle_and_call_qml].

Auto Trait Implementations§

§

impl<T> !Freeze for RustObjAccess<T>

§

impl<T> !RefUnwindSafe for RustObjAccess<T>

§

impl<T> !Send for RustObjAccess<T>

§

impl<T> !Sync for RustObjAccess<T>

§

impl<T> Unpin for RustObjAccess<T>
where T: ?Sized,

§

impl<T> !UnwindSafe for RustObjAccess<T>

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