#[macro_export]
macro_rules! constrain {
($n:expr, $type_name:expr $(, $arg:expr )*) => {{
use crate::ErrorKind;
use crate::Error;
let args: &[i64] = &[$($arg),*];
let (min, max) = match args.len() {
0 => (0, i64::MAX),
1 => (0, args[0]),
2 => (args[0], args[1]),
_ => return Err(Error::from_kind(ErrorKind::RangeError("Invalid arguments".to_string()))),
};
if $n < min || $n > max {
return Err(Error::from_kind(ErrorKind::RangeError(format!("{} is out of {} valid range", $n, $type_name))));
}
Ok($n)
}};
}
i want to define macro, but it report mismatched types.
this is the details error:
error[E0308]: mismatched types
–> util/src/number.rs:12:25
|
12 | _ => return Err(Error::from_kind(ErrorKind::RangeError(“Invalid arguments”.to_string()))),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected ()
, found Result<_, Error>
…
30 | assert_eq!(constrain!(10, “example”).unwrap(), 10);
| ————————- in this macro invocation
|
= note: expected unit type ()
found enum std::result::Result<_, Error>
= note: this error originates in the macro constrain
(in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try rustc --explain E0308
.
this it the test code:
assert_eq!(constrain!(10, "example").unwrap(), 10);
how to fix this error?