macros.rs

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