1
1
use proc_macro:: TokenStream ;
2
- use proc_macro2:: { Ident , Span } ;
2
+ use proc_macro2:: Span ;
3
3
use quote:: { quote, quote_spanned, ToTokens } ;
4
- use syn:: parse:: Parser ;
4
+ use syn:: { parse:: Parser , parse_quote , Path } ;
5
5
6
6
// syn::AttributeArgs does not implement syn::Parse
7
7
type AttributeArgs = syn:: punctuated:: Punctuated < syn:: NestedMeta , syn:: Token ![ , ] > ;
@@ -29,7 +29,7 @@ struct FinalConfig {
29
29
flavor : RuntimeFlavor ,
30
30
worker_threads : Option < usize > ,
31
31
start_paused : Option < bool > ,
32
- crate_name : Option < String > ,
32
+ crate_name : Option < Path > ,
33
33
}
34
34
35
35
/// Config used in case of the attribute not being able to build a valid config
@@ -47,7 +47,7 @@ struct Configuration {
47
47
worker_threads : Option < ( usize , Span ) > ,
48
48
start_paused : Option < ( bool , Span ) > ,
49
49
is_test : bool ,
50
- crate_name : Option < String > ,
50
+ crate_name : Option < Path > ,
51
51
}
52
52
53
53
impl Configuration {
@@ -112,8 +112,8 @@ impl Configuration {
112
112
if self . crate_name . is_some ( ) {
113
113
return Err ( syn:: Error :: new ( span, "`crate` set multiple times." ) ) ;
114
114
}
115
- let name_ident = parse_ident ( name, span, "crate" ) ?;
116
- self . crate_name = Some ( name_ident . to_string ( ) ) ;
115
+ let name_path = parse_path ( name, span, "crate" ) ?;
116
+ self . crate_name = Some ( name_path ) ;
117
117
Ok ( ( ) )
118
118
}
119
119
@@ -199,23 +199,22 @@ fn parse_string(int: syn::Lit, span: Span, field: &str) -> Result<String, syn::E
199
199
}
200
200
}
201
201
202
- fn parse_ident ( lit : syn:: Lit , span : Span , field : & str ) -> Result < Ident , syn:: Error > {
202
+ fn parse_path ( lit : syn:: Lit , span : Span , field : & str ) -> Result < Path , syn:: Error > {
203
203
match lit {
204
204
syn:: Lit :: Str ( s) => {
205
205
let err = syn:: Error :: new (
206
206
span,
207
207
format ! (
208
- "Failed to parse value of `{}` as ident : \" {}\" " ,
208
+ "Failed to parse value of `{}` as path : \" {}\" " ,
209
209
field,
210
210
s. value( )
211
211
) ,
212
212
) ;
213
- let path = s. parse :: < syn:: Path > ( ) . map_err ( |_| err. clone ( ) ) ?;
214
- path. get_ident ( ) . cloned ( ) . ok_or ( err)
213
+ s. parse :: < syn:: Path > ( ) . map_err ( |_| err. clone ( ) )
215
214
}
216
215
_ => Err ( syn:: Error :: new (
217
216
span,
218
- format ! ( "Failed to parse value of `{}` as ident ." , field) ,
217
+ format ! ( "Failed to parse value of `{}` as path ." , field) ,
219
218
) ) ,
220
219
}
221
220
}
@@ -354,16 +353,14 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
354
353
( start, end)
355
354
} ;
356
355
357
- let crate_name = config. crate_name . as_deref ( ) . unwrap_or ( "tokio" ) ;
358
-
359
- let crate_ident = Ident :: new ( crate_name, last_stmt_start_span) ;
356
+ let crate_path = config. crate_name . unwrap_or_else ( || parse_quote ! { tokio } ) ;
360
357
361
358
let mut rt = match config. flavor {
362
359
RuntimeFlavor :: CurrentThread => quote_spanned ! { last_stmt_start_span=>
363
- #crate_ident :: runtime:: Builder :: new_current_thread( )
360
+ #crate_path :: runtime:: Builder :: new_current_thread( )
364
361
} ,
365
362
RuntimeFlavor :: Threaded => quote_spanned ! { last_stmt_start_span=>
366
- #crate_ident :: runtime:: Builder :: new_multi_thread( )
363
+ #crate_path :: runtime:: Builder :: new_multi_thread( )
367
364
} ,
368
365
} ;
369
366
if let Some ( v) = config. worker_threads {
@@ -414,7 +411,7 @@ fn parse_knobs(mut input: syn::ItemFn, is_test: bool, config: FinalConfig) -> To
414
411
} ;
415
412
quote ! {
416
413
let body = async #body;
417
- #crate_ident :: pin!( body) ;
414
+ #crate_path :: pin!( body) ;
418
415
let body: :: std:: pin:: Pin <& mut dyn :: std:: future:: Future <Output = #output_type>> = body;
419
416
}
420
417
} else {
0 commit comments