Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions crates/bevy_reflect/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
utility::NonGenericTypeInfoCell, DynamicInfo, Reflect, ReflectMut, ReflectOwned, ReflectRef,
TypeInfo, Typed,
TypeInfo, Typed, ValueInfo,
};
use std::{
any::{Any, TypeId},
Expand Down Expand Up @@ -45,10 +45,8 @@ pub trait Array: Reflect {
/// A container for compile-time array info.
#[derive(Clone, Debug)]
pub struct ArrayInfo {
type_name: &'static str,
type_id: TypeId,
item_type_name: &'static str,
item_type_id: TypeId,
type_value: ValueInfo,
item_type_value: ValueInfo,
Comment on lines +48 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bikeshed: I think these names are fine, but here are some others to consider:

  1. container_type, item_type, key_type, field_type, etc.
  2. type_info, item_type_info, key_type_info, field_type_info, etc.

For me, I think I'd lean more towards either keeping what you have now or going with Alternative 1 (2 is okay, but has the implication that it contains a TypeInfo not a ValueInfo).

I’m curious if you or anyone else has any thoughts on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just replaced name and id with value for consistency. I'm thinking maybe key_type_value and item_type_value.

capacity: usize,
#[cfg(feature = "documentation")]
docs: Option<&'static str>,
Expand All @@ -63,10 +61,8 @@ impl ArrayInfo {
///
pub fn new<TArray: Array, TItem: Reflect>(capacity: usize) -> Self {
Self {
type_name: std::any::type_name::<TArray>(),
type_id: TypeId::of::<TArray>(),
item_type_name: std::any::type_name::<TItem>(),
item_type_id: TypeId::of::<TItem>(),
type_value: ValueInfo::new::<TArray>(),
item_type_value: ValueInfo::new::<TItem>(),
capacity,
#[cfg(feature = "documentation")]
docs: None,
Expand All @@ -88,34 +84,34 @@ impl ArrayInfo {
///
/// [type name]: std::any::type_name
pub fn type_name(&self) -> &'static str {
self.type_name
self.type_value.type_name()
}

/// The [`TypeId`] of the array.
pub fn type_id(&self) -> TypeId {
self.type_id
self.type_value.type_id()
}

/// Check if the given type matches the array type.
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id
self.type_value.is::<T>()
}

/// The [type name] of the array item.
///
/// [type name]: std::any::type_name
pub fn item_type_name(&self) -> &'static str {
self.item_type_name
self.item_type_value.type_name()
}

/// The [`TypeId`] of the array item.
pub fn item_type_id(&self) -> TypeId {
self.item_type_id
self.item_type_value.type_id()
}

/// Check if the given type matches the array item type.
pub fn item_is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.item_type_id
self.item_type_value.is::<T>()
}

/// The docstring of this array, if any.
Expand Down
14 changes: 6 additions & 8 deletions crates/bevy_reflect/src/enums/enum_trait.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DynamicEnum, Reflect, VariantInfo, VariantType};
use crate::{DynamicEnum, Reflect, ValueInfo, VariantInfo, VariantType};
use bevy_utils::HashMap;
use std::any::{Any, TypeId};
use std::slice::Iter;
Expand Down Expand Up @@ -133,8 +133,7 @@ pub trait Enum: Reflect {
#[derive(Clone, Debug)]
pub struct EnumInfo {
name: &'static str,
type_name: &'static str,
type_id: TypeId,
type_value: ValueInfo,
variants: Box<[VariantInfo]>,
variant_names: Box<[&'static str]>,
variant_indices: HashMap<&'static str, usize>,
Expand All @@ -161,8 +160,7 @@ impl EnumInfo {

Self {
name,
type_name: std::any::type_name::<TEnum>(),
type_id: TypeId::of::<TEnum>(),
type_value: ValueInfo::new::<TEnum>(),
variants: variants.to_vec().into_boxed_slice(),
variant_names,
variant_indices,
Expand Down Expand Up @@ -234,17 +232,17 @@ impl EnumInfo {
///
/// [type name]: std::any::type_name
pub fn type_name(&self) -> &'static str {
self.type_name
self.type_value.type_name()
}

/// The [`TypeId`] of the enum.
pub fn type_id(&self) -> TypeId {
self.type_id
self.type_value.type_id()
}

/// Check if the given type matches the enum type.
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id
self.type_value.is::<T>()
}

/// The docstring of this enum, if any.
Expand Down
26 changes: 11 additions & 15 deletions crates/bevy_reflect/src/fields.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::Reflect;
use crate::{Reflect, ValueInfo};
use std::any::{Any, TypeId};

/// The named field of a reflected struct.
#[derive(Clone, Debug)]
pub struct NamedField {
name: &'static str,
type_name: &'static str,
type_id: TypeId,
value: ValueInfo,
#[cfg(feature = "documentation")]
docs: Option<&'static str>,
}
Expand All @@ -16,8 +15,7 @@ impl NamedField {
pub fn new<T: Reflect>(name: &'static str) -> Self {
Self {
name,
type_name: std::any::type_name::<T>(),
type_id: TypeId::of::<T>(),
value: ValueInfo::new::<T>(),
#[cfg(feature = "documentation")]
docs: None,
}
Expand All @@ -38,17 +36,17 @@ impl NamedField {
///
/// [type name]: std::any::type_name
pub fn type_name(&self) -> &'static str {
self.type_name
self.value.type_name()
}

/// The [`TypeId`] of the field.
pub fn type_id(&self) -> TypeId {
self.type_id
self.value.type_id()
}

/// Check if the given type matches the field type.
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id
self.value.is::<T>()
}

/// The docstring of this field, if any.
Expand All @@ -62,8 +60,7 @@ impl NamedField {
#[derive(Clone, Debug)]
pub struct UnnamedField {
index: usize,
type_name: &'static str,
type_id: TypeId,
value: ValueInfo,
#[cfg(feature = "documentation")]
docs: Option<&'static str>,
}
Expand All @@ -72,8 +69,7 @@ impl UnnamedField {
pub fn new<T: Reflect>(index: usize) -> Self {
Self {
index,
type_name: std::any::type_name::<T>(),
type_id: TypeId::of::<T>(),
value: ValueInfo::new::<T>(),
#[cfg(feature = "documentation")]
docs: None,
}
Expand All @@ -94,17 +90,17 @@ impl UnnamedField {
///
/// [type name]: std::any::type_name
pub fn type_name(&self) -> &'static str {
self.type_name
self.value.type_name()
}

/// The [`TypeId`] of the field.
pub fn type_id(&self) -> TypeId {
self.type_id
self.value.type_id()
}

/// Check if the given type matches the field type.
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id
self.value.is::<T>()
}

/// The docstring of this field, if any.
Expand Down
26 changes: 11 additions & 15 deletions crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt::{Debug, Formatter};
use crate::utility::NonGenericTypeInfoCell;
use crate::{
Array, ArrayIter, DynamicArray, DynamicInfo, FromReflect, Reflect, ReflectMut, ReflectOwned,
ReflectRef, TypeInfo, Typed,
ReflectRef, TypeInfo, Typed, ValueInfo,
};

/// An ordered, mutable list of [Reflect] items. This corresponds to types like [`std::vec::Vec`].
Expand Down Expand Up @@ -59,10 +59,8 @@ pub trait List: Reflect + Array {
/// A container for compile-time list info.
#[derive(Clone, Debug)]
pub struct ListInfo {
type_name: &'static str,
type_id: TypeId,
item_type_name: &'static str,
item_type_id: TypeId,
type_value: ValueInfo,
item_type_value: ValueInfo,
#[cfg(feature = "documentation")]
docs: Option<&'static str>,
}
Expand All @@ -71,10 +69,8 @@ impl ListInfo {
/// Create a new [`ListInfo`].
pub fn new<TList: List, TItem: FromReflect>() -> Self {
Self {
type_name: std::any::type_name::<TList>(),
type_id: TypeId::of::<TList>(),
item_type_name: std::any::type_name::<TItem>(),
item_type_id: TypeId::of::<TItem>(),
type_value: ValueInfo::new::<TList>(),
item_type_value: ValueInfo::new::<TItem>(),
#[cfg(feature = "documentation")]
docs: None,
}
Expand All @@ -90,34 +86,34 @@ impl ListInfo {
///
/// [type name]: std::any::type_name
pub fn type_name(&self) -> &'static str {
self.type_name
self.type_value.type_name()
}

/// The [`TypeId`] of the list.
pub fn type_id(&self) -> TypeId {
self.type_id
self.type_value.type_id()
}

/// Check if the given type matches the list type.
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id
self.type_value.is::<T>()
}

/// The [type name] of the list item.
///
/// [type name]: std::any::type_name
pub fn item_type_name(&self) -> &'static str {
self.item_type_name
self.item_type_value.type_name()
}

/// The [`TypeId`] of the list item.
pub fn item_type_id(&self) -> TypeId {
self.item_type_id
self.item_type_value.type_id()
}

/// Check if the given type matches the list item type.
pub fn item_is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.item_type_id
self.item_type_value.is::<T>()
}

/// The docstring of this list, if any.
Expand Down
40 changes: 18 additions & 22 deletions crates/bevy_reflect/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::hash::Hash;
use bevy_utils::{Entry, HashMap};

use crate::utility::NonGenericTypeInfoCell;
use crate::{DynamicInfo, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, Typed};
use crate::{
DynamicInfo, Reflect, ReflectMut, ReflectOwned, ReflectRef, TypeInfo, Typed, ValueInfo,
};

/// An ordered mapping between [`Reflect`] values.
///
Expand Down Expand Up @@ -68,12 +70,9 @@ pub trait Map: Reflect {
/// A container for compile-time map info.
#[derive(Clone, Debug)]
pub struct MapInfo {
type_name: &'static str,
type_id: TypeId,
key_type_name: &'static str,
key_type_id: TypeId,
value_type_name: &'static str,
value_type_id: TypeId,
type_value: ValueInfo,
key_type_value: ValueInfo,
value_type_value: ValueInfo,
#[cfg(feature = "documentation")]
docs: Option<&'static str>,
}
Expand All @@ -82,12 +81,9 @@ impl MapInfo {
/// Create a new [`MapInfo`].
pub fn new<TMap: Map, TKey: Hash + Reflect, TValue: Reflect>() -> Self {
Self {
type_name: std::any::type_name::<TMap>(),
type_id: TypeId::of::<TMap>(),
key_type_name: std::any::type_name::<TKey>(),
key_type_id: TypeId::of::<TKey>(),
value_type_name: std::any::type_name::<TValue>(),
value_type_id: TypeId::of::<TValue>(),
type_value: ValueInfo::new::<TMap>(),
key_type_value: ValueInfo::new::<TKey>(),
value_type_value: ValueInfo::new::<TValue>(),
#[cfg(feature = "documentation")]
docs: None,
}
Expand All @@ -103,51 +99,51 @@ impl MapInfo {
///
/// [type name]: std::any::type_name
pub fn type_name(&self) -> &'static str {
self.type_name
self.type_value.type_name()
}

/// The [`TypeId`] of the map.
pub fn type_id(&self) -> TypeId {
self.type_id
self.type_value.type_id()
}

/// Check if the given type matches the map type.
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id
self.type_value.is::<T>()
}

/// The [type name] of the key.
///
/// [type name]: std::any::type_name
pub fn key_type_name(&self) -> &'static str {
self.key_type_name
self.key_type_value.type_name()
}

/// The [`TypeId`] of the key.
pub fn key_type_id(&self) -> TypeId {
self.key_type_id
self.key_type_value.type_id()
}

/// Check if the given type matches the key type.
pub fn key_is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.key_type_id
self.key_type_value.is::<T>()
}

/// The [type name] of the value.
///
/// [type name]: std::any::type_name
pub fn value_type_name(&self) -> &'static str {
self.value_type_name
self.value_type_value.type_name()
}

/// The [`TypeId`] of the value.
pub fn value_type_id(&self) -> TypeId {
self.value_type_id
self.value_type_value.type_id()
}

/// Check if the given type matches the value type.
pub fn value_is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.value_type_id
self.value_type_value.is::<T>()
}

/// The docstring of this map, if any.
Expand Down
Loading