Skip to main content

naia_derive/
lib.rs

1//! # Naia Derive
2//! Procedural macros to simplify implementation of Naia types
3
4#![deny(trivial_casts, trivial_numeric_casts, unstable_features)]
5
6use quote::quote;
7
8mod channel;
9mod message;
10mod replicate;
11mod shared;
12
13use channel::channel_impl;
14use message::message_impl;
15use replicate::replicate_impl;
16
17// Replicate
18
19/// Derives the Replicate trait for a given struct
20#[proc_macro_derive(Replicate, attributes(replicate))]
21pub fn replicate_derive_shared(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
22    let shared_crate_name = quote! { naia_shared };
23    replicate_impl(input, shared_crate_name, true)
24}
25
26/// Derives the Replicate trait for a given struct, for the Bevy adapter.
27/// Users add their own `#[derive(Component)]` alongside this derive, so we
28/// skip auto-emitting a Bevy Component impl here.
29#[proc_macro_derive(ReplicateBevy, attributes(replicate))]
30pub fn replicate_derive_bevy(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
31    let shared_crate_name = quote! { naia_bevy_shared };
32    replicate_impl(input, shared_crate_name, false)
33}
34
35// Channel
36
37/// Derives the Channel trait for a given struct
38#[proc_macro_derive(Channel)]
39pub fn channel_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
40    let shared_crate_name = quote! { naia_shared };
41    channel_impl(input, shared_crate_name)
42}
43
44/// Derives the Channel trait for a given struct, internal to naia-shared
45#[proc_macro_derive(ChannelInternal)]
46pub fn channel_derive_internal(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
47    let shared_crate_name = quote! { crate };
48    channel_impl(input, shared_crate_name)
49}
50
51// Message
52
53/// Derives the Message trait for a given struct, for internal
54#[proc_macro_derive(MessageInternal)]
55pub fn message_derive_internal(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
56    let shared_crate_name = quote! { crate };
57    message_impl(input, shared_crate_name, false, false)
58}
59
60/// Derives the Message trait for a given struct, for FragmentedMessage
61#[proc_macro_derive(MessageFragment)]
62pub fn message_derive_fragment(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
63    let shared_crate_name = quote! { crate };
64    message_impl(input, shared_crate_name, true, false)
65}
66
67/// Derives the Message trait for a given struct, for RequestMessage
68#[proc_macro_derive(MessageRequest)]
69pub fn message_derive_request(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
70    let shared_crate_name = quote! { crate };
71    message_impl(input, shared_crate_name, false, true)
72}
73
74/// Derives the Message trait for a given struct
75#[proc_macro_derive(Message)]
76pub fn message_derive_shared(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
77    let shared_crate_name = quote! { naia_shared };
78    message_impl(input, shared_crate_name, false, false)
79}
80
81/// Derives the Message trait for a given struct, for the Bevy adapter
82#[proc_macro_derive(MessageBevy)]
83pub fn message_derive_bevy(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
84    let shared_crate_name = quote! { naia_bevy_shared };
85    message_impl(input, shared_crate_name, false, false)
86}
87