Skip to content

Commit 02fbf16

Browse files
committed
bevy_reflect: Add Reflect::into_reflect (#6502)
# Objective Using `Reflect` we can easily switch between a specific reflection trait object, such as a `dyn Struct`, to a `dyn Reflect` object via `Reflect::as_reflect` or `Reflect::as_reflect_mut`. ```rust fn do_something(value: &dyn Reflect) {/* ... */} let foo: Box<dyn Struct> = Box::new(Foo::default()); do_something(foo.as_reflect()); ``` However, there is no way to convert a _boxed_ reflection trait object to a `Box<dyn Reflect>`. ## Solution Add a `Reflect::into_reflect` method which allows converting a boxed reflection trait object back into a boxed `Reflect` trait object. ```rust fn do_something(value: Box<dyn Reflect>) {/* ... */} let foo: Box<dyn Struct> = Box::new(Foo::default()); do_something(foo.into_reflect()); ``` --- ## Changelog - Added `Reflect::into_reflect`
1 parent 33299f0 commit 02fbf16

File tree

17 files changed

+109
-2
lines changed

17 files changed

+109
-2
lines changed

crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
202202
self
203203
}
204204

205+
#[inline]
206+
fn into_reflect(self: Box<Self>) -> Box<dyn #bevy_reflect_path::Reflect> {
207+
self
208+
}
209+
205210
#[inline]
206211
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
207212
self

crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream {
183183
self
184184
}
185185

186+
#[inline]
187+
fn into_reflect(self: Box<Self>) -> Box<dyn #bevy_reflect_path::Reflect> {
188+
self
189+
}
190+
186191
#[inline]
187192
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
188193
self

crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
145145
self
146146
}
147147

148+
#[inline]
149+
fn into_reflect(self: Box<Self>) -> Box<dyn #bevy_reflect_path::Reflect> {
150+
self
151+
}
152+
148153
#[inline]
149154
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
150155
self

crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> TokenStream {
6464
self
6565
}
6666

67+
#[inline]
68+
fn into_reflect(self: Box<Self>) -> Box<dyn #bevy_reflect_path::Reflect> {
69+
self
70+
}
71+
6772
#[inline]
6873
fn as_reflect(&self) -> &dyn #bevy_reflect_path::Reflect {
6974
self

crates/bevy_reflect/src/array.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ impl Reflect for DynamicArray {
197197
self
198198
}
199199

200+
#[inline]
201+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
202+
self
203+
}
204+
200205
#[inline]
201206
fn as_reflect(&self) -> &dyn Reflect {
202207
self

crates/bevy_reflect/src/enums/dynamic_enum.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ impl Reflect for DynamicEnum {
315315
self
316316
}
317317

318+
#[inline]
319+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
320+
self
321+
}
322+
318323
#[inline]
319324
fn as_reflect(&self) -> &dyn Reflect {
320325
self

crates/bevy_reflect/src/impls/smallvec.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ where
9090
self
9191
}
9292

93+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
94+
self
95+
}
96+
9397
fn as_reflect(&self) -> &dyn Reflect {
9498
self
9599
}

crates/bevy_reflect/src/impls/std.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ impl<T: FromReflect> Reflect for Vec<T> {
245245
self
246246
}
247247

248+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
249+
self
250+
}
251+
248252
fn as_reflect(&self) -> &dyn Reflect {
249253
self
250254
}
@@ -413,6 +417,11 @@ impl<K: FromReflect + Eq + Hash, V: FromReflect> Reflect for HashMap<K, V> {
413417
self
414418
}
415419

420+
#[inline]
421+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
422+
self
423+
}
424+
416425
fn as_reflect(&self) -> &dyn Reflect {
417426
self
418427
}
@@ -543,6 +552,11 @@ impl<T: Reflect, const N: usize> Reflect for [T; N] {
543552
self
544553
}
545554

555+
#[inline]
556+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
557+
self
558+
}
559+
546560
#[inline]
547561
fn as_reflect(&self) -> &dyn Reflect {
548562
self
@@ -661,6 +675,10 @@ impl Reflect for Cow<'static, str> {
661675
self
662676
}
663677

678+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
679+
self
680+
}
681+
664682
fn as_reflect(&self) -> &dyn Reflect {
665683
self
666684
}
@@ -819,6 +837,11 @@ impl<T: FromReflect> Reflect for Option<T> {
819837
self
820838
}
821839

840+
#[inline]
841+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
842+
self
843+
}
844+
822845
fn as_reflect(&self) -> &dyn Reflect {
823846
self
824847
}

crates/bevy_reflect/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,21 @@ mod tests {
989989
}
990990
}
991991

