pub struct ReentrantRefCell<T: ?Sized> { /* private fields */ }
Expand description

A RefCell that allows to recursively retrieve a mutable reference.

Like std::cell::RefCell, but with an additional Self::borrow_mut_reentrant() method. (If needed, the type could call through to more of RefCells other methods.)

Implementations§

source§

impl<T> ReentrantRefCell<T>

source

pub fn new(data: T) -> Self

source

pub fn borrow(&self) -> Ref<'_, T>

source

pub fn borrow_mut(&self) -> RefMut<'_, T>

source

pub unsafe fn borrow_mut_reentrant<F, U>(&self, f: F) -> U
where F: FnOnce(&mut T) -> U,

Mutably borrows the wrapped value for the duration of the closure call, again allowing mutable borrows by means of this method inside of the closure and the functions it calls.

It borrows with std::cell::RefCell::borrow_mut() for the outermost call, which means additional attempts to borrow during the outermost borrow, other than by means of this method, will panic. Repeated inner calls provide the mutable reference that the outermost call made available.

The function is useful when dealing with an FFI and foreign code calls into your callback with a ReentrantRefCell at hand, you then call an FFI function and, during this call, the foreign code calls into your callback again. This happens, e.g., with window procedures on Windows when calling functions like DestroyWindow() or MoveWindow() in the procedure itself.

Safety

You are responsible to only call reentrance causing functions (like FFI functions) as if they had a &mut self parameter and wouldn’t cause a compiler error with that. I.e., you must, e.g., not borrow something mutably from the mutable reference you get, call the reentrance causing function and then continue to use the borrow from before. When used in the relevant cases, a helper function that simply demands a &mut self parameter and just calls through to the closure from its second parameter would trigger compiler errors.

Searching for “sen” (“send”/“sent”) on Windows API function doc pages seems to be a good way to check whether a function may synchronously call the window procedure.

Trait Implementations§

source§

impl<T> Send for ReentrantRefCell<T>
where T: Send + ?Sized,

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for ReentrantRefCell<T>

§

impl<T> !Sync for ReentrantRefCell<T>

§

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

§

impl<T: ?Sized> UnwindSafe for ReentrantRefCell<T>
where T: UnwindSafe,

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

source§

fn map_self<O, T>(self, op: O) -> T
where O: FnOnce(Self) -> T, Self: Sized,

Example: Read more
source§

fn map_self_or_keep<O>(self, op: O) -> Self
where O: FnOnce(&Self) -> Option<Self>, Self: Sized,

Example: 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.