7575mod image;
7676
7777use proc_macro:: TokenStream ;
78- use proc_macro2:: { Delimiter , Group , Ident , Span , TokenTree } ;
78+ use proc_macro2:: { Delimiter , Group , Span , TokenTree } ;
7979
80- use syn:: {
81- ImplItemFn , ItemFn , Token , punctuated:: Punctuated , spanned:: Spanned , visit_mut:: VisitMut ,
82- } ;
80+ use syn:: { ImplItemFn , visit_mut:: VisitMut } ;
8381
8482use quote:: { ToTokens , quote} ;
8583use std:: fmt:: Write ;
@@ -224,150 +222,6 @@ pub fn gpu_only(_attr: TokenStream, item: TokenStream) -> TokenStream {
224222 output. into ( )
225223}
226224
227- /// Accepts a function with an argument named `component`, and outputs the
228- /// function plus a vectorized version of the function which accepts a vector
229- /// of `component`. This is mostly useful when you have the same impl body for
230- /// a scalar and vector versions of the same operation.
231- #[ proc_macro_attribute]
232- #[ doc( hidden) ]
233- pub fn vectorized ( _attr : TokenStream , item : TokenStream ) -> TokenStream {
234- let function = syn:: parse_macro_input!( item as syn:: ItemFn ) ;
235- let vectored_function = match create_vectored_fn ( function. clone ( ) ) {
236- Ok ( val) => val,
237- Err ( err) => return err. to_compile_error ( ) . into ( ) ,
238- } ;
239-
240- let output = quote:: quote!(
241- #function
242-
243- #vectored_function
244- ) ;
245-
246- output. into ( )
247- }
248-
249- fn create_vectored_fn (
250- ItemFn {
251- attrs,
252- vis,
253- mut sig,
254- block,
255- } : ItemFn ,
256- ) -> Result < ItemFn , syn:: Error > {
257- const COMPONENT_ARG_NAME : & str = "component" ;
258- let trait_bound_name = Ident :: new ( "VECTOR" , Span :: mixed_site ( ) ) ;
259- let const_bound_name = Ident :: new ( "LENGTH" , Span :: mixed_site ( ) ) ;
260-
261- sig. ident = Ident :: new ( & format ! ( "{}_vector" , sig. ident) , Span :: mixed_site ( ) ) ;
262- sig. output = syn:: ReturnType :: Type (
263- Default :: default ( ) ,
264- Box :: new ( path_from_ident ( trait_bound_name. clone ( ) ) ) ,
265- ) ;
266-
267- let component_type = sig. inputs . iter_mut ( ) . find_map ( |x| match x {
268- syn:: FnArg :: Typed ( ty) => match & * ty. pat {
269- syn:: Pat :: Ident ( pat) if pat. ident == COMPONENT_ARG_NAME => Some ( & mut ty. ty ) ,
270- _ => None ,
271- } ,
272- syn:: FnArg :: Receiver ( _) => None ,
273- } ) ;
274-
275- if component_type. is_none ( ) {
276- return Err ( syn:: Error :: new (
277- sig. inputs . span ( ) ,
278- "#[vectorized] requires an argument named `component`." ,
279- ) ) ;
280- }
281- let component_type = component_type. unwrap ( ) ;
282-
283- let vector_path = {
284- let mut path = syn:: Path {
285- leading_colon : None ,
286- segments : Punctuated :: new ( ) ,
287- } ;
288-
289- for segment in & [ "crate" , "vector" ] {
290- path. segments
291- . push ( Ident :: new ( segment, Span :: mixed_site ( ) ) . into ( ) ) ;
292- }
293-
294- path. segments . push ( syn:: PathSegment {
295- ident : Ident :: new ( "Vector" , Span :: mixed_site ( ) ) ,
296- arguments : syn:: PathArguments :: AngleBracketed ( syn:: AngleBracketedGenericArguments {
297- colon2_token : None ,
298- lt_token : Default :: default ( ) ,
299- args : {
300- let mut punct = Punctuated :: new ( ) ;
301-
302- punct. push ( syn:: GenericArgument :: Type ( * component_type. clone ( ) ) ) ;
303- punct. push ( syn:: GenericArgument :: Type ( path_from_ident (
304- const_bound_name. clone ( ) ,
305- ) ) ) ;
306-
307- punct
308- } ,
309- gt_token : Default :: default ( ) ,
310- } ) ,
311- } ) ;
312-
313- path
314- } ;
315-
316- // Replace the original component type with vector version.
317- * * component_type = path_from_ident ( trait_bound_name. clone ( ) ) ;
318-
319- let trait_bounds = {
320- let mut punct = Punctuated :: new ( ) ;
321- punct. push ( syn:: TypeParamBound :: Trait ( syn:: TraitBound {
322- paren_token : None ,
323- modifier : syn:: TraitBoundModifier :: None ,
324- lifetimes : None ,
325- path : vector_path,
326- } ) ) ;
327- punct
328- } ;
329-
330- sig. generics
331- . params
332- . push ( syn:: GenericParam :: Type ( syn:: TypeParam {
333- attrs : Vec :: new ( ) ,
334- ident : trait_bound_name,
335- colon_token : Some ( Token ! [ : ] ( Span :: mixed_site ( ) ) ) ,
336- bounds : trait_bounds,
337- eq_token : None ,
338- default : None ,
339- } ) ) ;
340-
341- sig. generics
342- . params
343- . push ( syn:: GenericParam :: Const ( syn:: ConstParam {
344- attrs : Vec :: default ( ) ,
345- const_token : Default :: default ( ) ,
346- ident : const_bound_name,
347- colon_token : Default :: default ( ) ,
348- ty : syn:: Type :: Path ( syn:: TypePath {
349- qself : None ,
350- path : Ident :: new ( "usize" , Span :: mixed_site ( ) ) . into ( ) ,
351- } ) ,
352- eq_token : None ,
353- default : None ,
354- } ) ) ;
355-
356- Ok ( ItemFn {
357- attrs,
358- vis,
359- sig,
360- block,
361- } )
362- }
363-
364- fn path_from_ident ( ident : Ident ) -> syn:: Type {
365- syn:: Type :: Path ( syn:: TypePath {
366- qself : None ,
367- path : syn:: Path :: from ( ident) ,
368- } )
369- }
370-
371225/// Print a formatted string with a newline using the debug printf extension.
372226///
373227/// Examples:
@@ -392,7 +246,7 @@ pub fn debug_printfln(input: TokenStream) -> TokenStream {
392246}
393247
394248struct DebugPrintfInput {
395- span : proc_macro2 :: Span ,
249+ span : Span ,
396250 format_string : String ,
397251 variables : Vec < syn:: Expr > ,
398252}
@@ -424,7 +278,7 @@ impl syn::parse::Parse for DebugPrintfInput {
424278 }
425279}
426280
427- fn parsing_error ( message : & str , span : proc_macro2 :: Span ) -> TokenStream {
281+ fn parsing_error ( message : & str , span : Span ) -> TokenStream {
428282 syn:: Error :: new ( span, message) . to_compile_error ( ) . into ( )
429283}
430284
0 commit comments