Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - bevy_reflect: Binary formats #6140

Closed
wants to merge 5 commits into from
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
2 changes: 2 additions & 0 deletions crates/bevy_reflect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ glam = { version = "0.21", features = ["serde"], optional = true }

[dev-dependencies]
ron = "0.8.0"
rmp-serde = "1.1"
bincode = "1.3"

[[example]]
name = "reflect_docs"
Expand Down
9 changes: 9 additions & 0 deletions crates/bevy_reflect/src/enums/enum_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub struct EnumInfo {
type_name: &'static str,
type_id: TypeId,
variants: Box<[VariantInfo]>,
variant_names: Box<[&'static str]>,
variant_indices: HashMap<&'static str, usize>,
#[cfg(feature = "documentation")]
docs: Option<&'static str>,
Expand All @@ -156,11 +157,14 @@ impl EnumInfo {
.map(|(index, variant)| (variant.name(), index))
.collect::<HashMap<_, _>>();

let variant_names = variants.iter().map(|variant| variant.name()).collect();
MrGVSV marked this conversation as resolved.
Show resolved Hide resolved

Self {
name,
type_name: std::any::type_name::<TEnum>(),
type_id: TypeId::of::<TEnum>(),
variants: variants.to_vec().into_boxed_slice(),
variant_names,
variant_indices,
#[cfg(feature = "documentation")]
docs: None,
Expand All @@ -173,6 +177,11 @@ impl EnumInfo {
Self { docs, ..self }
}

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

/// Get a variant with the given name.
pub fn variant(&self, name: &str) -> Option<&VariantInfo> {
self.variant_indices
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_reflect/src/enums/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl VariantInfo {
pub struct StructVariantInfo {
name: &'static str,
fields: Box<[NamedField]>,
field_names: Box<[&'static str]>,
field_indices: HashMap<&'static str, usize>,
#[cfg(feature = "documentation")]
docs: Option<&'static str>,
Expand All @@ -98,9 +99,11 @@ impl StructVariantInfo {
/// Create a new [`StructVariantInfo`].
pub fn new(name: &'static str, fields: &[NamedField]) -> Self {
let field_indices = Self::collect_field_indices(fields);
let field_names = fields.iter().map(|field| field.name()).collect();
MrGVSV marked this conversation as resolved.
Show resolved Hide resolved
Self {
name,
fields: fields.to_vec().into_boxed_slice(),
field_names,
field_indices,
#[cfg(feature = "documentation")]
docs: None,
Expand All @@ -118,6 +121,11 @@ impl StructVariantInfo {
self.name
}

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

/// Get the field with the given name.
pub fn field(&self, name: &str) -> Option<&NamedField> {
self.field_indices
Expand Down
Loading