From 0e936eaee3129d1444dffc375100053a71d15a54 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 2 Apr 2021 15:34:39 +0800 Subject: [PATCH] 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. --- xous-rs/src/definitions.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index b6f53448c..a869e99b4 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -803,3 +803,35 @@ impl From for Result { pub type SysCallRequest = core::result::Result; pub type SysCallResult = core::result::Result; + +#[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") + } + } + } +}