[][src]Macro kompact::match_deser

macro_rules! match_deser {
    ($msg:expr ; { $($id:ident : $target_ty:ty [$deser:ty] => $rhs:expr),* , }) => { ... };
    ($msg:expr ; { $($id:ident : $target_ty:ty [$deser:ty] => $rhs:expr),* , !Err($e:pat) => $err_handler:expr, }) => { ... };
    ($msg:expr ; { $($id:ident : $target_ty:ty [$deser:ty] => $rhs:expr),* , _ => $other:expr, }) => { ... };
    ($msg:expr ; { $($id:ident : $target_ty:ty [$deser:ty] => $rhs:expr),* , !Err($e:pat) => $err_handler:expr , _ => $other:expr, }) => { ... };
}

A macro to make matching serialisation ids and deserialising easier

This macro basically generates a large match statement on the serialisation ids and then uses try_deserialise_unchecked to get a value for the matched type, which it then uses to invoke the right-hand side which expects that particular type.

Basic Example

use kompact::prelude::*;
use bytes::BytesMut;


let test_str = "Test me".to_string();
// serialise the string
let mut mbuf = BytesMut::with_capacity(test_str.size_hint().expect("size hint"));
test_str.serialise(&mut mbuf).expect("serialise");
// create a net message
let buf = mbuf.freeze();
let msg = NetMessage::with_bytes(String::SER_ID, some_path, some_path2, buf);
// try to deserialise it again
match_deser!(msg; {
    _num: u64 [u64]           => unreachable!("It's definitely not a u64..."),
    test_res: String [String] => assert_eq!(test_str, test_res),
});

Example with Error Handling

use kompact::prelude::*;
use bytes::BytesMut;


let test_str = "Test me".to_string();
// serialise the string
let mut mbuf = BytesMut::with_capacity(test_str.size_hint().expect("size hint"));
test_str.serialise(&mut mbuf).expect("serialise");
// create a net message
let buf = mbuf.freeze();
let msg = NetMessage::with_bytes(String::SER_ID, some_path, some_path2, buf);
// try to deserialise it again
match_deser!(msg; {
    _num: u64 [u64]           => unreachable!("It's definitely not a u64..."),
    test_res: String [String] => assert_eq!(test_str, test_res),
    !Err(error)               => panic!("Some error occurred during deserialisation: {:?}", error),
    _                         => unreachable!("It's definitely not...whatever this is..."),
});