From 3ed020425bfd1fc5fedfa89a7ce20207bedcf5bc Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 23 Sep 2022 22:19:08 +0200 Subject: fix: prevent problems caused by non send + sync traits --- macros/src/fn_trait.rs | 20 +++++++++++++++++++- macros/src/lib.rs | 7 ++++++- 2 files changed, 25 insertions(+), 2 deletions(-) (limited to 'macros') diff --git a/macros/src/fn_trait.rs b/macros/src/fn_trait.rs index f9b3514..9820f02 100644 --- a/macros/src/fn_trait.rs +++ b/macros/src/fn_trait.rs @@ -2,7 +2,7 @@ use quote::ToTokens; use syn::parse::Parse; use syn::punctuated::Punctuated; use syn::token::Paren; -use syn::{parenthesized, Ident, Token, Type}; +use syn::{parenthesized, parse_str, Ident, Token, TraitBound, Type}; /// A function trait. `dyn Fn(u32) -> String` #[derive(Debug, Clone)] @@ -14,6 +14,15 @@ pub struct FnTrait pub inputs: Punctuated, pub r_arrow_token: Token![->], pub output: Type, + pub trait_bounds: Punctuated, +} + +impl FnTrait +{ + pub fn add_trait_bound(&mut self, trait_bound: TraitBound) + { + self.trait_bounds.push(trait_bound); + } } impl Parse for FnTrait @@ -45,6 +54,7 @@ impl Parse for FnTrait inputs, r_arrow_token, output, + trait_bounds: Punctuated::new(), }) } } @@ -64,5 +74,13 @@ impl ToTokens for FnTrait self.r_arrow_token.to_tokens(tokens); self.output.to_tokens(tokens); + + if !self.trait_bounds.is_empty() { + let plus: Token![+] = parse_str("+").unwrap(); + + plus.to_tokens(tokens); + + self.trait_bounds.to_tokens(tokens); + } } } diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 2cd57f0..b0ccc86 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -6,7 +6,7 @@ use proc_macro::TokenStream; use quote::quote; -use syn::{parse, parse_macro_input}; +use syn::{parse, parse_macro_input, parse_str}; mod decl_def_factory_args; mod declare_interface_args; @@ -236,6 +236,11 @@ pub fn factory(args_stream: TokenStream, type_alias_stream: TokenStream) -> Toke ) .unwrap(); + if is_threadsafe { + factory_interface.add_trait_bound(parse_str("Send").unwrap()); + factory_interface.add_trait_bound(parse_str("Sync").unwrap()); + } + type_alias.ty = Box::new(Type::Verbatim(factory_interface.to_token_stream())); let decl_interfaces = if is_threadsafe { -- cgit v1.2.3-18-g5258