Skip to content

Commit dd3ab70

Browse files
committed
add future support
1 parent 075ecd3 commit dd3ab70

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ documentation = "https://docs.rs/nb"
1111
readme = "README.md"
1212
version = "1.0.0" # remember to update html_root_url
1313
edition = "2018"
14+
15+
[dependencies]
16+
pin-project-lite = "0.2.8"

src/lib.rs

+48
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@
187187
#![doc(html_root_url = "https://docs.rs/nb/1.0.0")]
188188

189189
use core::fmt;
190+
use core::future::Future;
191+
use core::pin::Pin;
192+
use core::task::{Context, Poll};
193+
use pin_project_lite::pin_project;
190194

191195
/// A non-blocking result
192196
pub type Result<T, E> = ::core::result::Result<T, Error<E>>;
@@ -265,3 +269,47 @@ macro_rules! block {
265269
}
266270
};
267271
}
272+
pin_project! {
273+
pub struct NbFuture<Ok, Err, Gen: FnMut() -> Result<Ok, Err>> {
274+
gen: Gen,
275+
}
276+
}
277+
278+
impl<Ok, Err, Gen: FnMut() -> Result<Ok, Err>> From<Gen> for NbFuture<Ok, Err, Gen> {
279+
fn from(gen: Gen) -> Self {
280+
Self { gen }
281+
}
282+
}
283+
284+
impl<Ok, Err, Gen: FnMut() -> Result<Ok, Err>> Future for NbFuture<Ok, Err, Gen> {
285+
type Output = core::result::Result<Ok, Err>;
286+
287+
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
288+
let this = self.project();
289+
let res = (this.gen)();
290+
291+
match res {
292+
Ok(res) => Poll::Ready(Ok(res)),
293+
Err(Error::WouldBlock) => Poll::Pending,
294+
Err(Error::Other(err)) => Poll::Ready(Err(err)),
295+
}
296+
}
297+
}
298+
299+
/// The equivalent of [block], expect instead of blocking this creates a
300+
/// [Future] which can `.await`ed.
301+
///
302+
/// # Input
303+
///
304+
/// An expression `$e` that evaluates to `nb::Result<T, E>`
305+
///
306+
/// # Output
307+
///
308+
/// - `Ok(t)` if `$e` evaluates to `Ok(t)`
309+
/// - `Err(e)` if `$e` evaluates to `Err(nb::Error::Other(e))`
310+
#[macro_export]
311+
macro_rules! fut {
312+
($call:expr) => {{
313+
nb::NbFuture::from(|| $call)
314+
}};
315+
}

0 commit comments

Comments
 (0)