mirror of
https://github.com/fafhrd91/actix-net
synced 2025-06-29 06:04:58 +02:00
refactor crates for better api stability (#301)
This commit is contained in:
7
actix-utils/src/future/mod.rs
Normal file
7
actix-utils/src/future/mod.rs
Normal file
@ -0,0 +1,7 @@
|
||||
//! Asynchronous values.
|
||||
|
||||
mod poll_fn;
|
||||
mod ready;
|
||||
|
||||
pub use self::poll_fn::{poll_fn, PollFn};
|
||||
pub use self::ready::{err, ok, ready, Ready};
|
65
actix-utils/src/future/poll_fn.rs
Normal file
65
actix-utils/src/future/poll_fn.rs
Normal file
@ -0,0 +1,65 @@
|
||||
//! Simple "poll function" future and factory.
|
||||
|
||||
use core::{
|
||||
fmt,
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
/// Create a future driven by the provided function that receives a task context.
|
||||
pub fn poll_fn<F, T>(f: F) -> PollFn<F>
|
||||
where
|
||||
F: FnMut(&mut Context<'_>) -> Poll<T>,
|
||||
{
|
||||
PollFn { f }
|
||||
}
|
||||
|
||||
/// A Future driven by the inner function.
|
||||
pub struct PollFn<F> {
|
||||
f: F,
|
||||
}
|
||||
|
||||
impl<F> Unpin for PollFn<F> {}
|
||||
|
||||
impl<F> fmt::Debug for PollFn<F> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("PollFn").finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T> Future for PollFn<F>
|
||||
where
|
||||
F: FnMut(&mut Context<'_>) -> Poll<T>,
|
||||
{
|
||||
type Output = T;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
(self.f)(cx)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_poll_fn() {
|
||||
let res = poll_fn(|_| Poll::Ready(42)).await;
|
||||
assert_eq!(res, 42);
|
||||
|
||||
let mut i = 5;
|
||||
let res = poll_fn(|cx| {
|
||||
i -= 1;
|
||||
|
||||
if i > 0 {
|
||||
cx.waker().wake_by_ref();
|
||||
Poll::Pending
|
||||
} else {
|
||||
Poll::Ready(42)
|
||||
}
|
||||
})
|
||||
.await;
|
||||
assert_eq!(res, 42);
|
||||
}
|
||||
}
|
122
actix-utils/src/future/ready.rs
Normal file
122
actix-utils/src/future/ready.rs
Normal file
@ -0,0 +1,122 @@
|
||||
//! When MSRV is 1.48, replace with `core::future::Ready` and `core::future::ready()`.
|
||||
|
||||
use core::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
/// Future for the [`ready`](ready()) function.
|
||||
///
|
||||
/// Panic will occur if polled more than once.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// use actix_utils::future::ready;
|
||||
///
|
||||
/// // async
|
||||
/// # async fn run() {
|
||||
/// let a = ready(1);
|
||||
/// assert_eq!(a.await, 1);
|
||||
/// # }
|
||||
///
|
||||
/// // sync
|
||||
/// let a = ready(1);
|
||||
/// assert_eq!(a.into_inner(), 1);
|
||||
/// ```
|
||||
#[derive(Debug, Clone)]
|
||||
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
||||
pub struct Ready<T> {
|
||||
val: Option<T>,
|
||||
}
|
||||
|
||||
impl<T> Ready<T> {
|
||||
/// Unwraps the value from this immediately ready future.
|
||||
#[inline]
|
||||
pub fn into_inner(mut self) -> T {
|
||||
self.val.take().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Unpin for Ready<T> {}
|
||||
|
||||
impl<T> Future for Ready<T> {
|
||||
type Output = T;
|
||||
|
||||
#[inline]
|
||||
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> {
|
||||
let val = self.val.take().expect("Ready polled after completion");
|
||||
Poll::Ready(val)
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a future that is immediately ready with a value.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```no_run
|
||||
/// use actix_utils::future::ready;
|
||||
///
|
||||
/// # async fn run() {
|
||||
/// let a = ready(1);
|
||||
/// assert_eq!(a.await, 1);
|
||||
/// # }
|
||||
///
|
||||
/// // sync
|
||||
/// let a = ready(1);
|
||||
/// assert_eq!(a.into_inner(), 1);
|
||||
/// ```
|
||||
pub fn ready<T>(val: T) -> Ready<T> {
|
||||
Ready { val: Some(val) }
|
||||
}
|
||||
|
||||
/// Create a future that is immediately ready with a success value.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```no_run
|
||||
/// use actix_utils::future::ok;
|
||||
///
|
||||
/// # async fn run() {
|
||||
/// let a = ok::<_, ()>(1);
|
||||
/// assert_eq!(a.await, Ok(1));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn ok<T, E>(val: T) -> Ready<Result<T, E>> {
|
||||
Ready { val: Some(Ok(val)) }
|
||||
}
|
||||
|
||||
/// Create a future that is immediately ready with an error value.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```no_run
|
||||
/// use actix_utils::future::err;
|
||||
///
|
||||
/// # async fn run() {
|
||||
/// let a = err::<(), _>(1);
|
||||
/// assert_eq!(a.await, Err(1));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn err<T, E>(err: E) -> Ready<Result<T, E>> {
|
||||
Ready {
|
||||
val: Some(Err(err)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use futures_util::task::noop_waker;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn multiple_poll_panics() {
|
||||
let waker = noop_waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
|
||||
let mut ready = ready(1);
|
||||
assert_eq!(Pin::new(&mut ready).poll(&mut cx), Poll::Ready(1));
|
||||
|
||||
// panic!
|
||||
let _ = Pin::new(&mut ready).poll(&mut cx);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user