Skip to content

Instantly share code, notes, and snippets.

@Noxivs
Last active August 29, 2015 14:17
Show Gist options
  • Save Noxivs/1127fb6b9abc6129b71e to your computer and use it in GitHub Desktop.
Save Noxivs/1127fb6b9abc6129b71e to your computer and use it in GitHub Desktop.
pub trait Protocol {
type Clean = Self;
}
impl Protocol for usize {
type Clean = usize;
}
impl Protocol for IdentificationBaseMessage {
type Clean = IdentificationBaseMessage;
}
macro_rules! messages {
($($id:expr => $name:ident { $($message:tt)* })*) => {
$(proto_struct!{ $name { $($message)* } })*
pub enum Message {
$($name($name)),*
}
}
}
macro_rules! proto_struct {
($name:ident { $($fname:ident: $fty:ty),+ }) => {
pub struct $name {
$(pub $fname: <$fty as Protocol>::Clean),*
}
}
}
// Sample
messages! {
3 => HelloConnectMessage { field: usize }
4 => IdentificationMessage { base: IdentificationBaseMessage }
5 => IdentificationBaseMessage { field: usize }
}
-----------
// Generated code
#![feature(no_std)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate "std" as std;
pub trait Protocol {type Clean = Self; }
impl Protocol for usize {type Clean = usize; }
impl Protocol for IdentificationBaseMessage {type
Clean
=
IdentificationBaseMessage;
}
pub struct HelloConnectMessage {
pub field: <
usize as Protocol>::Clean,
}
pub struct IdentificationMessage {
pub base: <IdentificationBaseMessage as Protocol>::Clean,
}
pub struct IdentificationBaseMessage {
pub field: <usize as Protocol>::Clean,
}
pub enum Message {
HelloConnectMessage(HelloConnectMessage),
IdentificationMessage(IdentificationMessage),
IdentificationBaseMessage(IdentificationBaseMessage),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment