@@ -3,7 +3,7 @@ use crate::field_attributes::{parse_field_attrs, ReflectFieldAttr};
3
3
use crate :: type_path:: parse_path_no_leading_colon;
4
4
use crate :: utility:: members_to_serialization_denylist;
5
5
use bit_set:: BitSet ;
6
- use quote:: { quote, ToTokens } ;
6
+ use quote:: { quote, ToTokens , TokenStreamExt } ;
7
7
8
8
use crate :: {
9
9
utility, REFLECT_ATTRIBUTE_NAME , REFLECT_VALUE_ATTRIBUTE_NAME , TYPE_NAME_ATTRIBUTE_NAME ,
@@ -225,7 +225,7 @@ impl<'a> ReflectDerive<'a> {
225
225
}
226
226
227
227
let path_to_type = PathToType :: Internal {
228
- ident : & input. ident ,
228
+ name : & input. ident ,
229
229
alias : alias_type_path,
230
230
} ;
231
231
@@ -474,23 +474,27 @@ impl<'a> ReflectEnum<'a> {
474
474
}
475
475
}
476
476
477
+ /// Represents a path to a type.
477
478
pub ( crate ) enum PathToType < ' a > {
478
- /// Types without a crate/module (e.g. `bool`).
479
+ /// Types without a crate/module that can be named from any scope (e.g. `bool`).
479
480
Primitive ( & ' a Ident ) ,
480
481
/// Using `::my_crate::foo::Bar` syntax.
481
482
///
482
483
/// May have a seperate alias path used for the `TypePath` implementation.
483
484
External { path : & ' a Path , alias : Option < Path > } ,
484
- /// The name of a type relative to itself .
485
- /// Module and crate are found with [`module_path!()`](core::module_path) .
485
+ /// The name of a type relative to its scope .
486
+ /// The type must be able to be named from just its name .
486
487
///
487
488
/// May have a seperate alias path used for the `TypePath` implementation.
489
+ ///
490
+ /// Module and crate are found with [`module_path!()`](core::module_path),
491
+ /// if there is no alias specified.
488
492
Internal {
489
- ident : & ' a Ident ,
493
+ name : & ' a Ident ,
490
494
alias : Option < Path > ,
491
495
} ,
492
496
/// Any [`syn::Type`] with only a defined `type_path` and `short_type_path`.
493
- #[ allow( dead_code) ]
497
+ #[ allow( dead_code) ] // Not currently used but may be useful in the future due to its generality.
494
498
Anonymous {
495
499
qualified_type : Type ,
496
500
long_type_path : proc_macro2:: TokenStream ,
@@ -500,10 +504,10 @@ pub(crate) enum PathToType<'a> {
500
504
501
505
impl < ' a > PathToType < ' a > {
502
506
/// Returns the path interpreted as an [`Ident`],
503
- /// or [`None`] if anonymous.
507
+ /// or [`None`] if anonymous or primitive .
504
508
fn named_as_ident ( & self ) -> Option < & Ident > {
505
509
match self {
506
- Self :: Internal { ident, alias } => Some (
510
+ Self :: Internal { name : ident, alias } => Some (
507
511
alias
508
512
. as_ref ( )
509
513
. map ( |path| & path. segments . last ( ) . unwrap ( ) . ident )
@@ -523,7 +527,7 @@ impl<'a> PathToType<'a> {
523
527
}
524
528
525
529
/// Returns the path interpreted as a [`Path`],
526
- /// or [`None`] if anonymous or a non-aliased [`PathToType::Internal`].
530
+ /// or [`None`] if anonymous, primitive, or a non-aliased [`PathToType::Internal`].
527
531
fn named_as_path ( & self ) -> Option < & Path > {
528
532
match self {
529
533
Self :: Internal { alias, .. } => alias. as_ref ( ) ,
@@ -536,7 +540,7 @@ impl<'a> PathToType<'a> {
536
540
///
537
541
/// [internal]: PathToType::Internal
538
542
/// [external]: PathToType::External
539
- pub fn is_alias ( & self ) -> bool {
543
+ pub fn is_aliased ( & self ) -> bool {
540
544
match self {
541
545
Self :: Internal { alias, .. } => alias. is_some ( ) ,
542
546
Self :: External { alias, .. } => alias. is_some ( ) ,
@@ -635,93 +639,9 @@ impl<'a> PathToType<'a> {
635
639
impl < ' a > ToTokens for PathToType < ' a > {
636
640
fn to_tokens ( & self , tokens : & mut proc_macro2:: TokenStream ) {
637
641
match self {
638
- Self :: Internal { ident, .. } | Self :: Primitive ( ident) => ident. to_tokens ( tokens) ,
642
+ Self :: Internal { name : ident, .. } | Self :: Primitive ( ident) => ident. to_tokens ( tokens) ,
639
643
Self :: External { path, .. } => path. to_tokens ( tokens) ,
640
644
Self :: Anonymous { qualified_type, .. } => qualified_type. to_tokens ( tokens) ,
641
645
}
642
646
}
643
- }
644
-
645
- pub ( crate ) fn type_path_generator ( meta : & ReflectMeta ) -> proc_macro2:: TokenStream {
646
- let path_to_type = meta. path_to_type ( ) ;
647
- let generics = meta. generics ( ) ;
648
- let bevy_reflect_path = meta. bevy_reflect_path ( ) ;
649
- // Whether to use `GenericTypedCell` is not dependent on lifetimes
650
- // (which all have to be 'static anyway).
651
- let is_generic = !generics
652
- . params
653
- . iter ( )
654
- . all ( |param| matches ! ( param, GenericParam :: Lifetime ( _) ) ) ;
655
- let generic_type_paths: Vec < proc_macro2:: TokenStream > = generics
656
- . type_params ( )
657
- . map ( |param| {
658
- let ident = & param. ident ;
659
- quote ! {
660
- <#ident as #bevy_reflect_path:: TypePath >
661
- }
662
- } )
663
- . collect ( ) ;
664
-
665
- let ident = path_to_type. ident ( ) . unwrap ( ) . to_string ( ) ;
666
- let ident = LitStr :: new ( & ident, path_to_type. span ( ) ) ;
667
-
668
- let path = {
669
- let path = path_to_type. long_type_path ( ) ;
670
-
671
- if is_generic {
672
- let generics = generic_type_paths. iter ( ) . map ( |type_path| {
673
- quote ! {
674
- #type_path:: type_path( )
675
- }
676
- } ) ;
677
-
678
- quote ! {
679
- #path + "::<" #( + #generics) * + ">"
680
- }
681
- } else {
682
- quote ! {
683
- #path
684
- }
685
- }
686
- } ;
687
-
688
- let short_path = {
689
- if is_generic {
690
- let generics = generic_type_paths. iter ( ) . map ( |type_path| {
691
- quote ! {
692
- #type_path:: short_type_path( )
693
- }
694
- } ) ;
695
-
696
- quote ! {
697
- :: core:: concat!( #ident, "<" ) . to_owned( ) #( + #generics) * + ">"
698
- }
699
- } else {
700
- quote ! {
701
- #ident. to_owned( )
702
- }
703
- }
704
- } ;
705
-
706
- if !path_to_type. is_named ( ) {
707
- quote ! {
708
- #bevy_reflect_path:: utility:: TypePathStorage :: new_anonymous(
709
- #path,
710
- #short_path,
711
- )
712
- }
713
- } else {
714
- let crate_name = path_to_type. crate_name ( ) ;
715
- let module_path = path_to_type. module_path ( ) ;
716
-
717
- quote ! {
718
- #bevy_reflect_path:: utility:: TypePathStorage :: new_named(
719
- #path,
720
- #short_path,
721
- #ident. to_owned( ) ,
722
- #crate_name. to_owned( ) ,
723
- #module_path. to_owned( ) ,
724
- )
725
- }
726
- }
727
- }
647
+ }
0 commit comments