From b5b5263b3c55e14801e2c5d507c6d9a0782167f5 Mon Sep 17 00:00:00 2001 From: Caleb Leinz Date: Mon, 14 Oct 2024 10:49:07 -0700 Subject: [PATCH] refactor: Move RaceBiased to separate module --- src/executor.rs | 5 ++- src/futures.rs | 81 ++------------------------------------------- src/futures/race.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 80 deletions(-) create mode 100644 src/futures/race.rs diff --git a/src/executor.rs b/src/executor.rs index b4671af..1c85282 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -48,7 +48,10 @@ impl Context { /// /// A common pattern is to spawn the executor onto an async runtime like tokio. /// -/// ```ignore +/// ```no_run +/// # use black_box::*; +/// # struct MyActor; +/// # impl Actor for MyActor {} /// let my_actor = MyActor; /// let (executor, addr) = Executor::new(my_actor); /// diff --git a/src/futures.rs b/src/futures.rs index 9212eed..b846ca8 100644 --- a/src/futures.rs +++ b/src/futures.rs @@ -1,80 +1,3 @@ -use std::{future::Future, task::Poll}; +mod race; -use pin_project_lite::pin_project; - -/// Race two futures against one another, favoring the first future over the second -pub fn race_biased(first: F1, second: F2) -> RaceBiased { - RaceBiased { first, second } -} - -pin_project! { - pub struct RaceBiased { - #[pin] - first: F1, - #[pin] - second: F2, - } -} - -impl Future for RaceBiased -where - F1: Future, - F2: Future, -{ - type Output = T; - - fn poll( - self: std::pin::Pin<&mut Self>, - cx: &mut std::task::Context<'_>, - ) -> std::task::Poll { - let this = self.project(); - - if let Poll::Ready(val) = this.first.poll(cx) { - Poll::Ready(val) - } else if let Poll::Ready(val) = this.second.poll(cx) { - Poll::Ready(val) - } else { - Poll::Pending - } - } -} - -#[cfg(test)] -mod tests { - use std::time::Duration; - - use super::*; - - #[tokio::test] - async fn race_biased_on_ready() { - let first = std::future::ready(true); - let second = std::future::ready(false); - let result = race_biased(first, second).await; - assert!(result); - } - - #[tokio::test] - async fn race_biased_on_not_ready() { - let first = async { - tokio::time::sleep(Duration::from_millis(250)).await; - false - }; - let second = async { - tokio::time::sleep(Duration::from_millis(100)).await; - true - }; - let result = race_biased(first, second).await; - assert!(result); - } - - #[tokio::test] - async fn race_biased_on_one_ready() { - let first = async { - tokio::time::sleep(Duration::from_millis(250)).await; - false - }; - let second = std::future::ready(true); - let result = race_biased(first, second).await; - assert!(result); - } -} +pub use race::race_biased; diff --git a/src/futures/race.rs b/src/futures/race.rs new file mode 100644 index 0000000..b5eda7a --- /dev/null +++ b/src/futures/race.rs @@ -0,0 +1,80 @@ +use std::{future::Future, task::Poll}; + +use pin_project_lite::pin_project; + +/// Race two futures against one another, favoring the first future over the second +pub fn race_biased(first: F1, second: F2) -> RaceBiased { + RaceBiased { first, second } +} + +pin_project! { + pub struct RaceBiased { + #[pin] + first: F1, + #[pin] + second: F2, + } +} + +impl Future for RaceBiased +where + F1: Future, + F2: Future, +{ + type Output = T; + + fn poll( + self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll { + let this = self.project(); + + if let Poll::Ready(val) = this.first.poll(cx) { + Poll::Ready(val) + } else if let Poll::Ready(val) = this.second.poll(cx) { + Poll::Ready(val) + } else { + Poll::Pending + } + } +} + +#[cfg(test)] +mod tests { + use std::time::Duration; + + use super::*; + + #[tokio::test] + async fn race_biased_on_ready() { + let first = std::future::ready(true); + let second = std::future::ready(false); + let result = race_biased(first, second).await; + assert!(result); + } + + #[tokio::test] + async fn race_biased_on_not_ready() { + let first = async { + tokio::time::sleep(Duration::from_millis(100)).await; + false + }; + let second = async { + tokio::time::sleep(Duration::from_millis(25)).await; + true + }; + let result = race_biased(first, second).await; + assert!(result); + } + + #[tokio::test] + async fn race_biased_on_one_ready() { + let first = async { + tokio::time::sleep(Duration::from_millis(250)).await; + false + }; + let second = std::future::ready(true); + let result = race_biased(first, second).await; + assert!(result); + } +}