limnus_macros/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/limnus
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5extern crate proc_macro;
6
7use proc_macro::TokenStream;
8use quote::quote;
9use syn::{DeriveInput, parse_macro_input};
10
11#[proc_macro_derive(Resource)]
12pub fn resource_derive(input: TokenStream) -> TokenStream {
13    let input = parse_macro_input!(input as DeriveInput);
14    let name = &input.ident;
15    let generics = &input.generics;
16
17    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
18
19    let expanded = quote! {
20        impl #impl_generics Resource for #name #ty_generics #where_clause {
21        }
22
23        // TODO: Make sure type do not support Copy or Clone
24        // TODO: Should be a conf to have this as an option
25        // impl !Clone for #name {}
26        // impl !Copy for #name {}
27    };
28
29    TokenStream::from(expanded)
30}
31
32#[proc_macro_derive(LocalResource)]
33pub fn local_resource_derive(input: TokenStream) -> TokenStream {
34    let input = parse_macro_input!(input as DeriveInput);
35    let name = &input.ident;
36    let generics = &input.generics;
37
38    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
39
40    let expanded = quote! {
41        impl #impl_generics LocalResource for #name #ty_generics #where_clause {
42        }
43
44        // TODO: Make sure type do not support Copy or Clone
45        // TODO: Should be a conf to have this as an option
46        // impl !Clone for #name {}
47        // impl !Copy for #name {}
48    };
49
50    TokenStream::from(expanded)
51}
52
53#[proc_macro_derive(Message)]
54pub fn message_derive(input: TokenStream) -> TokenStream {
55    let input = parse_macro_input!(input as DeriveInput);
56    let name = &input.ident;
57    let generics = &input.generics;
58
59    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
60
61    let expanded = quote! {
62        impl #impl_generics Message for #name #ty_generics #where_clause {
63        }
64
65        // TODO: Make sure type do not support Copy or Clone
66        // TODO: Should be a conf to have this as an option
67        // impl !Clone for #name {}
68        // impl !Copy for #name {}
69        impl #impl_generics #name #ty_generics #where_clause {
70            #[doc(hidden)]
71            const __PREVENT_COPY_CLONE: () = {
72                let _marker: std::marker::PhantomData<*const ()> = std::marker::PhantomData;
73            };
74        }
75
76    };
77
78    TokenStream::from(expanded)
79}
80
81#[proc_macro_derive(Asset)]
82pub fn asset_derive(input: TokenStream) -> TokenStream {
83    let input = parse_macro_input!(input as DeriveInput);
84    let name = &input.ident;
85    let generics = &input.generics;
86
87    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
88
89    let expanded = quote! {
90        impl #impl_generics Asset for #name #ty_generics #where_clause {
91        }
92
93        // TODO: Make sure type do not support Copy or Clone
94        // TODO: Should be a conf to have this as an option
95        // impl !Clone for #name {}
96        // impl !Copy for #name {}
97    };
98
99    TokenStream::from(expanded)
100}