add a macro to help reduce the burden of unpacking messages

The syntax to pattern-match a message is a pretty heavy cognitive
burden. Try to ease that by introducing a macro.

The main coin-toss here is whether to include the "what you do
with the message" expression within the macro, or to allow it to
be a block that trails it.

The main benefit of including the body inside the macro is that
we can append an "else" clause that spits out an error message
in case the template didn't match. The drawback is the awkwardness
of putting curly braces inside the parens of the macro expansion.

I think Rust coders are probably more OK with that awkwardness,
even though I find it visually unappealing; and the benefit is that
scalars sent to opcodes expecting blockingscalars (and vice versa)
don't go to a "black hole" -- you'll get an error message.

This error message will probably save someone an hour of debugging
someday, so we'll go with the awkwardness.
This commit is contained in:
bunnie 2021-04-02 15:34:39 +08:00
parent d21c0fa3e7
commit 0e936eaee3

View file

@ -803,3 +803,35 @@ impl From<Error> for Result {
pub type SysCallRequest = core::result::Result<crate::syscall::SysCall, Error>;
pub type SysCallResult = core::result::Result<Result, Error>;
#[macro_export]
macro_rules! msg_scalar_unpack {
// the args are `tt` so that you can specify _ as the arg
($msg:ident, $arg1:tt, $arg2:tt, $arg3:tt, $arg4:tt, $body: block) => {
{
if let xous::Message::Scalar(xous::ScalarMessage {
id: _, arg1: $arg1, arg2: $arg2, arg3: $arg3, arg4: $arg4
}) = $msg.body {
$body
} else {
log::error!("message expansion failed in msg_scalar_unpack macro")
}
}
}
}
#[macro_export]
macro_rules! msg_blocking_scalar_unpack {
// the args are `tt` so that you can specify _ as the arg
($msg:ident, $arg1:tt, $arg2:tt, $arg3:tt, $arg4:tt, $body: block) => {
{
if let xous::Message::BlockingScalar(xous::ScalarMessage {
id: _, arg1: $arg1, arg2: $arg2, arg3: $arg3, arg4: $arg4
}) = $msg.body {
$body
} else {
log::error!("message expansion failed in msg_scalar_unpack macro")
}
}
}
}