Skip to content

Commit f7b9665

Browse files
committed
Add assertion
1 parent bc946f8 commit f7b9665

File tree

8 files changed

+98
-2
lines changed

8 files changed

+98
-2
lines changed

crates/bevy_reflect/src/array.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,20 @@ impl DynamicArray {
190190

191191
/// Sets the [type] to be represented by this `DynamicArray`.
192192
///
193+
/// # Panics
194+
///
195+
/// Panics if the given [type] is not a [`TypeInfo::Array`].
196+
///
193197
/// [type]: TypeInfo
194198
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
199+
if let Some(represented_type) = represented_type {
200+
assert!(
201+
matches!(represented_type, TypeInfo::Array(_)),
202+
"expected TypeInfo::Array but received: {:?}",
203+
represented_type
204+
);
205+
}
206+
195207
self.represented_type = represented_type;
196208
}
197209
}

crates/bevy_reflect/src/enums/dynamic_enum.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,20 @@ impl DynamicEnum {
114114

115115
/// Sets the [type] to be represented by this `DynamicEnum`.
116116
///
117+
/// # Panics
118+
///
119+
/// Panics if the given [type] is not a [`TypeInfo::Enum`].
120+
///
117121
/// [type]: TypeInfo
118122
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
123+
if let Some(represented_type) = represented_type {
124+
assert!(
125+
matches!(represented_type, TypeInfo::Enum(_)),
126+
"expected TypeInfo::Enum but received: {:?}",
127+
represented_type
128+
);
129+
}
130+
119131
self.represented_type = represented_type;
120132
}
121133

crates/bevy_reflect/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,21 @@ mod tests {
12971297
assert!(info.is::<MyValue>());
12981298
}
12991299

1300+
#[test]
1301+
fn should_permit_valid_represented_type_for_dynamic() {
1302+
let type_info = <[i32; 2] as Typed>::type_info();
1303+
let mut dynamic_array = [123; 2].clone_dynamic();
1304+
dynamic_array.set_represented_type(Some(type_info));
1305+
}
1306+
1307+
#[test]
1308+
#[should_panic(expected = "expected TypeInfo::Array but received")]
1309+
fn should_prohibit_invalid_represented_type_for_dynamic() {
1310+
let type_info = <(i32, i32) as Typed>::type_info();
1311+
let mut dynamic_array = [123; 2].clone_dynamic();
1312+
dynamic_array.set_represented_type(Some(type_info));
1313+
}
1314+
13001315
#[cfg(feature = "documentation")]
13011316
mod docstrings {
13021317
use super::*;

crates/bevy_reflect/src/list.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,20 @@ pub struct DynamicList {
181181

182182
impl DynamicList {
183183
/// Sets the [type] to be represented by this `DynamicList`.
184+
/// # Panics
185+
///
186+
/// Panics if the given [type] is not a [`TypeInfo::List`].
184187
///
185188
/// [type]: TypeInfo
186189
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
190+
if let Some(represented_type) = represented_type {
191+
assert!(
192+
matches!(represented_type, TypeInfo::List(_)),
193+
"expected TypeInfo::List but received: {:?}",
194+
represented_type
195+
);
196+
}
197+
187198
self.represented_type = represented_type;
188199
}
189200

crates/bevy_reflect/src/map.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,20 @@ pub struct DynamicMap {
191191
impl DynamicMap {
192192
/// Sets the [type] to be represented by this `DynamicMap`.
193193
///
194+
/// # Panics
195+
///
196+
/// Panics if the given [type] is not a [`TypeInfo::Map`].
197+
///
194198
/// [type]: TypeInfo
195199
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
200+
if let Some(represented_type) = represented_type {
201+
assert!(
202+
matches!(represented_type, TypeInfo::Map(_)),
203+
"expected TypeInfo::Map but received: {:?}",
204+
represented_type
205+
);
206+
}
207+
196208
self.represented_type = represented_type;
197209
}
198210

crates/bevy_reflect/src/struct_trait.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,20 @@ pub struct DynamicStruct {
278278
impl DynamicStruct {
279279
/// Sets the [type] to be represented by this `DynamicStruct`.
280280
///
281+
/// # Panics
282+
///
283+
/// Panics if the given [type] is not a [`TypeInfo::Struct`].
284+
///
281285
/// [type]: TypeInfo
282286
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
287+
if let Some(represented_type) = represented_type {
288+
assert!(
289+
matches!(represented_type, TypeInfo::Struct(_)),
290+
"expected TypeInfo::Struct but received: {:?}",
291+
represented_type
292+
);
293+
}
294+
283295
self.represented_type = represented_type;
284296
}
285297

crates/bevy_reflect/src/tuple.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,20 @@ pub struct DynamicTuple {
215215
impl DynamicTuple {
216216
/// Sets the [type] to be represented by this `DynamicTuple`.
217217
///
218+
/// # Panics
219+
///
220+
/// Panics if the given [type] is not a [`TypeInfo::Tuple`].
221+
///
218222
/// [type]: TypeInfo
219223
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
220-
if let Some(info) = represented_type {
221-
self.name = Cow::Borrowed(info.type_name());
224+
if let Some(represented_type) = represented_type {
225+
assert!(
226+
matches!(represented_type, TypeInfo::Tuple(_)),
227+
"expected TypeInfo::Tuple but received: {:?}",
228+
represented_type
229+
);
230+
231+
self.name = Cow::Borrowed(represented_type.type_name());
222232
}
223233
self.represented_type = represented_type;
224234
}

crates/bevy_reflect/src/tuple_struct.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,20 @@ pub struct DynamicTupleStruct {
226226
impl DynamicTupleStruct {
227227
/// Sets the [type] to be represented by this `DynamicTupleStruct`.
228228
///
229+
/// # Panics
230+
///
231+
/// Panics if the given [type] is not a [`TypeInfo::TupleStruct`].
232+
///
229233
/// [type]: TypeInfo
230234
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
235+
if let Some(represented_type) = represented_type {
236+
assert!(
237+
matches!(represented_type, TypeInfo::TupleStruct(_)),
238+
"expected TypeInfo::TupleStruct but received: {:?}",
239+
represented_type
240+
);
241+
}
242+
231243
self.represented_type = represented_type;
232244
}
233245

0 commit comments

Comments
 (0)