Skip to content

Commit

Permalink
[macros] Avoid glob imports where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
bal-e committed Jan 6, 2025
1 parent af13cf1 commit 8daf6b5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
2 changes: 1 addition & 1 deletion macros/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------------------

Expand Down
58 changes: 33 additions & 25 deletions macros/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------------

Expand All @@ -21,24 +25,24 @@ pub struct ImplSkeleton {
pub unsafety: Option<Token![unsafe]>,

/// The trait being implemented.
pub bound: Option<Path>,
pub bound: Option<syn::Path>,

/// 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();
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -92,12 +96,12 @@ impl ImplSkeleton {

let unsafety = unsafety.then_some(<Token![unsafe]>::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,
Expand All @@ -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(),
};
Expand All @@ -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,
Expand All @@ -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<T: ?Sized + #bound>() {}
assert_impl::<#target>();
Expand All @@ -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(),
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 9 additions & 3 deletions macros/src/repr.rs
Original file line number Diff line number Diff line change
@@ -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 -----------------------------------------------------------

Expand All @@ -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<Self> {
pub fn determine(
attrs: &[Attribute],
bound: &str,
) -> Result<Self, Error> {
let mut repr = None;
for attr in attrs {
if !attr.path().is_ident("repr") {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8daf6b5

Please sign in to comment.