2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-04-08 17:36:43 +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! {
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"))]

View file

@ -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>,
}

View file

@ -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,
}

View file

@ -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())
}
}