Skip to content

Commit 1ddf870

Browse files
MrGVSVinodentry
authored andcommitted
1 parent a76290d commit 1ddf870

File tree

19 files changed

+1453
-892
lines changed

19 files changed

+1453
-892
lines changed

assets/scenes/load_scene_example.scn.ron

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,41 @@
33
entity: 0,
44
components: [
55
{
6-
"type": "bevy_transform::components::transform::Transform",
7-
"struct": {
8-
"translation": {
9-
"type": "glam::vec3::Vec3",
10-
"value": (0.0, 0.0, 0.0),
11-
},
12-
"rotation": {
13-
"type": "glam::quat::Quat",
14-
"value": (0.0, 0.0, 0.0, 1.0),
15-
},
16-
"scale": {
17-
"type": "glam::vec3::Vec3",
18-
"value": (1.0, 1.0, 1.0),
19-
},
20-
},
6+
"bevy_transform::components::transform::Transform": (
7+
translation: (
8+
x: 0.0,
9+
y: 0.0,
10+
z: 0.0
11+
),
12+
rotation: (0.0, 0.0, 0.0, 1.0),
13+
scale: (
14+
x: 1.0,
15+
y: 1.0,
16+
z: 1.0
17+
),
18+
),
2119
},
2220
{
23-
"type": "scene::ComponentB",
24-
"struct": {
25-
"value": {
26-
"type": "alloc::string::String",
27-
"value": "hello",
28-
},
29-
},
21+
"scene::ComponentB": (
22+
value: "hello",
23+
),
3024
},
3125
{
32-
"type": "scene::ComponentA",
33-
"struct": {
34-
"x": {
35-
"type": "f32",
36-
"value": 1.0,
37-
},
38-
"y": {
39-
"type": "f32",
40-
"value": 2.0,
41-
},
42-
},
26+
"scene::ComponentA": (
27+
x: 1.0,
28+
y: 2.0,
29+
),
4330
},
4431
],
4532
),
4633
(
4734
entity: 1,
4835
components: [
4936
{
50-
"type": "scene::ComponentA",
51-
"struct": {
52-
"x": {
53-
"type": "f32",
54-
"value": 3.0,
55-
},
56-
"y": {
57-
"type": "f32",
58-
"value": 4.0,
59-
},
60-
},
37+
"scene::ComponentA": (
38+
x: 3.0,
39+
y: 4.0,
40+
),
6141
},
6242
],
6343
),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
5353
}
5454
});
5555

56+
let string_name = enum_name.to_string();
5657
let typed_impl = impl_typed(
5758
enum_name,
5859
reflect_enum.meta().generics(),
5960
quote! {
6061
let variants = [#(#variant_info),*];
61-
let info = #bevy_reflect_path::EnumInfo::new::<Self>(&variants);
62+
let info = #bevy_reflect_path::EnumInfo::new::<Self>(#string_name, &variants);
6263
#bevy_reflect_path::TypeInfo::Enum(info)
6364
},
6465
bevy_reflect_path,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream {
5151
}
5252
});
5353

54+
let string_name = struct_name.to_string();
5455
let typed_impl = impl_typed(
5556
struct_name,
5657
reflect_struct.meta().generics(),
5758
quote! {
5859
let fields = [
5960
#(#bevy_reflect_path::NamedField::new::<#field_types, _>(#field_names),)*
6061
];
61-
let info = #bevy_reflect_path::StructInfo::new::<Self>(&fields);
62+
let info = #bevy_reflect_path::StructInfo::new::<Self>(#string_name, &fields);
6263
#bevy_reflect_path::TypeInfo::Struct(info)
6364
},
6465
bevy_reflect_path,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
3535
}
3636
});
3737

