From a0598b4159ff4acb548b23d18c24807627b6a02c Mon Sep 17 00:00:00 2001 From: rasul Date: Sat, 4 Apr 2020 08:04:31 -0500 Subject: [PATCH] add try_send and option_send macros --- src/macros.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/macros.rs b/src/macros.rs index c3147b2..ec28a49 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -47,6 +47,32 @@ macro_rules! try_send_error { }; } +/// Unwrap a `Result`, send a message if it's E. +#[macro_export] +macro_rules! try_send { + ($i:ident, $e:expr, $($arg:tt)*) => { + match $e { + Ok(r) => r, + Err(e) => { + return SendQueue(vec![($i, std::fmt::format(format_args!($($arg)*)), true)].into()); + } + } + }; +} + +/// Unwrap a `Option`, send a message if it's None. +#[macro_export] +macro_rules! option_send { + ($i:ident, $e:expr, $($arg:tt)*) => { + match $e { + Some(r) => r, + None => { + return SendQueue(vec![($i, std::fmt::format(format_args!($($arg)*)), true)].into()); + } + } + }; +} + /// Unwrap a `Result, E>`, log message if it's error or none #[macro_export] macro_rules! try_option_send_error {