1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#![ cfg_attr( nightly, cfg_attr( nightly, doc = include_str!("../README.md") )) ]
#![ doc = "" ] // empty doc line to handle missing doc warning when the feature is missing.
//
#![ doc    ( html_root_url = "https://docs.rs/thespis_derive" ) ]
#![ deny   ( missing_docs                                     ) ]
#![ forbid ( unsafe_code                                      ) ]
#![ allow  ( clippy::suspicious_else_formatting               ) ]

#![ warn
(
	missing_debug_implementations ,
	missing_docs                  ,
	nonstandard_style             ,
	rust_2018_idioms              ,
	trivial_casts                 ,
	trivial_numeric_casts         ,
	unused_extern_crates          ,
	unused_qualifications         ,
	single_use_lifetimes          ,
	unreachable_pub               ,
	variant_size_differences      ,
)]


use
{
	quote      :: { quote                                             } ,
	proc_macro :: { TokenStream                                       } ,
	syn        :: { parse_macro_input, parse, DeriveInput, ReturnType } ,
};


/// Generate a basic impl of Actor without any methods.
//
#[ proc_macro_derive( Actor ) ]
//
pub fn derive_actor( input: TokenStream ) -> TokenStream
{
	let input = parse_macro_input!( input as DeriveInput );
	let name  = input.ident;

	let ( impl_generics, ty_generics, where_clause ) = input.generics.split_for_impl();

	// The generated impl.
	//
	let expanded = quote!
	{
		impl #impl_generics thespis::Actor for #name #ty_generics #where_clause
		{}
	};

	TokenStream::from( expanded )
}


/// Implement an async trait method for thespis traits.
///
/// For usage, please look at tests and examples in the _thespis_impl_ crate.
/// The [guide](https://thespis-rs.github.io/thespis_guide/thespis_impl/thespis_impl.html) shows
/// what this desugars to.
//
#[ proc_macro_attribute ]
//
pub fn async_fn( _args: TokenStream, item: TokenStream ) -> TokenStream
{
	let input: syn::ItemFn = match parse( item )
	{
		Ok (i) => i                                  ,
		Err(e) => return e.to_compile_error().into() ,
	};


	let name  = &input.sig.ident  ;
	let args  = &input.sig.inputs ;
	let body  = &input.block      ;
	let attrs = &input.attrs      ;

	let ret = match &input.sig.output
	{
		ReturnType::Default      => quote!( ()   ) ,
		ReturnType::Type(_, ret) => quote!( #ret ) ,
	};

	let tokens = quote!
	{
		#( #attrs )*
		//
		fn #name( #args ) -> ::thespis::Return< '_, #ret > { ::std::boxed::Box::pin
		(
			async move #body
		)}
	};

	tokens.into()
}


/// Implement an async trait method for thespis traits.
///
/// For usage, please look at tests and examples in the _thespis_impl_ crate.
/// The [guide](https://thespis-rs.github.io/thespis_guide/thespis_impl/thespis_impl.html) shows
/// what this desugars to.
//
#[ proc_macro_attribute ]
//
pub fn async_fn_local( _args: TokenStream, item: TokenStream ) -> TokenStream
{
	let input: syn::ItemFn = match parse( item )
	{
		Ok (i) => i                                  ,
		Err(e) => return e.to_compile_error().into() ,
	};


	let name  = &input.sig.ident  ;
	let args  = &input.sig.inputs ;
	let body  = &input.block      ;
	let attrs = &input.attrs      ;

	let ret = match &input.sig.output
	{
		ReturnType::Default      => quote!( ()   ) ,
		ReturnType::Type(_, ret) => quote!( #ret ) ,
	};

	let tokens = quote!
	{
		#( #attrs )*
		//
		fn #name( #args ) -> ::thespis::ReturnNoSend< '_, #ret > { ::std::boxed::Box::pin
		(
			async move #body
		)}
	};

	tokens.into()
}