Skip to content

Instantly share code, notes, and snippets.

@opalenic
Created September 25, 2017 23:16
Show Gist options
  • Save opalenic/d3464ea0b36664cbc4659f6658e1cede to your computer and use it in GitHub Desktop.
Save opalenic/d3464ea0b36664cbc4659f6658e1cede to your computer and use it in GitHub Desktop.
Message wrapper macro initial
#[macro_export]
macro_rules! derive_message_wrapper {
($type_name:tt<$($lifetime:tt),+>, $msg_id:pat => $parse_func:expr, $alloc_func:expr, $free_func:expr) => (
derive_message_wrapper!($type_name<$($lifetime),+>, [$msg_id => $parse_func], $alloc_func, $free_func);
);
($type_name:tt<$($lifetime:tt),+>, [$($msg_id:pat => $parse_func:expr),+], $alloc_func:expr, $free_func:expr) => (
impl<$($lifetime),+> $crate::bindings::message_dispatcher::TryFrom<*mut $crate::bindings::message_dispatcher::message> for $type_name {
derive_message_wrapper!(@try_from_impl_payload $type_name<$($lifetime),+>, [$($msg_id => $parse_func),+]);
}
);
($type_name:ty, $msg_id:ident => $parse_func:expr, $alloc_func:expr, $free_func:expr) => (
derive_message_wrapper!($type_name, [$msg_id => $parse_func], $alloc_func, $free_func);
);
($type_name:ty, [$($msg_id:ident => $parse_func:expr),+], $alloc_func:expr, $free_func:expr) => (
impl $crate::bindings::message_dispatcher::TryFrom<*mut $crate::bindings::message_dispatcher::message> for $type_name {
derive_message_wrapper!(@try_from_impl_payload $type_name, [$($msg_id => $parse_func),+]);
}
);
(@try_from_impl_payload $type_name:ty, [$($msg_id:ident => $parse_func:expr),+]) => (
type Error = ();
fn try_from(raw_ptr: *mut $crate::bindings::message_dispatcher::message) -> Result<$type_name, Self::Error> {
unsafe {
if raw_ptr.is_null() || (*raw_ptr).data.is_null() {
return Err(());
}
match (*raw_ptr).msg_type {
$($msg_id => $parse_func((*raw_ptr).data),)+
_ => Err(())
}
}
}
);
(@try_from_impl_no_payload) => ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment