Skip to content

Commit

Permalink
Revert "Pr bevyengine#5723"
Browse files Browse the repository at this point in the history
This reverts commit 1ddf870.
  • Loading branch information
inodentry committed Aug 21, 2022
1 parent 9e3d8b3 commit a02acfe
Show file tree
Hide file tree
Showing 19 changed files with 892 additions and 1,453 deletions.
68 changes: 44 additions & 24 deletions assets/scenes/load_scene_example.scn.ron
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,61 @@
entity: 0,
components: [
{
"bevy_transform::components::transform::Transform": (
translation: (
x: 0.0,
y: 0.0,
z: 0.0
),
rotation: (0.0, 0.0, 0.0, 1.0),
scale: (
x: 1.0,
y: 1.0,
z: 1.0
),
),
"type": "bevy_transform::components::transform::Transform",
"struct": {
"translation": {
"type": "glam::vec3::Vec3",
"value": (0.0, 0.0, 0.0),
},
"rotation": {
"type": "glam::quat::Quat",
"value": (0.0, 0.0, 0.0, 1.0),
},
"scale": {
"type": "glam::vec3::Vec3",
"value": (1.0, 1.0, 1.0),
},
},
},
{
"scene::ComponentB": (
value: "hello",
),
"type": "scene::ComponentB",
"struct": {
"value": {
"type": "alloc::string::String",
"value": "hello",
},
},
},
{
"scene::ComponentA": (
x: 1.0,
y: 2.0,
),
"type": "scene::ComponentA",
"struct": {
"x": {
"type": "f32",
"value": 1.0,
},
"y": {
"type": "f32",
"value": 2.0,
},
},
},
],
),
(
entity: 1,
components: [
{
"scene::ComponentA": (
x: 3.0,
y: 4.0,
),
"type": "scene::ComponentA",
"struct": {
"x": {
"type": "f32",
"value": 3.0,
},
"y": {
"type": "f32",
"value": 4.0,
},
},
},
],
),
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
}
});

let string_name = enum_name.to_string();
let typed_impl = impl_typed(
enum_name,
reflect_enum.meta().generics(),
quote! {
let variants = [#(#variant_info),*];
let info = #bevy_reflect_path::EnumInfo::new::<Self>(#string_name, &variants);
let info = #bevy_reflect_path::EnumInfo::new::<Self>(&variants);
#bevy_reflect_path::TypeInfo::Enum(info)
},
bevy_reflect_path,
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream {
}
});

let string_name = struct_name.to_string();
let typed_impl = impl_typed(
struct_name,
reflect_struct.meta().generics(),
quote! {
let fields = [
#(#bevy_reflect_path::NamedField::new::<#field_types, _>(#field_names),)*
];
let info = #bevy_reflect_path::StructInfo::new::<Self>(#string_name, &fields);
let info = #bevy_reflect_path::StructInfo::new::<Self>(&fields);
#bevy_reflect_path::TypeInfo::Struct(info)
},
bevy_reflect_path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
}
});