38+
let string_name = struct_name.to_string();
3839
let typed_impl = impl_typed(
3940
struct_name,
4041
reflect_struct.meta().generics(),
4142
quote! {
4243
let fields = [
4344
#(#bevy_reflect_path::UnnamedField::new::<#field_types>(#field_idents),)*
4445
];
45-
let info = #bevy_reflect_path::TupleStructInfo::new::<Self>(&fields);
46+
let info = #bevy_reflect_path::TupleStructInfo::new::<Self>(#string_name, &fields);
4647
#bevy_reflect_path::TypeInfo::TupleStruct(info)
4748
},
4849
bevy_reflect_path,

crates/bevy_reflect/src/enums/enum_trait.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{DynamicEnum, Reflect, VariantInfo, VariantType};
22
use bevy_utils::HashMap;
33
use std::any::{Any, TypeId};
4-
use std::borrow::Cow;
54
use std::slice::Iter;
65

76
/// A trait representing a [reflected] enum.
@@ -131,33 +130,40 @@ pub trait Enum: Reflect {
131130
/// A container for compile-time enum info, used by [`TypeInfo`](crate::TypeInfo).
132131
#[derive(Clone, Debug)]
133132
pub struct EnumInfo {
133+
name: &'static str,
134134
type_name: &'static str,
135135
type_id: TypeId,
136136
variants: Box<[VariantInfo]>,
137-
variant_indices: HashMap<Cow<'static, str>, usize>,
137+
variant_names: Box<[&'static str]>,
138+
variant_indices: HashMap<&'static str, usize>,
138139
}
139140

140141
impl EnumInfo {
141142
/// Create a new [`EnumInfo`].
142143
///
143144
/// # Arguments
144145
///
146+
/// * `name`: The name of this enum (_without_ generics or lifetimes)
145147
/// * `variants`: The variants of this enum in the order they are defined
146148
///
147-
pub fn new<TEnum: Enum>(variants: &[VariantInfo]) -> Self {
149+
pub fn new<TEnum: Enum>(name: &'static str, variants: &[VariantInfo]) -> Self {
148150
let variant_indices = variants
149151
.iter()
150152
.enumerate()
151-
.map(|(index, variant)| {
152-
let name = variant.name().clone();
153-
(name, index)
154-
})
153+
.map(|(index, variant)| (variant.name(), index))
155154
.collect::<HashMap<_, _>>();
156155

156+
let variant_names = variants
157+
.iter()
158+
.map(|variant| variant.name())
159+
.collect::<Vec<_>>();
160+
157161
Self {
162+
name,
158163
type_name: std::any::type_name::<TEnum>(),
159164
type_id: TypeId::of::<TEnum>(),
160165
variants: variants.to_vec().into_boxed_slice(),
166+
variant_names: variant_names.into_boxed_slice(),
161167
variant_indices,
162168
}
163169
}
@@ -169,6 +175,11 @@ impl EnumInfo {
169175
.map(|index| &self.variants[*index])
170176
}
171177

178+
/// A slice containing the names of all variants in order.
179+
pub fn variant_names(&self) -> &[&'static str] {
180+
&self.variant_names
181+
}
182+
172183
/// Get a variant at the given index.
173184
pub fn variant_at(&self, index: usize) -> Option<&VariantInfo> {
174185
self.variants.get(index)
@@ -201,6 +212,15 @@ impl EnumInfo {
201212
self.variants.len()
202213
}
203214

215+
/// The name of the enum.
216+
///
217+
/// This does _not_ include any generics or lifetimes.
218+
///
219+
/// For example, `foo::bar::Baz<'a, T>` would simply be `Baz`.
220+
pub fn name(&self) -> &'static str {
221+
self.name
222+
}
223+
204224
/// The [type name] of the enum.
205225
///
206226
/// [type name]: std::any::type_name

crates/bevy_reflect/src/enums/variants.rs

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::{NamedField, UnnamedField};
22
use bevy_utils::HashMap;
3-
use std::borrow::Cow;
43
use std::slice::Iter;
54

65
/// Describes the form of an enum variant.
@@ -66,7 +65,7 @@ pub enum VariantInfo {
6665
}
6766

6867
impl VariantInfo {
69-
pub fn name(&self) -> &Cow<'static, str> {
68+
pub fn name(&self) -> &'static str {
7069
match self {
7170
Self::Struct(info) => info.name(),
7271
Self::Tuple(info) => info.name(),
@@ -78,39 +77,28 @@ impl VariantInfo {
7877
/// Type info for struct variants.
7978
#[derive(Clone, Debug)]
8079
pub struct StructVariantInfo {
81-
name: Cow<'static, str>,
80+
name: &'static str,
8281
fields: Box<[NamedField]>,
83-
field_indices: HashMap<Cow<'static, str>, usize>,
82+
field_names: Box<[&'static str]>,
83+
field_indices: HashMap<&'static str, usize>,
8484
}
8585

8686
impl StructVariantInfo {
8787
/// Create a new [`StructVariantInfo`].
88-
pub fn new(name: &str, fields: &[NamedField]) -> Self {
89-
let field_indices = Self::collect_field_indices(fields);
90-
91-
Self {
92-
name: Cow::Owned(name.into()),
93-
fields: fields.to_vec().into_boxed_slice(),
94-
field_indices,
95-
}
96-
}
97-
98-
/// Create a new [`StructVariantInfo`] using a static string.
99-
///
100-
/// This helps save an allocation when the string has a static lifetime, such
101-
/// as when using defined sa a literal.
102-
pub fn new_static(name: &'static str, fields: &[NamedField]) -> Self {
88+
pub fn new(name: &'static str, fields: &[NamedField]) -> Self {
10389
let field_indices = Self::collect_field_indices(fields);
90+
let field_names = fields.iter().map(|field| field.name()).collect::<Vec<_>>();
10491
Self {
105-
name: Cow::Borrowed(name),
92+
name,
10693
fields: fields.to_vec().into_boxed_slice(),
94+
field_names: field_names.into_boxed_slice(),
10795
field_indices,
10896
}
10997
}
11098

11199
/// The name of this variant.
112-
pub fn name(&self) -> &Cow<'static, str> {
113-
&self.name
100+
pub fn name(&self) -> &'static str {
101+
self.name
114102
}
115103

116104
/// Get the field with the given name.
@@ -120,6 +108,11 @@ impl StructVariantInfo {
120108
.map(|index| &self.fields[*index])
121109
}
122110

111+
/// A slice containing the names of all fields in order.
112+
pub fn field_names(&self) -> &[&'static str] {
113+
&self.field_names
114+
}
115+
123116
/// Get the field at the given index.
124117
pub fn field_at(&self, index: usize) -> Option<&NamedField> {
125118
self.fields.get(index)
@@ -140,48 +133,34 @@ impl StructVariantInfo {
140133
self.fields.len()
141134
}
142135

143-
fn collect_field_indices(fields: &[NamedField]) -> HashMap<Cow<'static, str>, usize> {
136+
fn collect_field_indices(fields: &[NamedField]) -> HashMap<&'static str, usize> {
144137
fields
145138
.iter()
146139
.enumerate()
147-
.map(|(index, field)| {
148-
let name = field.name().clone();
149-
(name, index)
150-
})
140+
.map(|(index, field)| (field.name(), index))
151141
.collect()
152142
}
153143
}
154144

155145
/// Type info for tuple variants.
156146
#[derive(Clone, Debug)]
157147
pub struct TupleVariantInfo {
158-
name: Cow<'static, str>,
148+
name: &'static str,
159149
fields: Box<[UnnamedField]>,
160150
}
161151

162152
impl TupleVariantInfo {
163153
/// Create a new [`TupleVariantInfo`].
164-
pub fn new(name: &str, fields: &[UnnamedField]) -> Self {
165-
Self {
166-
name: Cow::Owned(name.into()),
167-
fields: fields.to_vec().into_boxed_slice(),
168-
}
169-
}
170-
171-
/// Create a new [`TupleVariantInfo`] using a static string.
172-
///
173-
/// This helps save an allocation when the string has a static lifetime, such
174-
/// as when using defined sa a literal.
175-
pub fn new_static(name: &'static str, fields: &[UnnamedField]) -> Self {
154+
pub fn new(name: &'static str, fields: &[UnnamedField]) -> Self {
176155
Self {
177-
name: Cow::Borrowed(name),
156+
name,
178157
fields: fields.to_vec().into_boxed_slice(),
179158
}
180159
}
181160

182161
/// The name of this variant.
183-
pub fn name(&self) -> &Cow<'static, str> {
184-
&self.name
162+
pub fn name(&self) -> &'static str {
163+
self.name
185164
}
186165

187166
/// Get the field at the given index.
@@ -203,29 +182,17 @@ impl TupleVariantInfo {
203182
/// Type info for unit variants.
204183
#[derive(Clone, Debug)]
205184
pub struct UnitVariantInfo {
206-
name: Cow<'static, str>,
185+
name: &'static str,
207186
}
208187

209188
impl UnitVariantInfo {
210189
/// Create a new [`UnitVariantInfo`].
211-
pub fn new(name: &str) -> Self {
212-
Self {
213-
name: Cow::Owned(name.into()),
214-
}
215-
}
216-
217-
/// Create a new [`UnitVariantInfo`] using a static string.
218-
///
219-
/// This helps save an allocation when the string has a static lifetime, such
220-
/// as when using defined sa a literal.
221-
pub fn new_static(name: &'static str) -> Self {
222-
Self {
223-
name: Cow::Borrowed(name),
224-
}
190+
pub fn new(name: &'static str) -> Self {
191+
Self { name }
225192
}
226193

227194
/// The name of this variant.
228-
pub fn name(&self) -> &Cow<'static, str> {
229-
&self.name
195+
pub fn name(&self) -> &'static str {
196+
self.name
230197
}
231198
}

0 commit comments

Comments
 (0)