Skip to content

Commit 1586650

Browse files
committed
Revert "Create default Enum impls"
This reverts commit 91409a061106a5ba64ce579c09e6fce4af710197. This was done in response to the poor performance of using TypeInfo directly: bevyengine#4042 (comment)
1 parent 7f75d76 commit 1586650

File tree

4 files changed

+105
-96
lines changed

4 files changed

+105
-96
lines changed

crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
1818
variant_info,
1919
enum_field,
2020
enum_field_at,
21+
enum_index_of,
22+
enum_name_at,
23+
enum_field_len,
2124
enum_variant_name,
25+
enum_variant_type,
2226
} = generate_impls(reflect_enum, &ref_index, &ref_name);
2327

2428
let EnumVariantConstructors {
@@ -37,7 +41,10 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
3741
}
3842
}
3943
});
40-
let debug_fn = reflect_enum.meta().traits().get_debug_impl();
44+
let debug_fn = reflect_enum
45+
.meta()
46+
.traits()
47+
.get_debug_impl();
4148
let partial_eq_fn = reflect_enum
4249
.meta()
4350
.traits()
@@ -99,10 +106,32 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
99106
}
100107
}
101108

109+
fn index_of(&self, #ref_name: &str) -> Option<usize> {
110+
match self {
111+
#(#enum_index_of,)*
112+
_ => None,
113+
}
114+
}
115+
116+
fn name_at(&self, #ref_index: usize) -> Option<&str> {
117+
match self {
118+
#(#enum_name_at,)*
119+
_ => None,
120+
}
121+
}
122+
102123
fn iter_fields(&self) -> #bevy_reflect_path::VariantFieldIter {
103124
#bevy_reflect_path::VariantFieldIter::new(self)
104125
}
105126

127+
#[inline]
128+
fn field_len(&self) -> usize {
129+
match self {
130+
#(#enum_field_len,)*
131+
_ => 0,
132+
}
133+
}
134+
106135
#[inline]
107136
fn variant_name(&self) -> &str {
108137
match self {
@@ -111,6 +140,14 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
111140
}
112141
}
113142

143+
#[inline]
144+
fn variant_type(&self) -> #bevy_reflect_path::VariantType {
145+
match self {
146+
#(#enum_variant_type,)*
147+
_ => unreachable!(),
148+
}
149+
}
150+
114151
fn clone_dynamic(&self) -> #bevy_reflect_path::DynamicEnum {
115152
#bevy_reflect_path::DynamicEnum::from_ref::<Self>(self)
116153
}
@@ -217,7 +254,11 @@ struct EnumImpls {
217254
variant_info: Vec<proc_macro2::TokenStream>,
218255
enum_field: Vec<proc_macro2::TokenStream>,
219256
enum_field_at: Vec<proc_macro2::TokenStream>,
257+
enum_index_of: Vec<proc_macro2::TokenStream>,
258+
enum_name_at: Vec<proc_macro2::TokenStream>,
259+
enum_field_len: Vec<proc_macro2::TokenStream>,
220260
enum_variant_name: Vec<proc_macro2::TokenStream>,
261+
enum_variant_type: Vec<proc_macro2::TokenStream>,
221262
}
222263

