AsyncRefCell

Struct AsyncRefCell 

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

Single-threaded async-aware interior-mutability cell.

Mirrors core::cell::RefCell, but borrow() and borrow_mut() are futures that await until the cell is available rather than panicking on contention.

Implementations§

Source§

impl<T> AsyncRefCell<T>

Source

pub fn new(value: T) -> Self

Construct a new cell holding value.

Source

pub fn into_inner(self) -> T

Consume the cell and return the contained value.

Source§

impl<T: ?Sized> AsyncRefCell<T>

Source

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

Wait until an immutable borrow is available, then take it.

Multiple borrow() guards may be held concurrently. borrow_mut() waits until all of them have been dropped.

Source

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

Wait until an exclusive borrow is available, then take it.

Source

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

Attempt to take an immutable borrow without waiting.

Source

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

Attempt to take an exclusive borrow without waiting.

Source

pub fn poll_with_mut<R>( &self, cx: &mut Context<'_>, ticket: &mut ScopedTicket<'_>, f: impl FnOnce(&mut T, &mut Context<'_>) -> Poll<R>, ) -> Poll<R>

Poll f with a short-lived exclusive borrow, staying registered on the cell’s wake condition across Pending polls.

This is the primitive for poll_fn-style drivers that need to race a borrow attempt against other wake sources (radio I/O, a timer) and be re-polled whenever a guard is released — for example when a second handle mutates the cell and drops its borrow.

Behavior per poll:

  1. Deregister this task’s waker from the wake condition. Every guard drop triggers the condition, so a waker registered before taking the borrow would be woken by our own guard drop in step 2, re-polling the task in a busy loop. This crate is single-threaded, so nothing can trigger between this step and step 3 — no wakeup can be lost to the gap.
  2. If the cell is free, take the exclusive borrow and run f (which may register other wakers, e.g. radio or timer). The guard is dropped — waking other waiters — before step 3.
  3. If f returned Pending (or the cell was busy), re-register on the condition so a later guard release re-polls this task.

ticket must be obtained from scoped_ticket once before the wait begins and reused across every poll of that wait. The ticket deregisters itself on drop, so a wait that is cancelled mid-Pending (e.g. losing a select! race) cannot leak its waker registration.

Source

pub fn scoped_ticket(&self) -> ScopedTicket<'_>

Create an RAII ticket for use with poll_with_mut.

The ticket deregisters from the cell’s wake condition when dropped, so waits abandoned before completion do not leak waker slots.

Source

pub fn cond(&self) -> &AsyncCondition

Borrow the underlying wake condition.

Each guard drop triggers this condition, waking any task currently suspended on cond.wait(). Useful for callers that want to race a borrow attempt against other wake sources (e.g. a poll_fn that should re-poll when the cell becomes available or when a timer fires, whichever comes first).

Trait Implementations§

Source§

impl<T: Debug> Debug for AsyncRefCell<T>

Source§

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

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

impl<T: Default> Default for AsyncRefCell<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for AsyncRefCell<T>

§

impl<T> !RefUnwindSafe for AsyncRefCell<T>

§

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

§

impl<T> !Sync for AsyncRefCell<T>

§

impl<T> Unpin for AsyncRefCell<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for AsyncRefCell<T>
where T: UnwindSafe + ?Sized,

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.