Skip to content

Commit

Permalink
Add assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Apr 17, 2023
1 parent 29dcae3 commit 63387b2
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
12 changes: 12 additions & 0 deletions crates/bevy_reflect/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,20 @@ impl DynamicArray {

/// Sets the [type] to be represented by this `DynamicArray`.
///
/// # Panics
///
/// Panics if the given [type] is not a [`TypeInfo::Array`].
///
/// [type]: TypeInfo
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
if let Some(represented_type) = represented_type {
assert!(
matches!(represented_type, TypeInfo::Array(_)),
"expected TypeInfo::Array but received: {:?}",
represented_type
);
}

self.represented_type = represented_type;
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/bevy_reflect/src/enums/dynamic_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,20 @@ impl DynamicEnum {

/// Sets the [type] to be represented by this `DynamicEnum`.
///
/// # Panics
///
/// Panics if the given [type] is not a [`TypeInfo::Enum`].
///
/// [type]: TypeInfo
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
if let Some(represented_type) = represented_type {
assert!(
matches!(represented_type, TypeInfo::Enum(_)),
"expected TypeInfo::Enum but received: {:?}",
represented_type
);
}

self.represented_type = represented_type;
}

Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,21 @@ mod tests {
assert!(info.is::<MyValue>());
}

#[test]
fn should_permit_valid_represented_type_for_dynamic() {
let type_info = <[i32; 2] as Typed>::type_info();
let mut dynamic_array = [123; 2].clone_dynamic();
dynamic_array.set_represented_type(Some(type_info));
}

#[test]
#[should_panic(expected = "expected TypeInfo::Array but received")]
fn should_prohibit_invalid_represented_type_for_dynamic() {
let type_info = <(i32, i32) as Typed>::type_info();
let mut dynamic_array = [123; 2].clone_dynamic();
dynamic_array.set_represented_type(Some(type_info));
}

#[cfg(feature = "documentation")]
mod docstrings {
use super::*;
Expand Down
11 changes: 11 additions & 0 deletions crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,20 @@ pub struct DynamicList {

impl DynamicList {
/// Sets the [type] to be represented by this `DynamicList`.
/// # Panics
///
/// Panics if the given [type] is not a [`TypeInfo::List`].
///
/// [type]: TypeInfo
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
if let Some(represented_type) = represented_type {
assert!(
matches!(represented_type, TypeInfo::List(_)),
"expected TypeInfo::List but received: {:?}",
represented_type
);
}

self.represented_type = represented_type;
}

Expand Down
12 changes: 12 additions & 0 deletions crates/bevy_reflect/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,20 @@ pub struct DynamicMap {
impl DynamicMap {
/// Sets the [type] to be represented by this `DynamicMap`.
///
/// # Panics
///
/// Panics if the given [type] is not a [`TypeInfo::Map`].
///
/// [type]: TypeInfo
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
if let Some(represented_type) = represented_type {
assert!(
matches!(represented_type, TypeInfo::Map(_)),
"expected TypeInfo::Map but received: {:?}",
represented_type
);
}

self.represented_type = represented_type;
}

Expand Down
12 changes: 12 additions & 0 deletions crates/bevy_reflect/src/struct_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,20 @@ pub struct DynamicStruct {
impl DynamicStruct {
/// Sets the [type] to be represented by this `DynamicStruct`.
///
/// # Panics
///
/// Panics if the given [type] is not a [`TypeInfo::Struct`].
///
/// [type]: TypeInfo
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
if let Some(represented_type) = represented_type {
assert!(
matches!(represented_type, TypeInfo::Struct(_)),
"expected TypeInfo::Struct but received: {:?}",
represented_type
);
}

self.represented_type = represented_type;
}

Expand Down
14 changes: 12 additions & 2 deletions crates/bevy_reflect/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,20 @@ pub struct DynamicTuple {
impl DynamicTuple {
/// Sets the [type] to be represented by this `DynamicTuple`.
///
/// # Panics
///
/// Panics if the given [type] is not a [`TypeInfo::Tuple`].
///
/// [type]: TypeInfo
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
if let Some(info) = represented_type {
self.name = Cow::Borrowed(info.type_name());
if let Some(represented_type) = represented_type {
assert!(
matches!(represented_type, TypeInfo::Tuple(_)),
"expected TypeInfo::Tuple but received: {:?}",
represented_type
);

self.name = Cow::Borrowed(represented_type.type_name());
}
self.represented_type = represented_type;
}
Expand Down
12 changes: 12 additions & 0 deletions crates/bevy_reflect/src/tuple_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,20 @@ pub struct DynamicTupleStruct {
impl DynamicTupleStruct {
/// Sets the [type] to be represented by this `DynamicTupleStruct`.
///
/// # Panics
///
/// Panics if the given [type] is not a [`TypeInfo::TupleStruct`].
///
/// [type]: TypeInfo
pub fn set_represented_type(&mut self, represented_type: Option<&'static TypeInfo>) {
if let Some(represented_type) = represented_type {
assert!(
matches!(represented_type, TypeInfo::TupleStruct(_)),
"expected TypeInfo::TupleStruct but received: {:?}",
represented_type
);
}

self.represented_type = represented_type;
}

Expand Down

0 comments on commit 63387b2

Please sign in to comment.