223264
fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Ident) -> EnumImpls {
@@ -226,7 +267,11 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
226267
let mut variant_info: Vec<proc_macro2::TokenStream> = Vec::new();
227268
let mut enum_field: Vec<proc_macro2::TokenStream> = Vec::new();
228269
let mut enum_field_at: Vec<proc_macro2::TokenStream> = Vec::new();
270+
let mut enum_index_of: Vec<proc_macro2::TokenStream> = Vec::new();
271+
let mut enum_name_at: Vec<proc_macro2::TokenStream> = Vec::new();
272+
let mut enum_field_len: Vec<proc_macro2::TokenStream> = Vec::new();
229273
let mut enum_variant_name: Vec<proc_macro2::TokenStream> = Vec::new();
274+
let mut enum_variant_type: Vec<proc_macro2::TokenStream> = Vec::new();
230275

231276
for variant in reflect_enum.active_variants() {
232277
let ident = &variant.data.ident;
@@ -243,6 +288,9 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
243288
enum_variant_name.push(quote! {
244289
#unit => #name
245290
});
291+
enum_variant_type.push(quote! {
292+
#unit => #bevy_reflect_path::VariantType::Unit
293+
});
246294
}
247295
EnumVariantFields::Unnamed(fields) => {
248296
let mut field_info = Vec::new();
@@ -266,9 +314,16 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
266314
field_idx += 1;
267315
}
268316

317+
let field_len = field_idx;
318+
enum_field_len.push(quote! {
319+
#unit(..) => #field_len
320+
});
269321
enum_variant_name.push(quote! {
270322
#unit(..) => #name
271323
});
324+
enum_variant_type.push(quote! {
325+
#unit(..) => #bevy_reflect_path::VariantType::Tuple
326+
});
272327
variant_info.push(quote! {
273328
#bevy_reflect_path::VariantInfo::Tuple(
274329
#bevy_reflect_path::TupleVariantInfo::new(#name, &[
@@ -295,6 +350,12 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
295350
enum_field_at.push(quote! {
296351
#unit{ #field_ident, .. } if #ref_index == #field_idx => Some(#field_ident)
297352
});
353+
enum_index_of.push(quote! {
354+
#unit{ .. } if #ref_name == #field_name => Some(#field_idx)
355+
});
356+
enum_name_at.push(quote! {
357+
#unit{ .. } if #ref_index == #field_idx => Some(#field_name)
358+
});
298359

299360
let field_ty = &field.data.ty;
300361
field_info.push(quote! {
@@ -304,9 +365,16 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
304365
field_idx += 1;
305366
}
306367

368+
let field_len = field_idx;
369+
enum_field_len.push(quote! {
370+
#unit{..} => #field_len
371+
});
307372
enum_variant_name.push(quote! {
308373
#unit{..} => #name
309374
});
375+
enum_variant_type.push(quote! {
376+
#unit{..} => #bevy_reflect_path::VariantType::Struct
377+
});
310378
variant_info.push(quote! {
311379
#bevy_reflect_path::VariantInfo::Struct(
312380
#bevy_reflect_path::StructVariantInfo::new(#name, &[
@@ -322,6 +390,10 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
322390
variant_info,
323391
enum_field,
324392
enum_field_at,
393+
enum_index_of,
394+
enum_name_at,
395+
enum_field_len,
325396
enum_variant_name,
397+
enum_variant_type,
326398
}
327399
}

crates/bevy_reflect/src/enums/enum_trait.rs

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::{DynamicEnum, Reflect, TypeInfo, VariantInfo, VariantType};
1+
use crate::{DynamicEnum, Reflect, VariantInfo, VariantType};
22
use bevy_utils::HashMap;
33
use std::any::{Any, TypeId};
4-
use std::borrow::{Borrow, Cow};
4+
use std::borrow::Cow;
55
use std::slice::Iter;
66

77
/// A trait representing a [reflected] enum.
@@ -93,61 +93,24 @@ pub trait Enum: Reflect {
9393
fn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>;
9494
/// Returns a mutable reference to the value of the field (in the current variant) at the given index.
9595
fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>;
96+
/// Returns the index of the field (in the current variant) with the given name.
97+
///
98+
/// For non-[`VariantType::Struct`] variants, this should return `None`.
99+
fn index_of(&self, name: &str) -> Option<usize>;
100+
/// Returns the name of the field (in the current variant) with the given index.
101+
///
102+
/// For non-[`VariantType::Struct`] variants, this should return `None`.
103+
fn name_at(&self, index: usize) -> Option<&str>;
96104
/// Returns an iterator over the values of the current variant's fields.
97105
fn iter_fields(&self) -> VariantFieldIter;
106+
/// Returns the number of fields in the current variant.
107+
fn field_len(&self) -> usize;
98108
/// The name of the current variant.
99109
fn variant_name(&self) -> &str;
110+
/// The type of the current variant.
111+
fn variant_type(&self) -> VariantType;
100112
// Clones the enum into a [`DynamicEnum`].
101113
fn clone_dynamic(&self) -> DynamicEnum;
102-
/// The type of the current variant.
103-
fn variant_type(&self) -> VariantType {
104-
if let TypeInfo::Enum(info) = self.get_type_info() {
105-
if let Some(variant) = info.variant(self.variant_name()) {
106-
variant.variant_type()
107-
} else {
108-
panic!("invalid variant: `{}`", self.variant_name());
109-
}
110-
} else {
111-
panic!(
112-
"`{:?}` is not `TypeInfo::Enum`",
113-
std::any::type_name::<Self>()
114-
);
115-
}
116-
}
117-
/// Returns the name of the field (in the current variant) with the given index.
118-
///
119-
/// For non-[`VariantType::Struct`] variants, this should return `None`.
120-
fn name_at(&self, index: usize) -> Option<&str> {
121-
if let TypeInfo::Enum(info) = self.get_type_info() {
122-
match info.variant(self.variant_name())? {
123-
VariantInfo::Struct(variant) => {
124-
variant.field_at(index).map(|field| field.name().borrow())
125-
}
126-
_ => None,
127-
}
128-
} else {
129-
panic!(
130-
"`{:?}` is not `TypeInfo::Enum`",
131-
std::any::type_name::<Self>()
132-
);
133-
}
134-
}
135-
/// Returns the index of the field (in the current variant) with the given name.
136-
///
137-
/// For non-[`VariantType::Struct`] variants, this should return `None`.
138-
fn index_of(&self, name: &str) -> Option<usize> {
139-
if let TypeInfo::Enum(info) = self.get_type_info() {
140-
match info.variant(self.variant_name())? {
141-
VariantInfo::Struct(variant) => variant.index_of(name),
142-
_ => None,
143-
}
144-
} else {
145-
panic!(
146-
"`{:?}` is not `TypeInfo::Enum`",
147-
std::any::type_name::<Self>()
148-
);
149-
}
150-
}
151114
/// Returns true if the current variant's type matches the given one.
152115
fn is_variant(&self, variant_type: VariantType) -> bool {
153116
self.variant_type() == variant_type
@@ -156,25 +119,6 @@ pub trait Enum: Reflect {
156119
fn variant_path(&self) -> String {
157120
format!("{}::{}", self.type_name(), self.variant_name())
158121
}
159-
/// Returns the number of fields in the current variant.
160-
fn field_len(&self) -> usize {
161-
if let TypeInfo::Enum(info) = self.get_type_info() {
162-
if let Some(variant) = info.variant(self.variant_name()) {
163-
match variant {
164-
VariantInfo::Unit(..) => 0,
165-
VariantInfo::Tuple(variant_info) => variant_info.field_len(),
166-
VariantInfo::Struct(variant_info) => variant_info.field_len(),
167-
}
168-
} else {
169-
panic!("invalid variant: `{}`", self.variant_name());
170-
}
171-
} else {
172-
panic!(
173-
"`{:?}` is not `TypeInfo::Enum`",
174-
std::any::type_name::<Self>()
175-
);
176-
}
177-
}
178122
}
179123

180124
/// A container for compile-time enum info.

crates/bevy_reflect/src/enums/variants.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,6 @@ impl VariantInfo {
7373
Self::Unit(info) => info.name(),
7474
}
7575
}
76-
pub fn variant_type(&self) -> VariantType {
77-
match self {
78-
Self::Struct(..) => VariantType::Struct,
79-
Self::Tuple(..) => VariantType::Tuple,
80-
Self::Unit(..) => VariantType::Unit,
81-
}
82-
}
8376
}
8477

8578
/// Type info for struct variants.

crates/bevy_reflect/src/impls/std.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,26 @@ impl<T: Reflect + Clone> Enum for Option<T> {
574574
}
575575
}
576576

577+
fn index_of(&self, _name: &str) -> Option<usize> {
578+
None
579+
}
580+
581+
fn name_at(&self, _index: usize) -> Option<&str> {
582+
None
583+
}
584+
577585
fn iter_fields(&self) -> VariantFieldIter {
578586
VariantFieldIter::new(self)
579587
}
580588

589+
#[inline]
590+
fn field_len(&self) -> usize {
591+
match self {
592+
Some(..) => 1,
593+
None => 0,
594+
}
595+
}
596+
581597
#[inline]
582598
fn variant_name(&self) -> &str {
583599
match self {
@@ -586,10 +602,6 @@ impl<T: Reflect + Clone> Enum for Option<T> {
586602
}
587603
}
588604

589-
fn clone_dynamic(&self) -> DynamicEnum {
590-
DynamicEnum::from_ref::<Self>(self)
591-
}
592-
593605
#[inline]
594606
fn variant_type(&self) -> VariantType {
595607
match self {
@@ -598,20 +610,8 @@ impl<T: Reflect + Clone> Enum for Option<T> {
598610
}
599611
}
600612

601-
fn name_at(&self, _index: usize) -> Option<&str> {
602-
None
603-
}
604-
605-
fn index_of(&self, _name: &str) -> Option<usize> {
606-
None
607-
}
608-
609-
#[inline]
610-
fn field_len(&self) -> usize {
611-
match self {
612-
Some(..) => 1,
613-
None => 0,
614-
}
613+
fn clone_dynamic(&self) -> DynamicEnum {
614+
DynamicEnum::from_ref::<Self>(self)
615615
}
616616
}
617617

0 commit comments

Comments
 (0)