Skip to content

Commit b2ce4d8

Browse files
MrGVSVjames7132
authored andcommitted
bevy_reflect: Added get_boxed method to reflect_trait (bevyengine#4120)
# Objective Allow `Box<dyn Reflect>` to be converted into a `Box<dyn MyTrait>` using the `#[reflect_trait]` macro. The other methods `get` and `get_mut` only provide a reference to the reflected object. ## Solution Add a `get_boxed` method to the `Reflect***` struct generated by the `#[reflect_trait]` macro. This method takes in a `Box<dyn Reflect>` and returns a `Box<dyn MyTrait>`. Co-authored-by: MrGVSV <49806985+MrGVSV@users.noreply.github.com>
1 parent 7bde9a6 commit b2ce4d8

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

crates/bevy_reflect/bevy_reflect_derive/src/trait_reflection.rs

+35
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,62 @@ impl Parse for TraitInfo {
2121
}
2222
}
2323

24+
/// A trait attribute macro that allows a reflected type to be downcast to a trait object.
25+
///
26+
/// This generates a struct that takes the form `ReflectMyTrait`. An instance of this struct can then be
27+
/// used to perform the conversion.
2428
pub(crate) fn reflect_trait(_args: &TokenStream, input: TokenStream) -> TokenStream {
2529
let trait_info = parse_macro_input!(input as TraitInfo);
2630
let item_trait = &trait_info.item_trait;
2731
let trait_ident = &item_trait.ident;
2832
let trait_vis = &item_trait.vis;
2933
let reflect_trait_ident = crate::utility::get_reflect_ident(&item_trait.ident.to_string());
3034
let bevy_reflect_path = BevyManifest::default().get_path("bevy_reflect");
35+
36+
let struct_doc = format!(
37+
" A type generated by the #[reflect_trait] macro for the `{}` trait.\n\n This allows casting from `dyn Reflect` to `dyn {}`.",
38+
trait_ident,
39+
trait_ident
40+
);
41+
let get_doc = format!(
42+
" Downcast a `&dyn Reflect` type to `&dyn {}`.\n\n If the type cannot be downcast, `None` is returned.",
43+
trait_ident,
44+
);
45+
let get_mut_doc = format!(
46+
" Downcast a `&mut dyn Reflect` type to `&mut dyn {}`.\n\n If the type cannot be downcast, `None` is returned.",
47+
trait_ident,
48+
);
49+
let get_box_doc = format!(
50+
" Downcast a `Box<dyn Reflect>` type to `Box<dyn {}>`.\n\n If the type cannot be downcast, this will return `Err(Box<dyn Reflect>)`.",
51+
trait_ident,
52+
);
53+
3154
TokenStream::from(quote! {
3255
#item_trait
3356

57+
#[doc = #struct_doc]
3458
#[derive(Clone)]
3559
#trait_vis struct #reflect_trait_ident {
3660
get_func: fn(&dyn #bevy_reflect_path::Reflect) -> Option<&dyn #trait_ident>,
3761
get_mut_func: fn(&mut dyn #bevy_reflect_path::Reflect) -> Option<&mut dyn #trait_ident>,
62+
get_boxed_func: fn(Box<dyn #bevy_reflect_path::Reflect>) -> Result<Box<dyn #trait_ident>, Box<dyn #bevy_reflect_path::Reflect>>,
3863
}
3964

4065
impl #reflect_trait_ident {
66+
#[doc = #get_doc]
4167
pub fn get<'a>(&self, reflect_value: &'a dyn #bevy_reflect_path::Reflect) -> Option<&'a dyn #trait_ident> {
4268
(self.get_func)(reflect_value)
4369
}
4470

71+
#[doc = #get_mut_doc]
4572
pub fn get_mut<'a>(&self, reflect_value: &'a mut dyn #bevy_reflect_path::Reflect) -> Option<&'a mut dyn #trait_ident> {
4673
(self.get_mut_func)(reflect_value)
4774
}
75+
76+
#[doc = #get_box_doc]
77+
pub fn get_boxed(&self, reflect_value: Box<dyn #bevy_reflect_path::Reflect>) -> Result<Box<dyn #trait_ident>, Box<dyn #bevy_reflect_path::Reflect>> {
78+
(self.get_boxed_func)(reflect_value)
79+
}
4880
}
4981

5082
impl<T: #trait_ident + #bevy_reflect_path::Reflect> #bevy_reflect_path::FromType<T> for #reflect_trait_ident {
@@ -55,6 +87,9 @@ pub(crate) fn reflect_trait(_args: &TokenStream, input: TokenStream) -> TokenStr
5587
},
5688
get_mut_func: |reflect_value| {
5789
reflect_value.downcast_mut::<T>().map(|value| value as &mut dyn #trait_ident)
90+
},
91+
get_boxed_func: |reflect_value| {
92+
reflect_value.downcast::<T>().map(|value| value as Box<dyn #trait_ident>)
5893
}
5994
}
6095
}

0 commit comments

Comments
 (0)