| 
 | 1 | +// This file is Copyright its original authors, visible in version control  | 
 | 2 | +// history.  | 
 | 3 | +//  | 
 | 4 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE  | 
 | 5 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license  | 
 | 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.  | 
 | 7 | +// You may not use this file except in accordance with one or both of these  | 
 | 8 | +// licenses.  | 
 | 9 | + | 
 | 10 | +#![crate_name = "lightning_macros"]  | 
 | 11 | + | 
 | 12 | +//! Proc macros used by LDK  | 
 | 13 | +
  | 
 | 14 | +#![cfg_attr(not(test), no_std)]  | 
 | 15 | +#![deny(missing_docs)]  | 
 | 16 | +#![forbid(unsafe_code)]  | 
 | 17 | +#![deny(rustdoc::broken_intra_doc_links)]  | 
 | 18 | +#![deny(rustdoc::private_intra_doc_links)]  | 
 | 19 | +#![cfg_attr(docsrs, feature(doc_auto_cfg))]  | 
 | 20 | + | 
 | 21 | +use proc_macro::TokenStream;  | 
 | 22 | +use quote::quote;  | 
 | 23 | +use syn::spanned::Spanned;  | 
 | 24 | +use syn::{parse, ImplItemFn, ItemImpl, ItemTrait, Token};  | 
 | 25 | + | 
 | 26 | +fn add_async_trait(mut parsed: ItemTrait) -> TokenStream {  | 
 | 27 | +	let output = quote! {  | 
 | 28 | +		#[cfg(not(feature = "async-interface"))]  | 
 | 29 | +		#parsed  | 
 | 30 | +	};  | 
 | 31 | + | 
 | 32 | +	for mut item in &mut parsed.items {  | 
 | 33 | +		if let syn::TraitItem::Fn(f) = &mut item {  | 
 | 34 | +			f.sig.asyncness = Some(Token));  | 
 | 35 | +		}  | 
 | 36 | +	}  | 
 | 37 | + | 
 | 38 | +	let output = quote! {  | 
 | 39 | +		#output  | 
 | 40 | + | 
 | 41 | +		#[cfg(feature = "async-interface")]  | 
 | 42 | +		#[async_trait(?Send)]  | 
 | 43 | +		#parsed  | 
 | 44 | +	};  | 
 | 45 | + | 
 | 46 | +	output.into()  | 
 | 47 | +}  | 
 | 48 | + | 
 | 49 | +fn add_async_method(mut parsed: ImplItemFn) -> TokenStream {  | 
 | 50 | +	let output = quote! {  | 
 | 51 | +		#[cfg(not(feature = "async-interface"))]  | 
 | 52 | +		#parsed  | 
 | 53 | +	};  | 
 | 54 | + | 
 | 55 | +	parsed.sig.asyncness = Some(Token));  | 
 | 56 | + | 
 | 57 | +	let output = quote! {  | 
 | 58 | +		#output  | 
 | 59 | + | 
 | 60 | +		#[cfg(feature = "async-interface")]  | 
 | 61 | +		#parsed  | 
 | 62 | +	};  | 
 | 63 | + | 
 | 64 | +	output.into()  | 
 | 65 | +}  | 
 | 66 | + | 
 | 67 | +fn add_async_impl_trait(mut parsed: ItemImpl) -> TokenStream {  | 
 | 68 | +	let output = quote! {  | 
 | 69 | +		#[cfg(not(feature = "async-interface"))]  | 
 | 70 | +		#parsed  | 
 | 71 | +	};  | 
 | 72 | + | 
 | 73 | +	for mut item in &mut parsed.items {  | 
 | 74 | +		if let syn::ImplItem::Fn(f) = &mut item {  | 
 | 75 | +			f.sig.asyncness = Some(Token));  | 
 | 76 | +		}  | 
 | 77 | +	}  | 
 | 78 | + | 
 | 79 | +	let output = quote! {  | 
 | 80 | +		#output  | 
 | 81 | + | 
 | 82 | +		#[cfg(feature = "async-interface")]  | 
 | 83 | +		#[async_trait(?Send)]  | 
 | 84 | +		#parsed  | 
 | 85 | +	};  | 
 | 86 | + | 
 | 87 | +	output.into()  | 
 | 88 | +}  | 
 | 89 | + | 
 | 90 | +/// Makes a method or every method of a trait `async`, if the `async-interface` feature is enabled.  | 
 | 91 | +///  | 
 | 92 | +/// Requires the `async-trait` crate as a dependency whenever this attribute is used on a trait  | 
 | 93 | +/// definition or trait implementation.  | 
 | 94 | +#[proc_macro_attribute]  | 
 | 95 | +pub fn maybe_async(_attr: TokenStream, item: TokenStream) -> TokenStream {  | 
 | 96 | +	if let Ok(parsed) = parse(item.clone()) {  | 
 | 97 | +		add_async_trait(parsed)  | 
 | 98 | +	} else if let Ok(parsed) = parse(item.clone()) {  | 
 | 99 | +		add_async_method(parsed)  | 
 | 100 | +	} else if let Ok(parsed) = parse(item) {  | 
 | 101 | +		add_async_impl_trait(parsed)  | 
 | 102 | +	} else {  | 
 | 103 | +		(quote! {  | 
 | 104 | +			compile_error!("#[maybe_async] can only be used on methods, trait or trait impl blocks")  | 
 | 105 | +		})  | 
 | 106 | +		.into()  | 
 | 107 | +	}  | 
 | 108 | +}  | 
 | 109 | + | 
 | 110 | +/// Awaits, if the `async-interface` feature is enabled.  | 
 | 111 | +#[proc_macro]  | 
 | 112 | +pub fn maybe_await(expr: TokenStream) -> TokenStream {  | 
 | 113 | +	let expr: proc_macro2::TokenStream = expr.into();  | 
 | 114 | +	let quoted = quote! {  | 
 | 115 | +		{  | 
 | 116 | +			#[cfg(not(feature = "async-interface"))]  | 
 | 117 | +			{  | 
 | 118 | +				#expr  | 
 | 119 | +			}  | 
 | 120 | + | 
 | 121 | +			#[cfg(feature = "async-interface")]  | 
 | 122 | +			{  | 
 | 123 | +				#expr.await  | 
 | 124 | +			}  | 
 | 125 | +		}  | 
 | 126 | +	};  | 
 | 127 | + | 
 | 128 | +	quoted.into()  | 
 | 129 | +}  | 
0 commit comments