mirror of
https://github.com/async-rs/async-std.git
synced 2025-04-08 17:36:43 +00:00
Merge 3883079fa5
into 96f564672a
This commit is contained in:
commit
b8ffd172df
4 changed files with 46 additions and 22 deletions
|
@ -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;
|
||||
mod pending;
|
||||
mod poll_fn;
|
||||
mod ready;
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "unstable", feature = "default"))]
|
||||
|
|
|
@ -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};
|
||||
|
||||
|
@ -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>,
|
||||
}
|
||||
|
||||
|
|
|
@ -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};
|
||||
|
||||
|
@ -23,15 +23,18 @@ use crate::task::{Context, Poll};
|
|||
/// #
|
||||
/// # })
|
||||
/// ```
|
||||
pub async fn poll_fn<F, T>(f: F) -> T
|
||||
pub fn poll_fn<F, T>(f: F) -> PollFn<F>
|
||||
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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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`].
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue