Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some functions in future module to no_std #696

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,14 @@

cfg_alloc! {
pub use future::Future;
pub(crate) mod future;
}

cfg_std! {
pub use ready::ready;
pub use pending::pending;
pub use poll_fn::poll_fn;
pub use ready::ready;

pub(crate) mod future;
mod ready;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe ready should be able to work in no-std.

mod pending;
mod poll_fn;
mod ready;
}

cfg_default! {
Expand Down
19 changes: 11 additions & 8 deletions src/future/pending.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use core::future::Future;
use core::marker::PhantomData;
use core::pin::Pin;

use crate::task::{Context, Poll};

Expand All @@ -24,14 +24,17 @@ use crate::task::{Context, Poll};
/// #
/// # })
/// ```
pub async fn pending<T>() -> T {
let fut = Pending {
pub fn pending<T>() -> Pending<T> {
Pending {
_marker: PhantomData,
};
fut.await
}
}

struct Pending<T> {
/// This future is constructed by the [`pending`] function.
///
/// [`pending`]: fn.pending.html
#[derive(Debug)]
pub struct Pending<T> {
_marker: PhantomData<T>,
}

Expand Down
15 changes: 9 additions & 6 deletions src/future/poll_fn.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::pin::Pin;
use std::future::Future;
use core::future::Future;
use core::pin::Pin;

use crate::task::{Context, Poll};

Expand All @@ -23,15 +23,18 @@ use crate::task::{Context, Poll};
/// #
/// # })
/// ```
pub async fn poll_fn<F, T>(f: F) -> T
k-nasa marked this conversation as resolved.
Show resolved Hide resolved
pub fn poll_fn<F, T>(f: F) -> PollFn<F>
Copy link
Contributor

@yoshuawuyts yoshuawuyts Mar 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm so-so about this change. Functions such as io::copy don't work on no-std either, but would be really useful to have as well. Having these functions be async fn was a bit of a deliberate choice when designing async-std.

With async await for embedded rust steadily progressing, I wonder how needed this is. It seems as async/await continues to move beyond mvp status this will be much of a non-issue.

All in all I think it'd be okay to leave these methods as-is for now. Thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, the feature won't make the cut in 6 days, so it's at least 18 weeks out. Given that this is no user-visible change, I'd be fine with enabling embrio users to use this currently.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the interface seen from the user does not change, I think that it is OK to change this.

where
F: FnMut(&mut Context<'_>) -> Poll<T>,
{
let fut = PollFn { f };
fut.await
PollFn { f }
}

struct PollFn<F> {
/// This future is constructed by the [`poll_fn`] function.
///
/// [`poll_fn`]: fn.poll_fn.html
#[derive(Debug)]
pub struct PollFn<F> {
f: F,
}

Expand Down
25 changes: 23 additions & 2 deletions src/future/ready.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use core::future::Future;
use core::pin::Pin;

use crate::task::{Context, Poll};

/// Resolves to the provided value.
///
/// This function is an async version of [`std::convert::identity`].
Expand All @@ -15,6 +20,22 @@
/// #
/// # })
/// ```
pub async fn ready<T>(val: T) -> T {
val
pub fn ready<T>(val: T) -> Ready<T> {
Ready(Some(val))
}

/// This future is constructed by the [`ready`] function.
///
/// [`ready`]: fn.ready.html
#[derive(Debug)]
pub struct Ready<T>(Option<T>);

impl<T> Unpin for Ready<T> {}

impl<T> Future for Ready<T> {
type Output = T;

fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<T> {
Poll::Ready(self.0.take().unwrap())
}
}