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: Get owned fields #5728

Closed
wants to merge 2 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
7 changes: 7 additions & 0 deletions crates/bevy_reflect/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub trait Array: Reflect {
}
/// Returns an iterator over the collection.
fn iter(&self) -> ArrayIter;
/// Drain the elements of this array to get a vector of owned values.
fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>>;

fn clone_dynamic(&self) -> DynamicArray {
DynamicArray {
Expand Down Expand Up @@ -246,6 +248,11 @@ impl Array for DynamicArray {
}
}

#[inline]
fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>> {
self.values.into_vec()
}

#[inline]
fn clone_dynamic(&self) -> DynamicArray {
DynamicArray {
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_reflect/src/impls/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ where
index: 0,
}
}

fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>> {
self.into_iter()
.map(|value| Box::new(value) as Box<dyn Reflect>)
.collect()
}
}

impl<T: smallvec::Array + Send + Sync + 'static> List for SmallVec<T>
Expand Down
25 changes: 25 additions & 0 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ impl<T: FromReflect> Array for Vec<T> {
index: 0,
}
}

#[inline]
fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>> {
self.into_iter()
.map(|value| Box::new(value) as Box<dyn Reflect>)
.collect()
}
}

impl<T: FromReflect> List for Vec<T> {
Expand Down Expand Up @@ -247,6 +254,17 @@ impl<K: FromReflect + Eq + Hash, V: FromReflect> Map for HashMap<K, V> {
}
}

fn drain(self: Box<Self>) -> Vec<(Box<dyn Reflect>, Box<dyn Reflect>)> {
self.into_iter()
.map(|(key, value)| {
(
Box::new(key) as Box<dyn Reflect>,
Box::new(value) as Box<dyn Reflect>,
)
})
.collect()
}

fn clone_dynamic(&self) -> DynamicMap {
let mut dynamic_map = DynamicMap::default();
dynamic_map.set_name(self.type_name().to_string());
Expand Down Expand Up @@ -395,6 +413,13 @@ impl<T: Reflect, const N: usize> Array for [T; N] {
index: 0,
}
}

#[inline]
fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>> {
self.into_iter()
.map(|value| Box::new(value) as Box<dyn Reflect>)
.collect()
}
}

impl<T: Reflect, const N: usize> Reflect for [T; N] {
Expand Down
23 changes: 23 additions & 0 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,29 @@ mod tests {
assert_eq!(foo, *foo2.downcast::<Foo>().unwrap());
}

#[test]
fn should_drain_fields() {
let array_value: Box<dyn Array> = Box::new([123_i32, 321_i32]);
let fields = array_value.drain();
assert!(fields[0].reflect_partial_eq(&123_i32).unwrap_or_default());
assert!(fields[1].reflect_partial_eq(&321_i32).unwrap_or_default());

let list_value: Box<dyn List> = Box::new(vec![123_i32, 321_i32]);
let fields = list_value.drain();
assert!(fields[0].reflect_partial_eq(&123_i32).unwrap_or_default());
assert!(fields[1].reflect_partial_eq(&321_i32).unwrap_or_default());

let tuple_value: Box<dyn Tuple> = Box::new((123_i32, 321_i32));
let fields = tuple_value.drain();
assert!(fields[0].reflect_partial_eq(&123_i32).unwrap_or_default());
assert!(fields[1].reflect_partial_eq(&321_i32).unwrap_or_default());

let map_value: Box<dyn Map> = Box::new(HashMap::from([(123_i32, 321_i32)]));
let fields = map_value.drain();
assert!(fields[0].0.reflect_partial_eq(&123_i32).unwrap_or_default());
assert!(fields[0].1.reflect_partial_eq(&321_i32).unwrap_or_default());
}

#[test]
fn reflect_take() {
#[derive(Reflect, Debug, PartialEq)]
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ impl Array for DynamicList {
}
}

fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>> {
self.values
}

fn clone_dynamic(&self) -> DynamicArray {
DynamicArray {
name: self.name.clone(),
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_reflect/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub trait Map: Reflect {
/// Returns an iterator over the key-value pairs of the map.
fn iter(&self) -> MapIter;

/// Drain the key-value pairs of this map to get a vector of owned values.
fn drain(self: Box<Self>) -> Vec<(Box<dyn Reflect>, Box<dyn Reflect>)>;

/// Clones the map, producing a [`DynamicMap`].
fn clone_dynamic(&self) -> DynamicMap;

Expand Down Expand Up @@ -226,6 +229,10 @@ impl Map for DynamicMap {
}
}
}

fn drain(self: Box<Self>) -> Vec<(Box<dyn Reflect>, Box<dyn Reflect>)> {
self.values
}
}

impl Reflect for DynamicMap {
Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_reflect/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub trait Tuple: Reflect {
/// Returns an iterator over the values of the tuple's fields.
fn iter_fields(&self) -> TupleFieldIter;

/// Drain the fields of this tuple to get a vector of owned values.
fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>>;

/// Clones the struct into a [`DynamicTuple`].
fn clone_dynamic(&self) -> DynamicTuple;
}
Expand Down Expand Up @@ -253,6 +256,11 @@ impl Tuple for DynamicTuple {
}
}

#[inline]
fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>> {
self.fields
}

#[inline]
fn clone_dynamic(&self) -> DynamicTuple {
DynamicTuple {
Expand Down Expand Up @@ -451,6 +459,13 @@ macro_rules! impl_reflect_tuple {
}
}

#[inline]
fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>> {
vec![
$(Box::new(self.$index),)*
]
}

#[inline]
fn clone_dynamic(&self) -> DynamicTuple {
let mut dyn_tuple = DynamicTuple {
Expand Down