make a macro that automates errno assertions

This commit is contained in:
Pat Hickey
2021-01-27 17:42:59 -08:00
parent 4f2696fedf
commit 4f655001c0

View File

@@ -61,3 +61,47 @@ pub unsafe fn drop_rights(fd: wasi::Fd, drop_base: wasi::Rights, drop_inheriting
wasi::fd_fdstat_set_rights(fd, new_base, new_inheriting).expect("dropping fd rights");
}
#[macro_export]
macro_rules! assert_errno {
($s:expr, $( $i:expr ),+,) => {
assert_errno!($s, $( $i ),+)
};
($s:expr, $( $i:expr ),+) => {
let e = $s;
{
// Pretty printing infrastructure
struct Alt<'a>(&'a [&'static str]);
impl<'a> std::fmt::Display for Alt<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let l = self.0.len();
if l == 0 {
unreachable!()
} else if l == 1 {
f.write_str(self.0[0])
} else if l == 2 {
f.write_str(self.0[0])?;
f.write_str(" or ")?;
f.write_str(self.0[1])
} else {
for (ix, s) in self.0.iter().enumerate() {
if ix == l - 1 {
f.write_str("or ")?;
f.write_str(s)?;
} else {
f.write_str(s)?;
f.write_str(", ")?;
}
}
Ok(())
}
}
}
assert!( $( e == $i || )+ false,
"expected errno {}; got {}",
Alt(&[ $( wasi::errno_name($i) ),+ ]),
wasi::errno_name(e),
)
}
};
}