From 8976c166cff4c98c43cdc409275f548eb27ecb13 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 18 Jul 2022 20:48:41 +0200 Subject: refactor: reduce the capabilities of the castable_to macro --- syrette_macros/src/lib.rs | 16 +---- syrette_macros/src/libs/intertrait_macros/args.rs | 77 ++-------------------- .../src/libs/intertrait_macros/gen_caster.rs | 34 +++------- 3 files changed, 20 insertions(+), 107 deletions(-) diff --git a/syrette_macros/src/lib.rs b/syrette_macros/src/lib.rs index 3145b5f..0a6f0e7 100644 --- a/syrette_macros/src/lib.rs +++ b/syrette_macros/src/lib.rs @@ -10,10 +10,7 @@ mod libs; use factory_type_alias::FactoryTypeAlias; use injectable_impl::InjectableImpl; use injectable_macro_args::InjectableMacroArgs; -use libs::intertrait_macros::{ - args::{Casts, Flag, Targets}, - gen_caster::generate_caster, -}; +use libs::intertrait_macros::{args::Cast, gen_caster::generate_caster}; /// Makes a struct injectable. Thereby usable with `DIContainer`. /// @@ -149,14 +146,7 @@ pub fn factory(_: TokenStream, type_alias_stream: TokenStream) -> TokenStream #[proc_macro] pub fn castable_to(input: TokenStream) -> TokenStream { - let Casts { - ty, - targets: Targets { flags, paths }, - } = parse_macro_input!(input); + let Cast { ty, target } = parse_macro_input!(input); - paths - .iter() - .map(|t| generate_caster(&ty, t, flags.contains(&Flag::Sync))) - .collect::() - .into() + generate_caster(&ty, &target).into() } diff --git a/syrette_macros/src/libs/intertrait_macros/args.rs b/syrette_macros/src/libs/intertrait_macros/args.rs index d576ae2..a49567f 100644 --- a/syrette_macros/src/libs/intertrait_macros/args.rs +++ b/syrette_macros/src/libs/intertrait_macros/args.rs @@ -11,89 +11,26 @@ * at your option. */ -use std::collections::HashSet; - -use syn::bracketed; use syn::parse::{Parse, ParseStream, Result}; -use syn::punctuated::Punctuated; -use syn::{Error, Ident, Path, Token, Type}; - -#[derive(Hash, PartialEq, Eq)] -pub enum Flag -{ - Sync, -} - -impl Flag -{ - fn from(ident: &Ident) -> Result - { - match ident.to_string().as_str() { - "sync" => Ok(Flag::Sync), - unknown => { - let msg = format!("Unknown flag: {}", unknown); - Err(Error::new_spanned(ident, msg)) - } - } - } -} - -pub struct Targets -{ - pub flags: HashSet, - pub paths: Vec, -} +use syn::{Path, Token, Type}; -impl Parse for Targets -{ - fn parse(input: ParseStream) -> Result - { - let mut flags = HashSet::new(); - let mut paths = Vec::new(); - - if input.is_empty() { - return Ok(Targets { flags, paths }); - } - - if input.peek(syn::token::Bracket) { - let content; - bracketed!(content in input); - for ident in Punctuated::::parse_terminated(&content)? { - if !flags.insert(Flag::from(&ident)?) { - let msg = format!("Duplicated flag: {}", ident); - return Err(Error::new_spanned(ident, msg)); - } - } - } - - if input.is_empty() { - return Ok(Targets { flags, paths }); - } - - paths = Punctuated::::parse_terminated(input)? - .into_iter() - .collect(); - - Ok(Targets { flags, paths }) - } -} - -pub struct Casts +pub struct Cast { pub ty: Type, - pub targets: Targets, + pub target: Path, } -impl Parse for Casts +impl Parse for Cast { fn parse(input: ParseStream) -> Result { let ty: Type = input.parse()?; + input.parse::]>()?; - Ok(Casts { + Ok(Cast { ty, - targets: input.parse()?, + target: input.parse()?, }) } } diff --git a/syrette_macros/src/libs/intertrait_macros/gen_caster.rs b/syrette_macros/src/libs/intertrait_macros/gen_caster.rs index 268fb09..a0e63b5 100644 --- a/syrette_macros/src/libs/intertrait_macros/gen_caster.rs +++ b/syrette_macros/src/libs/intertrait_macros/gen_caster.rs @@ -20,33 +20,19 @@ use quote::ToTokens; use uuid::adapter::Simple; use uuid::Uuid; -pub fn generate_caster( - ty: &impl ToTokens, - trait_: &impl ToTokens, - sync: bool, -) -> TokenStream +pub fn generate_caster(ty: &impl ToTokens, trait_: &impl ToTokens) -> TokenStream { let mut fn_buf = [0u8; FN_BUF_LEN]; + let fn_ident = format_ident!("{}", new_fn_name(&mut fn_buf)); - let new_caster = if sync { - quote! { - syrette::libs::intertrait::Caster::::new_sync( - |from| from.downcast_ref::<#ty>().unwrap(), - |from| from.downcast_mut::<#ty>().unwrap(), - |from| from.downcast::<#ty>().unwrap(), - |from| from.downcast::<#ty>().unwrap(), - |from| from.downcast::<#ty>().unwrap() - ) - } - } else { - quote! { - syrette::libs::intertrait::Caster::::new( - |from| from.downcast_ref::<#ty>().unwrap(), - |from| from.downcast_mut::<#ty>().unwrap(), - |from| from.downcast::<#ty>().unwrap(), - |from| from.downcast::<#ty>().unwrap(), - ) - } + + let new_caster = quote! { + syrette::libs::intertrait::Caster::::new( + |from| from.downcast_ref::<#ty>().unwrap(), + |from| from.downcast_mut::<#ty>().unwrap(), + |from| from.downcast::<#ty>().unwrap(), + |from| from.downcast::<#ty>().unwrap(), + ) }; quote! { -- cgit v1.2.3-18-g5258