From 26ec7406772d6348952d5e0309f7e25e57ae9320 Mon Sep 17 00:00:00 2001 From: kellerkindt Date: Sun, 30 Jun 2019 15:16:32 +0200 Subject: [PATCH 1/5] Add block_while! macro block_while! blocks as for the operation to finish as long as the expression $c evaluates to true. --- src/lib.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7f0d386..b9aa122 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -472,6 +472,45 @@ macro_rules! block { } } +/// Turns the non-blocking expression `$e` into a blocking operation for as long +/// as the given expression evaluates to true. +/// +/// This is accomplished by continuously calling the expression `$e` until it no +/// longer returns `Error::WouldBlock` and by calling expression `$c` to evaluate +/// whether to keep polling. If `$c` evaluates to false and `$e` evaluates to +/// `Error::WouldBlock`, `Err(nb::Error::WouldBlock)` is returned. +/// +/// # Input +/// +/// An expression `$c` that evaluates to `bool` +/// An expression `$e` that evaluates to `nb::Result` +/// +/// # Output +/// +/// - `Ok(t)` if `$e` evaluates to `Ok(t)` +/// - `Err(e)` if `$e` evaluates to `Err(nb::Error::Other(e))` +/// - `Err(Error::WouldBlock)` if `$e` evaluates to `Err(Error::WouldBlock)` and `$c` evaluates to false +#[macro_export] +macro_rules! block_while { + ($c:expr, $e:expr) => { + loop { + #[allow(unreachable_patterns)] + match $e { + Err($crate::Error::Other(e)) => { + #[allow(unreachable_code)] + break Err(e) + }, + Err($crate::Error::WouldBlock) => { + if !$c { + break Err($crate::Error::WouldBlock); + } + }, + Ok(x) => break Ok(x), + } + } + } +} + /// Future adapter /// /// This is a *try* operation from a `nb::Result` to a `futures::Poll` From e274cea749ff50656f566ac1a7cc110d5e16e424 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Sun, 30 Jun 2019 15:32:18 +0200 Subject: [PATCH 2/5] Let block_while! return Result> --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b9aa122..3cdf1b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -488,7 +488,7 @@ macro_rules! block { /// # Output /// /// - `Ok(t)` if `$e` evaluates to `Ok(t)` -/// - `Err(e)` if `$e` evaluates to `Err(nb::Error::Other(e))` +/// - `Err(nb::Error::Other(e))` if `$e` evaluates to `Err(nb::Error::Other(e))` /// - `Err(Error::WouldBlock)` if `$e` evaluates to `Err(Error::WouldBlock)` and `$c` evaluates to false #[macro_export] macro_rules! block_while { @@ -498,7 +498,7 @@ macro_rules! block_while { match $e { Err($crate::Error::Other(e)) => { #[allow(unreachable_code)] - break Err(e) + break Err($crate::Error::Other(e)) }, Err($crate::Error::WouldBlock) => { if !$c { From 3999067537063f5f022c88478a894f0a6030f754 Mon Sep 17 00:00:00 2001 From: kellerkindt Date: Sat, 21 Nov 2020 22:54:01 +0100 Subject: [PATCH 3/5] Remove try_nb that did sneak in through the merge --- src/lib.rs | 39 --------------------------------------- 1 file changed, 39 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3f32240..60ba551 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -304,42 +304,3 @@ macro_rules! block_while { } } } - -/// Future adapter -/// -/// This is a *try* operation from a `nb::Result` to a `futures::Poll` -/// -/// # Requirements -/// -/// This macro must be called within a function / closure that has signature -/// `fn(..) -> futures::Poll`. -/// -/// This macro requires that the [`futures`] crate is in the root of the crate. -/// -/// [`futures`]: https://crates.io/crates/futures -/// -/// # Input -/// -/// An expression `$e` that evaluates to `nb::Result` -/// -/// # Early return -/// -/// - `Ok(Async::NotReady)` if `$e` evaluates to `Err(nb::Error::WouldBlock)` -/// - `Err(e)` if `$e` evaluates to `Err(nb::Error::Other(e))` -/// -/// # Output -/// -/// `t` if `$e` evaluates to `Ok(t)` -#[cfg(feature = "unstable")] -#[macro_export] -macro_rules! try_nb { - ($e:expr) => { - match $e { - Err($crate::Error::Other(e)) => return Err(e), - Err($crate::Error::WouldBlock) => { - return Ok(::futures::Async::NotReady) - }, - Ok(x) => x, - } - }; -} From 791ac908f3e30b7cfe4cac1144d3c6244a8cd5b4 Mon Sep 17 00:00:00 2001 From: kellerkindt Date: Sat, 21 Nov 2020 22:54:57 +0100 Subject: [PATCH 4/5] Add missing semicolon --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 60ba551..e216f53 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -263,7 +263,7 @@ macro_rules! block { Ok(x) => break Ok(x), } } - } + }; } /// Turns the non-blocking expression `$e` into a blocking operation for as long @@ -302,5 +302,5 @@ macro_rules! block_while { Ok(x) => break Ok(x), } } - } + }; } From f3311ae27fd445ec542f28ef12efe75f53137d27 Mon Sep 17 00:00:00 2001 From: kellerkindt Date: Sat, 21 Nov 2020 23:17:06 +0100 Subject: [PATCH 5/5] cargo fmt --- src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e216f53..4c754b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -290,15 +290,16 @@ macro_rules! block_while { loop { #[allow(unreachable_patterns)] match $e { - Err($crate::Error::Other(e)) => { + Err($crate::Error::Other(e)) => + { #[allow(unreachable_code)] break Err($crate::Error::Other(e)) - }, + } Err($crate::Error::WouldBlock) => { if !$c { break Err($crate::Error::WouldBlock); } - }, + } Ok(x) => break Ok(x), } }