From 8daf6b502b7c172fd9cccce55a29026f12f1e566 Mon Sep 17 00:00:00 2001 From: arya dradjica Date: Mon, 6 Jan 2025 18:42:25 +0100 Subject: [PATCH] [macros] Avoid glob imports where possible --- macros/src/data.rs | 2 +- macros/src/impls.rs | 58 ++++++++++++++++++++++++++------------------- macros/src/repr.rs | 12 +++++++--- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/macros/src/data.rs b/macros/src/data.rs index 6a0788b3e..ee0c52baf 100644 --- a/macros/src/data.rs +++ b/macros/src/data.rs @@ -4,7 +4,7 @@ use std::ops::Deref; use proc_macro2::TokenStream; use quote::{quote, ToTokens}; -use syn::{spanned::Spanned, *}; +use syn::{spanned::Spanned, Field, Fields, Ident, Index, Member, Token}; //----------- Struct --------------------------------------------------------- diff --git a/macros/src/impls.rs b/macros/src/impls.rs index 2d9724f0e..4c0971998 100644 --- a/macros/src/impls.rs +++ b/macros/src/impls.rs @@ -2,7 +2,11 @@ use proc_macro2::{Span, TokenStream}; use quote::{format_ident, quote, ToTokens}; -use syn::{punctuated::Punctuated, visit::Visit, *}; +use syn::{ + punctuated::Punctuated, visit::Visit, ConstParam, GenericArgument, + GenericParam, Ident, Lifetime, LifetimeParam, Token, TypeParam, + TypeParamBound, WhereClause, WherePredicate, +}; //----------- ImplSkeleton --------------------------------------------------- @@ -21,24 +25,24 @@ pub struct ImplSkeleton { pub unsafety: Option, /// The trait being implemented. - pub bound: Option, + pub bound: Option, /// The type being implemented on. - pub subject: Path, + pub subject: syn::Path, /// The where clause of the `impl` block. pub where_clause: WhereClause, /// The contents of the `impl`. - pub contents: Block, + pub contents: syn::Block, /// A `const` block for asserting requirements. - pub requirements: Block, + pub requirements: syn::Block, } impl ImplSkeleton { /// Construct an [`ImplSkeleton`] for a [`DeriveInput`]. - pub fn new(input: &DeriveInput, unsafety: bool) -> Self { + pub fn new(input: &syn::DeriveInput, unsafety: bool) -> Self { let mut lifetimes = Vec::new(); let mut types = Vec::new(); let mut consts = Vec::new(); @@ -55,13 +59,13 @@ impl ImplSkeleton { GenericParam::Type(value) => { types.push(value.clone()); let id = value.ident.clone(); - let id = TypePath { + let id = syn::TypePath { qself: None, - path: Path { + path: syn::Path { leading_colon: None, - segments: [PathSegment { + segments: [syn::PathSegment { ident: id, - arguments: PathArguments::None, + arguments: syn::PathArguments::None, }] .into_iter() .collect(), @@ -73,13 +77,13 @@ impl ImplSkeleton { GenericParam::Const(value) => { consts.push(value.clone()); let id = value.ident.clone(); - let id = TypePath { + let id = syn::TypePath { qself: None, - path: Path { + path: syn::Path { leading_colon: None, - segments: [PathSegment { + segments: [syn::PathSegment { ident: id, - arguments: PathArguments::None, + arguments: syn::PathArguments::None, }] .into_iter() .collect(), @@ -92,12 +96,12 @@ impl ImplSkeleton { let unsafety = unsafety.then_some(::default()); - let subject = Path { + let subject = syn::Path { leading_colon: None, - segments: [PathSegment { + segments: [syn::PathSegment { ident: input.ident.clone(), - arguments: PathArguments::AngleBracketed( - AngleBracketedGenericArguments { + arguments: syn::PathArguments::AngleBracketed( + syn::AngleBracketedGenericArguments { colon2_token: None, lt_token: Default::default(), args: subject_args, @@ -115,12 +119,12 @@ impl ImplSkeleton { predicates: Punctuated::new(), }); - let contents = Block { + let contents = syn::Block { brace_token: Default::default(), stmts: Vec::new(), }; - let requirements = Block { + let requirements = syn::Block { brace_token: Default::default(), stmts: Vec::new(), }; @@ -142,7 +146,11 @@ impl ImplSkeleton { /// /// If the type is concrete, a verifying statement is added for it. /// Otherwise, it is added to the where clause. - pub fn require_bound(&mut self, target: Type, bound: TypeParamBound) { + pub fn require_bound( + &mut self, + target: syn::Type, + bound: TypeParamBound, + ) { let mut visitor = ConcretenessVisitor { skeleton: self, is_concrete: true, @@ -154,7 +162,7 @@ impl ImplSkeleton { if visitor.is_concrete { // Add a concrete requirement for this bound. - self.requirements.stmts.push(parse_quote! { + self.requirements.stmts.push(syn::parse_quote! { const _: fn() = || { fn assert_impl() {} assert_impl::<#target>(); @@ -164,7 +172,7 @@ impl ImplSkeleton { // Add this bound to the `where` clause. let mut bounds = Punctuated::new(); bounds.push(bound); - let pred = WherePredicate::Type(PredicateType { + let pred = WherePredicate::Type(syn::PredicateType { lifetimes: None, bounded_ty: target, colon_token: Default::default(), @@ -196,9 +204,9 @@ impl ImplSkeleton { let lifetime = self.new_lifetime(prefix); let mut bounds = bounds.into_iter().peekable(); let param = if bounds.peek().is_some() { - parse_quote! { #lifetime: #(#bounds)+* } + syn::parse_quote! { #lifetime: #(#bounds)+* } } else { - parse_quote! { #lifetime } + syn::parse_quote! { #lifetime } }; (lifetime, param) } diff --git a/macros/src/repr.rs b/macros/src/repr.rs index 80c900eb6..b699b571b 100644 --- a/macros/src/repr.rs +++ b/macros/src/repr.rs @@ -1,7 +1,10 @@ //! Determining the memory layout of a type. use proc_macro2::Span; -use syn::{punctuated::Punctuated, spanned::Spanned, *}; +use syn::{ + punctuated::Punctuated, spanned::Spanned, Attribute, Error, LitInt, Meta, + Token, +}; //----------- Repr ----------------------------------------------------------- @@ -19,7 +22,10 @@ impl Repr { /// Determine the representation for a type from its attributes. /// /// This will fail if a stable representation cannot be found. - pub fn determine(attrs: &[Attribute], bound: &str) -> Result { + pub fn determine( + attrs: &[Attribute], + bound: &str, + ) -> Result { let mut repr = None; for attr in attrs { if !attr.path().is_ident("repr") { @@ -57,7 +63,7 @@ impl Repr { || meta.path.is_ident("aligned") => { let span = meta.span(); - let lit: LitInt = parse2(meta.tokens)?; + let lit: LitInt = syn::parse2(meta.tokens)?; let n: usize = lit.base10_parse()?; if n != 1 { return Err(Error::new(span,