Skip to content

Commit a2f966e

Browse files
committed
create mutable versions of TypeRegistry methods (#4484)
# Objective It is possible to get a mutable reference to a `TypeRegistration` using `TypeRegistry::get_mut`. However, none of its other methods (`get_mut_with_name`, `get_type_data`, `iter`, etc.) have mutable versions. Besides improving consistency, this change would facilitate use cases which involve storing mutable state data in the `TypeRegistry`. ## Solution Provides a trivial wrapper around the mutable accessors that the `TypeRegistration` already provides. Exactly mirrors the existing immutable versions.
1 parent 96078c7 commit a2f966e

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

crates/bevy_reflect/src/type_registry.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl TypeRegistry {
104104
.and_then(move |id| self.get_mut(id))
105105
}
106106

107-
/// Returns a mutable reference to the [`TypeRegistration`] of the type with
107+
/// Returns a reference to the [`TypeRegistration`] of the type with
108108
/// the given short name.
109109
///
110110
/// If the short name is ambiguous, or if no type with the given short name
@@ -115,7 +115,21 @@ impl TypeRegistry {
115115
.and_then(|id| self.registrations.get(id))
116116
}
117117

118-
/// Returns the [`TypeData`] of type `T` associated with the given `TypeId`.
118+
/// Returns a mutable reference to the [`TypeRegistration`] of the type with
119+
/// the given short name.
120+
///
121+
/// If the short name is ambiguous, or if no type with the given short name
122+
/// has been registered, returns `None`.
123+
pub fn get_with_short_name_mut(
124+
&mut self,
125+
short_type_name: &str,
126+
) -> Option<&mut TypeRegistration> {
127+
self.short_name_to_id
128+
.get(short_type_name)
129+
.and_then(|id| self.registrations.get_mut(id))
130+
}
131+
132+
/// Returns a reference to the [`TypeData`] of type `T` associated with the given `TypeId`.
119133
///
120134
/// The returned value may be used to downcast [`Reflect`] trait objects to
121135
/// trait objects of the trait used to generate `T`, provided that the
@@ -129,11 +143,26 @@ impl TypeRegistry {
129143
.and_then(|registration| registration.data::<T>())
130144
}
131145

132-
/// Returns an iterator overed the [`TypeRegistration`]s of the registered
146+
/// Returns a mutable reference to the [`TypeData`] of type `T` associated with the given `TypeId`.
147+
///
148+
/// If the specified type has not been registered, or if `T` is not present
149+
/// in its type registration, returns `None`.
150+
pub fn get_type_data_mut<T: TypeData>(&mut self, type_id: TypeId) -> Option<&mut T> {
151+
self.get_mut(type_id)
152+
.and_then(|registration| registration.data_mut::<T>())
153+
}
154+
155+
/// Returns an iterator over the [`TypeRegistration`]s of the registered
133156
/// types.
134157
pub fn iter(&self) -> impl Iterator<Item = &TypeRegistration> {
135158
self.registrations.values()
136159
}
160+
161+
/// Returns a mutable iterator over the [`TypeRegistration`]s of the registered
162+
/// types.
163+
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut TypeRegistration> {
164+
self.registrations.values_mut()
165+
}
137166
}
138167

139168
impl TypeRegistryArc {

0 commit comments

Comments
 (0)