Desugar some async fns

This commit is contained in:
Dániel Buga
2024-12-30 12:13:13 +01:00
parent a4f8fddd69
commit 44217aa092
18 changed files with 103 additions and 124 deletions

View File

@@ -9,7 +9,7 @@
//! Please also see [crate::uarte] to understand when [BufferedUarte] should be used.
use core::cmp::min;
use core::future::poll_fn;
use core::future::{poll_fn, Future};
use core::marker::PhantomData;
use core::slice;
use core::sync::atomic::{compiler_fence, AtomicBool, AtomicU8, AtomicUsize, Ordering};
@@ -452,7 +452,7 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> {
}
/// Write a buffer into this writer, returning how many bytes were written.
pub async fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
pub fn write<'a>(&'a mut self, buf: &'a [u8]) -> impl Future<Output = Result<usize, Error>> + 'a {
poll_fn(move |cx| {
//trace!("poll_write: {:?}", buf.len());
let ss = U::state();
@@ -477,7 +477,6 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> {
Poll::Ready(Ok(n))
})
.await
}
/// Try writing a buffer without waiting, returning how many bytes were written.
@@ -504,7 +503,7 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> {
}
/// Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
pub async fn flush(&mut self) -> Result<(), Error> {
pub fn flush(&mut self) -> impl Future<Output = Result<(), Error>> + '_ {
poll_fn(move |cx| {
//trace!("poll_flush");
let ss = U::state();
@@ -517,7 +516,6 @@ impl<'d, U: UarteInstance> BufferedUarteTx<'d, U> {
Poll::Ready(Ok(()))
})
.await
}
}
@@ -721,7 +719,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> {
}
/// Return the contents of the internal buffer, filling it with more data from the inner reader if it is empty.
pub async fn fill_buf(&mut self) -> Result<&[u8], Error> {
pub fn fill_buf(&mut self) -> impl Future<Output = Result<&'_ [u8], Error>> {
poll_fn(move |cx| {
compiler_fence(Ordering::SeqCst);
//trace!("poll_read");
@@ -771,7 +769,6 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarteRx<'d, U, T> {
let buf = s.rx_buf.buf.load(Ordering::Relaxed);
Poll::Ready(Ok(unsafe { slice::from_raw_parts(buf.add(start), n) }))
})
.await
}
/// Tell this buffer that `amt` bytes have been consumed from the buffer, so they should no longer be returned in calls to `fill_buf`.

View File

@@ -2,7 +2,7 @@
#![macro_use]
use core::future::poll_fn;
use core::future::{poll_fn, Future};
use core::marker::PhantomData;
use core::ptr;
use core::task::Poll;
@@ -314,7 +314,7 @@ impl<'d, T: Instance> Qspi<'d, T> {
Ok(())
}
async fn wait_ready(&mut self) {
fn wait_ready(&mut self) -> impl Future<Output = ()> {
poll_fn(move |cx| {
let r = T::regs();
let s = T::state();
@@ -324,7 +324,6 @@ impl<'d, T: Instance> Qspi<'d, T> {
}
Poll::Pending
})
.await
}
fn blocking_wait_ready() {

View File

@@ -4,7 +4,7 @@
pub mod vbus_detect;
use core::future::poll_fn;
use core::future::{poll_fn, Future};
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use core::sync::atomic::{compiler_fence, AtomicU32, Ordering};
@@ -219,8 +219,8 @@ impl<'d, T: Instance, V: VbusDetect> driver::Bus for Bus<'d, T, V> {
regs.enable().write(|x| x.set_enable(false));
}
async fn poll(&mut self) -> Event {
poll_fn(move |cx| {
fn poll(&mut self) -> impl Future<Output = Event> {
poll_fn(|cx| {
BUS_WAKER.register(cx.waker());
let regs = T::regs();
@@ -277,7 +277,6 @@ impl<'d, T: Instance, V: VbusDetect> driver::Bus for Bus<'d, T, V> {
Poll::Pending
})
.await
}
fn endpoint_set_stalled(&mut self, ep_addr: EndpointAddress, stalled: bool) {
@@ -468,7 +467,7 @@ impl<'d, T: Instance, Dir: EndpointDir> driver::Endpoint for Endpoint<'d, T, Dir
#[allow(private_bounds)]
impl<'d, T: Instance, Dir: EndpointDir> Endpoint<'d, T, Dir> {
async fn wait_enabled_state(&mut self, state: bool) {
fn wait_enabled_state(&mut self, state: bool) -> impl Future<Output = ()> {
let i = self.info.addr.index();
assert!(i != 0);
@@ -480,12 +479,11 @@ impl<'d, T: Instance, Dir: EndpointDir> Endpoint<'d, T, Dir> {
Poll::Pending
}
})
.await
}
/// Wait for the endpoint to be disabled
pub async fn wait_disabled(&mut self) {
self.wait_enabled_state(false).await
pub fn wait_disabled(&mut self) -> impl Future<Output = ()> {
self.wait_enabled_state(false)
}
}

View File

@@ -1,6 +1,6 @@
//! Trait and implementations for performing VBUS detection.
use core::future::poll_fn;
use core::future::{poll_fn, Future};
use core::sync::atomic::{AtomicBool, Ordering};
use core::task::Poll;
@@ -99,8 +99,8 @@ impl VbusDetect for HardwareVbusDetect {
regs.usbregstatus().read().vbusdetect()
}
async fn wait_power_ready(&mut self) -> Result<(), ()> {
poll_fn(move |cx| {
fn wait_power_ready(&mut self) -> impl Future<Output = Result<(), ()>> {
poll_fn(|cx| {
POWER_WAKER.register(cx.waker());
let regs = USB_REG_PERI;
@@ -112,7 +112,6 @@ impl VbusDetect for HardwareVbusDetect {
Poll::Pending
}
})
.await
}
}
@@ -163,7 +162,7 @@ impl VbusDetect for &SoftwareVbusDetect {
self.usb_detected.load(Ordering::Relaxed)
}
async fn wait_power_ready(&mut self) -> Result<(), ()> {
fn wait_power_ready(&mut self) -> impl Future<Output = Result<(), ()>> {
poll_fn(move |cx| {
POWER_WAKER.register(cx.waker());
@@ -175,6 +174,5 @@ impl VbusDetect for &SoftwareVbusDetect {
Poll::Pending
}
})
.await
}
}