992+
#[test]
993+
fn into_reflect() {
994+
trait TestTrait: Reflect {}
995+
996+
#[derive(Reflect)]
997+
struct TestStruct;
998+
999+
impl TestTrait for TestStruct {}
1000+
1001+
let trait_object: Box<dyn TestTrait> = Box::new(TestStruct);
1002+
1003+
// Should compile:
1004+
let _ = trait_object.into_reflect();
1005+
}
1006+
9921007
#[test]
9931008
fn as_reflect() {
9941009
trait TestTrait: Reflect {}

crates/bevy_reflect/src/list.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ impl Reflect for DynamicList {
216216
self
217217
}
218218

219+
#[inline]
220+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
221+
self
222+
}
223+
219224
#[inline]
220225
fn as_reflect(&self) -> &dyn Reflect {
221226
self

crates/bevy_reflect/src/map.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ impl Reflect for DynamicMap {
273273
self
274274
}
275275

276+
#[inline]
277+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
278+
self
279+
}
280+
276281
#[inline]
277282
fn as_reflect(&self) -> &dyn Reflect {
278283
self

crates/bevy_reflect/src/reflect.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,13 @@ pub trait Reflect: Any + Send + Sync {
9292
/// Returns the value as a [`&mut dyn Any`][std::any::Any].
9393
fn as_any_mut(&mut self) -> &mut dyn Any;
9494

95-
/// Casts this type to a reflected value
95+
/// Casts this type to a boxed reflected value.
96+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>;
97+
98+
/// Casts this type to a reflected value.
9699
fn as_reflect(&self) -> &dyn Reflect;
97100

98-
/// Casts this type to a mutable reflected value
101+
/// Casts this type to a mutable reflected value.
99102
fn as_reflect_mut(&mut self) -> &mut dyn Reflect;
100103

101104
/// Applies a reflected value to this value.

crates/bevy_reflect/src/struct_trait.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ impl Reflect for DynamicStruct {
404404
self
405405
}
406406

407+
#[inline]
408+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
409+
self
410+
}
411+
407412
#[inline]
408413
fn as_reflect(&self) -> &dyn Reflect {
409414
self

crates/bevy_reflect/src/tuple.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ impl Reflect for DynamicTuple {
316316
self
317317
}
318318

319+
#[inline]
320+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
321+
self
322+
}
323+
319324
#[inline]
320325
fn as_reflect(&self) -> &dyn Reflect {
321326
self
@@ -520,6 +525,10 @@ macro_rules! impl_reflect_tuple {
520525
self
521526
}
522527

528+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
529+
self
530+
}
531+
523532
fn as_reflect(&self) -> &dyn Reflect {
524533
self
525534
}

crates/bevy_reflect/src/tuple_struct.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ impl Reflect for DynamicTupleStruct {
307307
self
308308
}
309309

310+
#[inline]
311+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
312+
self
313+
}
314+
310315
#[inline]
311316
fn as_reflect(&self) -> &dyn Reflect {
312317
self

crates/bevy_reflect/src/type_info.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use std::any::{Any, TypeId};
5353
/// # fn into_any(self: Box<Self>) -> Box<dyn Any> { todo!() }
5454
/// # fn as_any(&self) -> &dyn Any { todo!() }
5555
/// # fn as_any_mut(&mut self) -> &mut dyn Any { todo!() }
56+
/// # fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> { todo!() }
5657
/// # fn as_reflect(&self) -> &dyn Reflect { todo!() }
5758
/// # fn as_reflect_mut(&mut self) -> &mut dyn Reflect { todo!() }
5859
/// # fn apply(&mut self, value: &dyn Reflect) { todo!() }

crates/bevy_reflect/src/utility.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use std::any::{Any, TypeId};
4040
/// # fn into_any(self: Box<Self>) -> Box<dyn Any> { todo!() }
4141
/// # fn as_any(&self) -> &dyn Any { todo!() }
4242
/// # fn as_any_mut(&mut self) -> &mut dyn Any { todo!() }
43+
/// # fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> { todo!() }
4344
/// # fn as_reflect(&self) -> &dyn Reflect { todo!() }
4445
/// # fn as_reflect_mut(&mut self) -> &mut dyn Reflect { todo!() }
4546
/// # fn apply(&mut self, value: &dyn Reflect) { todo!() }
@@ -102,6 +103,7 @@ impl NonGenericTypeInfoCell {
102103
/// # fn into_any(self: Box<Self>) -> Box<dyn Any> { todo!() }
103104
/// # fn as_any(&self) -> &dyn Any { todo!() }
104105
/// # fn as_any_mut(&mut self) -> &mut dyn Any { todo!() }
106+
/// # fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> { todo!() }
105107
/// # fn as_reflect(&self) -> &dyn Reflect { todo!() }
106108
/// # fn as_reflect_mut(&mut self) -> &mut dyn Reflect { todo!() }
107109
/// # fn apply(&mut self, value: &dyn Reflect) { todo!() }

0 commit comments

Comments
 (0)