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>
impl<T> AsyncRefCell<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume the cell and return the contained value.
Source§impl<T: ?Sized> AsyncRefCell<T>
impl<T: ?Sized> AsyncRefCell<T>
Sourcepub async fn borrow(&self) -> Ref<'_, T>
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.
Sourcepub async fn borrow_mut(&self) -> RefMut<'_, T>
pub async fn borrow_mut(&self) -> RefMut<'_, T>
Wait until an exclusive borrow is available, then take it.
Sourcepub fn try_borrow(&self) -> Option<Ref<'_, T>>
pub fn try_borrow(&self) -> Option<Ref<'_, T>>
Attempt to take an immutable borrow without waiting.
Sourcepub fn try_borrow_mut(&self) -> Option<RefMut<'_, T>>
pub fn try_borrow_mut(&self) -> Option<RefMut<'_, T>>
Attempt to take an exclusive borrow without waiting.
Sourcepub fn poll_with_mut<R>(
&self,
cx: &mut Context<'_>,
ticket: &mut ScopedTicket<'_>,
f: impl FnOnce(&mut T, &mut Context<'_>) -> Poll<R>,
) -> Poll<R>
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:
- 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.
- 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. - If
freturnedPending(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.
Sourcepub fn scoped_ticket(&self) -> ScopedTicket<'_>
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.
Sourcepub fn cond(&self) -> &AsyncCondition
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).