Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid stringifying and reparsing a TokenStream #144

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use syn::{
parse_str, punctuated::Punctuated, spanned::Spanned, Attribute, Data, DeriveInput,
Error, Field, Fields, FieldsNamed, FieldsUnnamed, GenericParam, Generics, Ident,
ImplGenerics, Index, Meta, NestedMeta, Result, Token, Type, TypeGenerics,
TypeParamBound, Variant, WhereClause,
parse_quote, punctuated::Punctuated, spanned::Spanned, Attribute, Data,
DeriveInput, Error, Field, Fields, FieldsNamed, FieldsUnnamed, GenericParam,
Generics, Ident, ImplGenerics, Index, Meta, NestedMeta, Result, Token, Type,
TypeGenerics, TypeParamBound, Variant, WhereClause,
};

#[derive(Clone, Copy, Default)]
Expand Down Expand Up @@ -121,10 +121,9 @@ pub fn add_extra_type_param_bound_op_output<'a>(
let mut generics = generics.clone();
for type_param in &mut generics.type_params_mut() {
let type_ident = &type_param.ident;
let bound: TypeParamBound = parse_str(
&quote!(::core::ops::#trait_ident<Output=#type_ident>).to_string(),
)
.unwrap();
let bound: TypeParamBound = parse_quote! {
::core::ops::#trait_ident<Output=#type_ident>
};
type_param.bounds.push(bound)
}

Expand All @@ -143,7 +142,7 @@ pub fn add_extra_ty_param_bound<'a>(
bound: &'a TokenStream,
) -> Generics {
let mut generics = generics.clone();
let bound: TypeParamBound = parse_str(&bound.to_string()).unwrap();
let bound: TypeParamBound = parse_quote! { #bound };
for type_param in &mut generics.type_params_mut() {
type_param.bounds.push(bound.clone())
}
Expand Down Expand Up @@ -176,7 +175,7 @@ pub fn add_extra_generic_param(
generics: &Generics,
generic_param: TokenStream,
) -> Generics {
let generic_param: GenericParam = parse_str(&generic_param.to_string()).unwrap();
let generic_param: GenericParam = parse_quote! { #generic_param };
let mut generics = generics.clone();
generics.params.push(generic_param);

Expand All @@ -187,8 +186,7 @@ pub fn add_extra_where_clauses(
generics: &Generics,
type_where_clauses: TokenStream,
) -> Generics {
let mut type_where_clauses: WhereClause =
parse_str(&type_where_clauses.to_string()).unwrap();
let mut type_where_clauses: WhereClause = parse_quote! { #type_where_clauses };
let mut new_generics = generics.clone();
if let Some(old_where) = new_generics.where_clause {
type_where_clauses.predicates.extend(old_where.predicates)
Expand Down
14 changes: 14 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ struct DoubleUIntStruct {
#[derive(From, Into, Constructor)]
struct Unit;

// Tests that we can forward to a path
// containing `$crate`
macro_rules! use_dollar_crate {
() => {
struct Foo;
#[derive(From)]
enum Bar {
First(#[from(forward)] $crate::Foo),
}
};
}

use_dollar_crate!();

#[test]
fn main() {
let mut myint: MyInt = 5.into();
Expand Down