1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 15:40:03 +08:00

feat: add opt_result

This commit is contained in:
2021-04-30 23:01:35 +08:00
parent 6ed6a9d26f
commit b91e6e060a
3 changed files with 24 additions and 5 deletions

View File

@@ -41,7 +41,7 @@ pub mod util_tlv;
($($arg:tt)+) => ( rust_util::util_msg::print_debug(&format!($($arg)+)); )
}
#[macro_export] macro_rules! opt_value {
($e: expr) => ( match $e { Some(o) => o, None => return, } )
($ex: expr) => ( match $ex { Some(o) => o, None => return, } )
}
pub type XResult<T> = Result<T, Box<dyn Error>>;
@@ -54,8 +54,20 @@ pub fn new_box_ioerror(m: &str) -> Box<dyn Error> {
Box::new(IoError::new(ErrorKind::Other, m))
}
#[macro_export] macro_rules! opt_result {
($ex: expr, $($arg:tt)+) => (
match $ex {
Ok(o) => o,
Err(e) => return Err(rust_util::SimpleError::new(
format!("{}, file: {}, line: {}", format!($($arg)+, e), file!(), line!())
).into()),
}
)
}
#[macro_export] macro_rules! simple_error {
($($arg:tt)+) => ( Err(rust_util::SimpleError::new(format!($($arg)+)).into()) )
($($arg:tt)+) => ( Err(rust_util::SimpleError::new(
format!("{}, file: {}, line: {}", format!($($arg)+), file!(), line!())
).into()) )
}
#[derive(Debug)]