Skip to content

Commit

Permalink
style: use core ready, poll_fn, and pin! (#331)
Browse files Browse the repository at this point in the history
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;
```
  • Loading branch information
hawkw authored Sep 11, 2024
1 parent 909b964 commit 4b1a385
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions platforms/allwinner-d1/d1-core/src/dmac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![warn(missing_docs)]
use core::{
fmt,
pin::pin,
ptr::NonNull,
sync::atomic::{fence, Ordering},
};
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion platforms/allwinner-d1/d1-core/src/drivers/smhc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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(());
Expand Down
3 changes: 2 additions & 1 deletion platforms/allwinner-d1/d1-core/src/drivers/twi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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(());
Expand Down
2 changes: 1 addition & 1 deletion source/forth3/src/testutil/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn async_blockon_runtest(contents: &str) {

struct TestAsyncDispatcher;
impl<'forth> AsyncBuiltins<'forth, ()> for TestAsyncDispatcher {
type Future = futures::future::Ready<Result<(), Error>>;
type Future = core::future::Ready<Result<(), Error>>;
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")
Expand Down
4 changes: 1 addition & 3 deletions source/mstd/src/executor/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ use abi::{
UserRequestHeader,
},
};
use futures_util::pin_mut;
use maitake::sync::{
wait_map::{self, WaitMap},
WaitQueue,
Expand Down Expand Up @@ -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<u32, KernelResponseBody> = 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?;

Expand Down

0 comments on commit 4b1a385

Please sign in to comment.