macros.rs

 1#[macro_export]
 2macro_rules! messages {
 3    ($(($name:ident, $priority:ident)),* $(,)?) => {
 4        pub fn build_typed_envelope(sender_id: ConnectionId, envelope: Envelope) -> Option<Box<dyn AnyTypedEnvelope>> {
 5            match envelope.payload {
 6                $(Some(envelope::Payload::$name(payload)) => {
 7                    Some(Box::new(TypedEnvelope {
 8                        sender_id,
 9                        original_sender_id: envelope.original_sender_id.map(|original_sender| PeerId {
10                            owner_id: original_sender.owner_id,
11                            id: original_sender.id
12                        }),
13                        message_id: envelope.id,
14                        payload,
15                    }))
16                }, )*
17                _ => None
18            }
19        }
20
21        $(
22            impl EnvelopedMessage for $name {
23                const NAME: &'static str = std::stringify!($name);
24                const PRIORITY: MessagePriority = MessagePriority::$priority;
25
26                fn into_envelope(
27                    self,
28                    id: u32,
29                    responding_to: Option<u32>,
30                    original_sender_id: Option<PeerId>,
31                ) -> Envelope {
32                    Envelope {
33                        id,
34                        responding_to,
35                        original_sender_id,
36                        payload: Some(envelope::Payload::$name(self)),
37                    }
38                }
39
40                fn from_envelope(envelope: Envelope) -> Option<Self> {
41                    if let Some(envelope::Payload::$name(msg)) = envelope.payload {
42                        Some(msg)
43                    } else {
44                        None
45                    }
46                }
47            }
48        )*
49    };
50}
51
52#[macro_export]
53macro_rules! request_messages {
54    ($(($request_name:ident, $response_name:ident)),* $(,)?) => {
55        $(impl RequestMessage for $request_name {
56            type Response = $response_name;
57        })*
58    };
59}
60
61#[macro_export]
62macro_rules! entity_messages {
63    ($id_field:ident, $($name:ident),* $(,)?) => {
64        $(impl EntityMessage for $name {
65            fn remote_entity_id(&self) -> u64 {
66                self.$id_field
67            }
68        })*
69    };
70}