let string_name = struct_name.to_string();
let typed_impl = impl_typed(
struct_name,
reflect_struct.meta().generics(),
quote! {
let fields = [
#(#bevy_reflect_path::UnnamedField::new::<#field_types>(#field_idents),)*
];
let info = #bevy_reflect_path::TupleStructInfo::new::<Self>(#string_name, &fields);
let info = #bevy_reflect_path::TupleStructInfo::new::<Self>(&fields);
#bevy_reflect_path::TypeInfo::TupleStruct(info)
},
bevy_reflect_path,
Expand Down
34 changes: 7 additions & 27 deletions crates/bevy_reflect/src/enums/enum_trait.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{DynamicEnum, Reflect, VariantInfo, VariantType};
use bevy_utils::HashMap;
use std::any::{Any, TypeId};
use std::borrow::Cow;
use std::slice::Iter;

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

impl EnumInfo {
/// Create a new [`EnumInfo`].
///
/// # Arguments
///
/// * `name`: The name of this enum (_without_ generics or lifetimes)
/// * `variants`: The variants of this enum in the order they are defined
///
pub fn new<TEnum: Enum>(name: &'static str, variants: &[VariantInfo]) -> Self {
pub fn new<TEnum: Enum>(variants: &[VariantInfo]) -> Self {
let variant_indices = variants
.iter()
.enumerate()
.map(|(index, variant)| (variant.name(), index))
.map(|(index, variant)| {
let name = variant.name().clone();
(name, index)
})
.collect::<HashMap<_, _>>();

let variant_names = variants
.iter()
.map(|variant| variant.name())
.collect::<Vec<_>>();

Self {
name,
type_name: std::any::type_name::<TEnum>(),
type_id: TypeId::of::<TEnum>(),
variants: variants.to_vec().into_boxed_slice(),
variant_names: variant_names.into_boxed_slice(),
variant_indices,
}
}
Expand All @@ -175,11 +169,6 @@ impl EnumInfo {
.map(|index| &self.variants[*index])
}

/// A slice containing the names of all variants in order.
pub fn variant_names(&self) -> &[&'static str] {
&self.variant_names
}

/// Get a variant at the given index.
pub fn variant_at(&self, index: usize) -> Option<&VariantInfo> {
self.variants.get(index)
Expand Down Expand Up @@ -212,15 +201,6 @@ impl EnumInfo {
self.variants.len()
}

/// The name of the enum.
///
/// This does _not_ include any generics or lifetimes.
///
/// For example, `foo::bar::Baz<'a, T>` would simply be `Baz`.
pub fn name(&self) -> &'static str {
self.name
}

/// The [type name] of the enum.
///
/// [type name]: std::any::type_name
Expand Down
87 changes: 60 additions & 27 deletions crates/bevy_reflect/src/enums/variants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{NamedField, UnnamedField};
use bevy_utils::HashMap;
use std::borrow::Cow;
use std::slice::Iter;

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

impl VariantInfo {
pub fn name(&self) -> &'static str {
pub fn name(&self) -> &Cow<'static, str> {
match self {
Self::Struct(info) => info.name(),
Self::Tuple(info) => info.name(),
Expand All @@ -77,28 +78,39 @@ impl VariantInfo {
/// Type info for struct variants.
#[derive(Clone, Debug)]
pub struct StructVariantInfo {
name: &'static str,
name: Cow<'static, str>,
fields: Box<[NamedField]>,
field_names: Box<[&'static str]>,
field_indices: HashMap<&'static str, usize>,
field_indices: HashMap<Cow<'static, str>, usize>,
}

impl StructVariantInfo {
/// Create a new [`StructVariantInfo`].
pub fn new(name: &'static str, fields: &[NamedField]) -> Self {
pub fn new(name: &str, fields: &[NamedField]) -> Self {
let field_indices = Self::collect_field_indices(fields);

Self {
name: Cow::Owned(name.into()),
fields: fields.to_vec().into_boxed_slice(),
field_indices,
}
}

/// Create a new [`StructVariantInfo`] using a static string.
///
/// This helps save an allocation when the string has a static lifetime, such
/// as when using defined sa a literal.
pub fn new_static(name: &'static str, fields: &[NamedField]) -> Self {
let field_indices = Self::collect_field_indices(fields);
let field_names = fields.iter().map(|field| field.name()).collect::<Vec<_>>();
Self {
name,
name: Cow::Borrowed(name),
fields: fields.to_vec().into_boxed_slice(),
field_names: field_names.into_boxed_slice(),
field_indices,
}
}

/// The name of this variant.
pub fn name(&self) -> &'static str {
self.name
pub fn name(&self) -> &Cow<'static, str> {
&self.name
}

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

/// A slice containing the names of all fields in order.
pub fn field_names(&self) -> &[&'static str] {
&self.field_names
}

/// Get the field at the given index.
pub fn field_at(&self, index: usize) -> Option<&NamedField> {
self.fields.get(index)
Expand All @@ -133,34 +140,48 @@ impl StructVariantInfo {
self.fields.len()
}

fn collect_field_indices(fields: &[NamedField]) -> HashMap<&'static str, usize> {
fn collect_field_indices(fields: &[NamedField]) -> HashMap<Cow<'static, str>, usize> {
fields
.iter()
.enumerate()
.map(|(index, field)| (field.name(), index))
.map(|(index, field)| {
let name = field.name().clone();
(name, index)
})
.collect()
}
}

/// Type info for tuple variants.
#[derive(Clone, Debug)]
pub struct TupleVariantInfo {
name: &'static str,
name: Cow<'static, str>,
fields: Box<[UnnamedField]>,
}

impl TupleVariantInfo {
/// Create a new [`TupleVariantInfo`].
pub fn new(name: &'static str, fields: &[UnnamedField]) -> Self {
pub fn new(name: &str, fields: &[UnnamedField]) -> Self {
Self {
name: Cow::Owned(name.into()),
fields: fields.to_vec().into_boxed_slice(),
}
}

/// Create a new [`TupleVariantInfo`] using a static string.
///
/// This helps save an allocation when the string has a static lifetime, such
/// as when using defined sa a literal.
pub fn new_static(name: &'static str, fields: &[UnnamedField]) -> Self {
Self {
name,
name: Cow::Borrowed(name),
fields: fields.to_vec().into_boxed_slice(),
}
}

/// The name of this variant.
pub fn name(&self) -> &'static str {
self.name
pub fn name(&self) -> &Cow<'static, str> {
&self.name
}

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

impl UnitVariantInfo {
/// Create a new [`UnitVariantInfo`].
pub fn new(name: &'static str) -> Self {
Self { name }
pub fn new(name: &str) -> Self {
Self {
name: Cow::Owned(name.into()),
}
}

/// Create a new [`UnitVariantInfo`] using a static string.
///
/// This helps save an allocation when the string has a static lifetime, such
/// as when using defined sa a literal.
pub fn new_static(name: &'static str) -> Self {
Self {
name: Cow::Borrowed(name),
}
}

/// The name of this variant.
pub fn name(&self) -> &'static str {
self.name
pub fn name(&self) -> &Cow<'static, str> {
&self.name
}
}
Loading

0 comments on commit a02acfe

Please sign in to comment.