Desugar some async fns
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
//!
|
||||
//! This module provides a mutex that can be used to synchronize data between asynchronous tasks.
|
||||
use core::cell::{RefCell, UnsafeCell};
|
||||
use core::future::poll_fn;
|
||||
use core::future::{poll_fn, Future};
|
||||
use core::ops::{Deref, DerefMut};
|
||||
use core::task::Poll;
|
||||
use core::{fmt, mem};
|
||||
@@ -73,7 +73,7 @@ where
|
||||
/// Lock the mutex.
|
||||
///
|
||||
/// This will wait for the mutex to be unlocked if it's already locked.
|
||||
pub async fn lock(&self) -> MutexGuard<'_, M, T> {
|
||||
pub fn lock(&self) -> impl Future<Output = MutexGuard<'_, M, T>> {
|
||||
poll_fn(|cx| {
|
||||
let ready = self.state.lock(|s| {
|
||||
let mut s = s.borrow_mut();
|
||||
@@ -92,7 +92,6 @@ where
|
||||
Poll::Pending
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Attempt to immediately lock the mutex.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Synchronization primitive for initializing a value once, allowing others to await a reference to the value.
|
||||
|
||||
use core::cell::Cell;
|
||||
use core::future::poll_fn;
|
||||
use core::future::{poll_fn, Future};
|
||||
use core::mem::MaybeUninit;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use core::task::Poll;
|
||||
@@ -55,7 +55,7 @@ impl<T> OnceLock<T> {
|
||||
|
||||
/// Get a reference to the underlying value, waiting for it to be set.
|
||||
/// If the value is already set, this will return immediately.
|
||||
pub async fn get(&self) -> &T {
|
||||
pub fn get(&self) -> impl Future<Output = &T> {
|
||||
poll_fn(|cx| match self.try_get() {
|
||||
Some(data) => Poll::Ready(data),
|
||||
None => {
|
||||
@@ -63,7 +63,6 @@ impl<T> OnceLock<T> {
|
||||
Poll::Pending
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Try to get a reference to the underlying value if it exists.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! A synchronization primitive for passing the latest value to **multiple** receivers.
|
||||
|
||||
use core::cell::RefCell;
|
||||
use core::future::poll_fn;
|
||||
use core::future::{poll_fn, Future};
|
||||
use core::marker::PhantomData;
|
||||
use core::ops::{Deref, DerefMut};
|
||||
use core::task::{Context, Poll};
|
||||
@@ -547,8 +547,8 @@ impl<'a, T: Clone, W: WatchBehavior<T> + ?Sized> Rcv<'a, T, W> {
|
||||
/// Returns the current value of the `Watch` once it is initialized, marking it as seen.
|
||||
///
|
||||
/// **Note**: Futures do nothing unless you `.await` or poll them.
|
||||
pub async fn get(&mut self) -> T {
|
||||
poll_fn(|cx| self.watch.poll_get(&mut self.at_id, cx)).await
|
||||
pub fn get(&mut self) -> impl Future<Output = T> + '_ {
|
||||
poll_fn(|cx| self.watch.poll_get(&mut self.at_id, cx))
|
||||
}
|
||||
|
||||
/// Tries to get the current value of the `Watch` without waiting, marking it as seen.
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
//! another message will result in an error being returned.
|
||||
|
||||
use core::cell::RefCell;
|
||||
use core::future::poll_fn;
|
||||
use core::future::{poll_fn, Future};
|
||||
use core::marker::PhantomData;
|
||||
use core::task::{Context, Poll};
|
||||
|
||||
@@ -131,12 +131,15 @@ impl<'a, M: RawMutex, T> Sender<'a, M, T> {
|
||||
}
|
||||
|
||||
/// Asynchronously send a value over the channel.
|
||||
pub async fn send(&mut self) -> &mut T {
|
||||
let i = poll_fn(|cx| {
|
||||
pub fn send(&mut self) -> impl Future<Output = &mut T> {
|
||||
poll_fn(|cx| {
|
||||
self.channel.state.lock(|s| {
|
||||
let s = &mut *s.borrow_mut();
|
||||
match s.push_index() {
|
||||
Some(i) => Poll::Ready(i),
|
||||
Some(i) => {
|
||||
let r = unsafe { &mut *self.channel.buf.add(i) };
|
||||
Poll::Ready(r)
|
||||
}
|
||||
None => {
|
||||
s.receive_waker.register(cx.waker());
|
||||
Poll::Pending
|
||||
@@ -144,8 +147,6 @@ impl<'a, M: RawMutex, T> Sender<'a, M, T> {
|
||||
}
|
||||
})
|
||||
})
|
||||
.await;
|
||||
unsafe { &mut *self.channel.buf.add(i) }
|
||||
}
|
||||
|
||||
/// Notify the channel that the sending of the value has been finalized.
|
||||
@@ -213,12 +214,15 @@ impl<'a, M: RawMutex, T> Receiver<'a, M, T> {
|
||||
}
|
||||
|
||||
/// Asynchronously receive a value over the channel.
|
||||
pub async fn receive(&mut self) -> &mut T {
|
||||
let i = poll_fn(|cx| {
|
||||
pub fn receive(&mut self) -> impl Future<Output = &mut T> {
|
||||
poll_fn(|cx| {
|
||||
self.channel.state.lock(|s| {
|
||||
let s = &mut *s.borrow_mut();
|
||||
match s.pop_index() {
|
||||
Some(i) => Poll::Ready(i),
|
||||
Some(i) => {
|
||||
let r = unsafe { &mut *self.channel.buf.add(i) };
|
||||
Poll::Ready(r)
|
||||
}
|
||||
None => {
|
||||
s.send_waker.register(cx.waker());
|
||||
Poll::Pending
|
||||
@@ -226,8 +230,6 @@ impl<'a, M: RawMutex, T> Receiver<'a, M, T> {
|
||||
}
|
||||
})
|
||||
})
|
||||
.await;
|
||||
unsafe { &mut *self.channel.buf.add(i) }
|
||||
}
|
||||
|
||||
/// Notify the channel that the receiving of the value has been finalized.
|
||||
|
||||
Reference in New Issue
Block a user