futures-ext: add ensure_boxfuture! macro similar to failure's ensure!

Summary: As in the comment to the macro: if the condition is not met, return a BoxFuture with Err inside

Reviewed By: farnz

Differential Revision: D8877731

fbshipit-source-id: 7f31a1739155201ea2be30901b8cda2511f49b03
This commit is contained in:
Lukas Piatkowski 2018-07-19 03:01:31 -07:00 committed by Facebook Github Bot
parent 836f34b6bd
commit af3d993cd3

View File

@ -485,6 +485,30 @@ macro_rules! try_boxstream {
})
}
/// Macro that can be used like ensure! macro from failure crate, but in the context where the
/// expected return type is BoxFuture. Exits a function early with an Error if the condition is not
/// satisfied.
#[macro_export]
macro_rules! ensure_boxfuture {
($cond:expr, $e:expr) => {
if !($cond) {
return ::futures::future::err($e.into()).boxify();
}
};
}
/// Macro that can be used like ensure! macro from failure crate, but in the context where the
/// expected return type is BoxStream. Exits a function early with an Error if the condition is not
/// satisfied.
#[macro_export]
macro_rules! ensure_boxstream {
($cond:expr, $e:expr) => {
if !($cond) {
return ::futures::stream::once(Err($e.into())).boxify();
}
};
}
/// This method allows us to take synchronous code, schedule it on the default tokio thread pool
/// and convert it to the future. Func can return anything that is convertable to a future, for
/// example, Result