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

Improve DynamicStruct::insert #11068

Merged
merged 2 commits into from Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 12 additions & 18 deletions crates/bevy_reflect/src/struct_trait.rs
Expand Up @@ -3,7 +3,7 @@ use crate::{
TypePath, TypePathTable,
};
use bevy_reflect_derive::impl_type_path;
use bevy_utils::{Entry, HashMap};
use bevy_utils::HashMap;
use std::fmt::{Debug, Formatter};
use std::{
any::{Any, TypeId},
Expand Down Expand Up @@ -300,29 +300,23 @@ impl DynamicStruct {
/// Inserts a field named `name` with value `value` into the struct.
///
/// If the field already exists, it is overwritten.
pub fn insert_boxed(&mut self, name: &str, value: Box<dyn Reflect>) {
let name = Cow::Owned(name.to_string());
match self.field_indices.entry(name) {
Entry::Occupied(entry) => {
self.fields[*entry.get()] = value;
}
Entry::Vacant(entry) => {
self.fields.push(value);
self.field_names.push(entry.key().clone());
entry.insert(self.fields.len() - 1);
}
pub fn insert_boxed<'a>(&mut self, name: impl Into<Cow<'a, str>>, value: Box<dyn Reflect>) {
let name: Cow<str> = name.into();
if let Some(index) = self.field_indices.get(&name) {
self.fields[*index] = value;
} else {
self.fields.push(value);
self.field_indices
.insert(Cow::Owned(name.clone().into_owned()), self.fields.len() - 1);
self.field_names.push(Cow::Owned(name.into_owned()));
Comment on lines +303 to +311
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a reason you dropped the usage of the entry api? to me it's more readable in this case.

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 can't use the entry API because it always requires an owned String regardless of whether it needs it or not.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah good point.

Comment on lines +309 to +311
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self.field_indices
.insert(Cow::Owned(name.clone().into_owned()), self.fields.len() - 1);
self.field_names.push(Cow::Owned(name.into_owned()));
let name = name.into_owned();
self.field_indices
.insert(Cow::Owned(name.clone()), self.fields.len() - 1);
self.field_names.push(Cow::Owned(name));

definitely not a cow expert, but this would feel better to me

}
}

/// Inserts a field named `name` with the typed value `value` into the struct.
///
/// If the field already exists, it is overwritten.
pub fn insert<T: Reflect>(&mut self, name: &str, value: T) {
if let Some(index) = self.field_indices.get(name) {
self.fields[*index] = Box::new(value);
} else {
self.insert_boxed(name, Box::new(value));
}
pub fn insert<'a, T: Reflect>(&mut self, name: impl Into<Cow<'a, str>>, value: T) {
self.insert_boxed(name, Box::new(value));
}

/// Gets the index of the field with the given name.
Expand Down