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>
impl<T: ?Sized> RustObjAccess<T>
pub fn new_strong(ptr: Rc<RefCell<T>>) -> Self
pub fn new_weak(ptr: Weak<RefCell<T>>) -> Self
pub fn try_with_borrow<F, R>(&self, f: F) -> Result<R, RustObjAccessError>
pub fn try_with_borrow_mut<F, R>(&self, f: F) -> Result<R, RustObjAccessError>
Sourcepub fn try_store_handle_and_call_qml<F, R>(
&self,
f: F,
) -> Result<R, RustObjAccessError>
pub fn try_store_handle_and_call_qml<F, R>( &self, f: F, ) -> Result<R, RustObjAccessError>
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.
Sourcepub fn try_store_handle_and_call_qml_mut<F, R>(
&self,
f: F,
) -> Result<R, RustObjAccessError>
pub fn try_store_handle_and_call_qml_mut<F, R>( &self, f: F, ) -> Result<R, RustObjAccessError>
Similar to [try_store_handle_and_call_qml].