2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-04-23 08:46:46 +00:00
This commit is contained in:
nasa 2025-03-16 10:05:18 +09:00 committed by GitHub
commit b8ffd172df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 46 additions and 22 deletions

View file

@ -48,17 +48,14 @@
cfg_alloc! { cfg_alloc! {
pub use future::Future; pub use future::Future;
pub(crate) mod future; pub use ready::ready;
}
cfg_std! {
pub use pending::pending; pub use pending::pending;
pub use poll_fn::poll_fn; pub use poll_fn::poll_fn;
pub use ready::ready;
pub(crate) mod future;
mod ready;
mod pending; mod pending;
mod poll_fn; mod poll_fn;
mod ready;
} }
#[cfg(any(feature = "unstable", feature = "default"))] #[cfg(any(feature = "unstable", feature = "default"))]

View file

@ -1,6 +1,6 @@
use std::future::Future; use core::future::Future;
use std::marker::PhantomData; use core::marker::PhantomData;
use std::pin::Pin; use core::pin::Pin;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
@ -24,14 +24,17 @@ use crate::task::{Context, Poll};
/// # /// #
/// # }) /// # })
/// ``` /// ```
pub async fn pending<T>() -> T { pub fn pending<T>() -> Pending<T> {
let fut = Pending { Pending {
_marker: PhantomData, _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>, _marker: PhantomData<T>,
} }

View file

@ -1,5 +1,5 @@
use std::pin::Pin; use core::future::Future;
use std::future::Future; use core::pin::Pin;
use crate::task::{Context, Poll}; 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 where
F: FnMut(&mut Context<'_>) -> Poll<T>, F: FnMut(&mut Context<'_>) -> Poll<T>,
{ {
let fut = PollFn { f }; PollFn { f }
fut.await
} }
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, f: F,
} }

View file

@ -1,3 +1,8 @@
use core::future::Future;
use core::pin::Pin;
use crate::task::{Context, Poll};
/// Resolves to the provided value. /// Resolves to the provided value.
/// ///
/// This function is an async version of [`std::convert::identity`]. /// This function is an async version of [`std::convert::identity`].
@ -15,6 +20,22 @@
/// # /// #
/// # }) /// # })
/// ``` /// ```
pub async fn ready<T>(val: T) -> T { pub fn ready<T>(val: T) -> Ready<T> {
val 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())
}
} }