From 849d827486c75b2ab223d7b0e638dbb5b74d4d1d Mon Sep 17 00:00:00 2001 From: hozan23 Date: Thu, 9 Nov 2023 11:38:19 +0300 Subject: rename crates --- karyons_core/src/async_utils/select.rs | 99 ---------------------------------- 1 file changed, 99 deletions(-) delete mode 100644 karyons_core/src/async_utils/select.rs (limited to 'karyons_core/src/async_utils/select.rs') diff --git a/karyons_core/src/async_utils/select.rs b/karyons_core/src/async_utils/select.rs deleted file mode 100644 index d61b355..0000000 --- a/karyons_core/src/async_utils/select.rs +++ /dev/null @@ -1,99 +0,0 @@ -use std::pin::Pin; -use std::task::{Context, Poll}; - -use pin_project_lite::pin_project; -use smol::future::Future; - -/// Returns the result of the future that completes first, preferring future1 -/// if both are ready. -/// -/// # Examples -/// -/// ``` -/// use std::future; -/// -/// use karyons_core::async_utils::{select, Either}; -/// -/// async { -/// let fut1 = future::pending::(); -/// let fut2 = future::ready(0); -/// let res = select(fut1, fut2).await; -/// assert!(matches!(res, Either::Right(0))); -/// // .... -/// }; -/// -/// ``` -/// -pub fn select(future1: F1, future2: F2) -> Select -where - F1: Future, - F2: Future, -{ - Select { future1, future2 } -} - -pin_project! { - #[derive(Debug)] - pub struct Select { - #[pin] - future1: F1, - #[pin] - future2: F2, - } -} - -/// The return value from the `select` function, indicating which future -/// completed first. -#[derive(Debug)] -pub enum Either { - Left(T1), - Right(T2), -} - -// Implement the Future trait for the Select struct. -impl Future for Select -where - F1: Future, - F2: Future, -{ - type Output = Either; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.project(); - - if let Poll::Ready(t) = this.future1.poll(cx) { - return Poll::Ready(Either::Left(t)); - } - - if let Poll::Ready(t) = this.future2.poll(cx) { - return Poll::Ready(Either::Right(t)); - } - - Poll::Pending - } -} - -#[cfg(test)] -mod tests { - use super::{select, Either}; - use smol::Timer; - use std::future; - - #[test] - fn test_async_select() { - smol::block_on(async move { - let fut = select(Timer::never(), future::ready(0 as u32)).await; - assert!(matches!(fut, Either::Right(0))); - - let fut1 = future::pending::(); - let fut2 = future::ready(0); - let res = select(fut1, fut2).await; - assert!(matches!(res, Either::Right(0))); - - let fut1 = future::ready(0); - let fut2 = future::pending::(); - let res = select(fut1, fut2).await; - assert!(matches!(res, Either::Left(_))); - }); - } -} -- cgit v1.2.3