From 4b1a385aa14724c1696adc89f3399264d283617c Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Wed, 11 Sep 2024 13:48:40 -0700 Subject: [PATCH] style: use core `ready`, `poll_fn`, and `pin!` (#331) Currently, we use the `future::ready` and `future::poll_fn` functions and the `pin_mut!` macro from the `futures` crate. The equivalent APIs in libcore have stabilized, so we can use them instead. Because we use a bunch of other stuff from `futures` --- in particular, `futures::select!` --- this doesn't allow us to reduce our dependency footprint by removing dependencies on `futures`. But, IMO, it feels like sligly better style to use the standard library versions of common APIs when it's possible to do so. Also, I think the `core::pin::pin!` macro has a somewhat nicer API than `futures::pin_mut!` since it allows pinning the expression on the left-hand side of a `let` binding directly, rather than having to assign the value to a name and *then* pin it: ```rust // Personally, I think this: let mut pinned_fut = core::pin::pin!(future::ready()); pinned_fut.await; // ...looks nicer than this: let pinned_fut = future::ready(); pin_mut!(pinned_fut); pinned_fut.await; ``` --- platforms/allwinner-d1/d1-core/src/dmac/mod.rs | 4 ++-- platforms/allwinner-d1/d1-core/src/drivers/smhc.rs | 3 ++- platforms/allwinner-d1/d1-core/src/drivers/twi.rs | 3 ++- source/forth3/src/testutil/mod.rs | 2 +- source/mstd/src/executor/mailbox.rs | 4 +--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/platforms/allwinner-d1/d1-core/src/dmac/mod.rs b/platforms/allwinner-d1/d1-core/src/dmac/mod.rs index 4eb8e501..83c1d53c 100644 --- a/platforms/allwinner-d1/d1-core/src/dmac/mod.rs +++ b/platforms/allwinner-d1/d1-core/src/dmac/mod.rs @@ -2,6 +2,7 @@ #![warn(missing_docs)] use core::{ fmt, + pin::pin, ptr::NonNull, sync::atomic::{fence, Ordering}, }; @@ -261,8 +262,7 @@ impl Dmac { loop { // if no channel was available, register our waker and try again. - let wait = STATE.claim_wait.wait(); - futures::pin_mut!(wait); + let mut wait = pin!(STATE.claim_wait.wait()); // ensure the `WaitQueue` entry is registered before we actually // check the claim state. let _ = wait.as_mut().subscribe(); diff --git a/platforms/allwinner-d1/d1-core/src/drivers/smhc.rs b/platforms/allwinner-d1/d1-core/src/drivers/smhc.rs index 1a41e9a9..e7ab598e 100644 --- a/platforms/allwinner-d1/d1-core/src/drivers/smhc.rs +++ b/platforms/allwinner-d1/d1-core/src/drivers/smhc.rs @@ -12,6 +12,7 @@ //! the transfer and reception of large amounts of data to/from the device. use core::{ cell::UnsafeCell, + future, ops::{Deref, DerefMut}, task::{Poll, Waker}, }; @@ -553,7 +554,7 @@ impl Drop for SmhcDataGuard<'_> { impl SmhcDataGuard<'_> { async fn wait_for_irq(&mut self) { let mut waiting = false; - futures::future::poll_fn(|cx| { + future::poll_fn(|cx| { if waiting { self.smhc.smhc_ctrl.modify(|_, w| w.ine_enb().disable()); return Poll::Ready(()); diff --git a/platforms/allwinner-d1/d1-core/src/drivers/twi.rs b/platforms/allwinner-d1/d1-core/src/drivers/twi.rs index 63a11cc6..64edd0ac 100644 --- a/platforms/allwinner-d1/d1-core/src/drivers/twi.rs +++ b/platforms/allwinner-d1/d1-core/src/drivers/twi.rs @@ -53,6 +53,7 @@ //! [linux-driver]: https://github.com/torvalds/linux/blob/995b406c7e972fab181a4bb57f3b95e59b8e5bf3/drivers/i2c/busses/i2c-mv64xxx.c use core::{ cell::UnsafeCell, + future, ops::{Deref, DerefMut}, task::{Poll, Waker}, }; @@ -431,7 +432,7 @@ impl Drop for TwiDataGuard<'_> { impl TwiDataGuard<'_> { async fn wait_for_irq(&mut self) { let mut waiting = false; - futures::future::poll_fn(|cx| { + future::poll_fn(|cx| { if waiting { self.twi.twi_cntr.modify(|_r, w| w.int_en().low()); return Poll::Ready(()); diff --git a/source/forth3/src/testutil/mod.rs b/source/forth3/src/testutil/mod.rs index 70b76df0..9682a871 100644 --- a/source/forth3/src/testutil/mod.rs +++ b/source/forth3/src/testutil/mod.rs @@ -101,7 +101,7 @@ pub fn async_blockon_runtest(contents: &str) { struct TestAsyncDispatcher; impl<'forth> AsyncBuiltins<'forth, ()> for TestAsyncDispatcher { - type Future = futures::future::Ready>; + type Future = core::future::Ready>; const BUILTINS: &'static [AsyncBuiltinEntry<()>] = &[]; fn dispatch_async(&self, _id: &FaStr, _forth: &'forth mut Forth<()>) -> Self::Future { unreachable!("no async builtins should be called in this test") diff --git a/source/mstd/src/executor/mailbox.rs b/source/mstd/src/executor/mailbox.rs index 391badbd..2d34d17b 100644 --- a/source/mstd/src/executor/mailbox.rs +++ b/source/mstd/src/executor/mailbox.rs @@ -47,7 +47,6 @@ use abi::{ UserRequestHeader, }, }; -use futures_util::pin_mut; use maitake::sync::{ wait_map::{self, WaitMap}, WaitQueue, @@ -143,8 +142,7 @@ impl MailBox { let nonce = self.nonce.fetch_add(1, Ordering::AcqRel); // Start listening for the response BEFORE we send the request - let rx: wait_map::Wait = MAILBOX.recv_wait.wait(nonce); - pin_mut!(rx); + let mut rx = core::pin::pin!(MAILBOX.recv_wait.wait(nonce)); rx.as_mut().enqueue().await.map_err(drop)?; self.send_inner(nonce, msg).await?;