`Reflect::clone_dynamic` on `Handle<T>` doesn't increase refcount
Bevy version
Current main (6f2cc0b30e335f3084f7028498c0866279c78099).
What you did
fn check(mut assets: ResMut<Assets<StandardMaterial>>) {
let handle = assets.add(StandardMaterial::default());
assert!(handle.is_strong());
let handle: &dyn Reflect = &handle;
let cloned_handle = handle.clone_value();
let cloned_handle: Handle<StandardMaterial> =
FromReflect::from_reflect(&*cloned_handle).unwrap();
assert!(cloned_handle.is_strong());
}
What you expected
I expected the clone into a DynamicStruct to clone the sender as well, so that the asset refcount is accurate.
Why this went wrong
The HandleType (which is either Weak or Strong(Sender)) is #[reflect(ignore)], so it doesn't get included in the DynamicStruct returned by clone_value and the default value of Weak is used.
https://github.com/bevyengine/bevy/blob/f531a94370755155accd625c58a06f07d33edcce/crates/bevy_asset/src/handle.rs#L110-L111
How to solve this
First I thought, we can just implement Clone on the HandleType (increasing the refcount in the process) but that doesn't work as the HandleType doesn't contain the handle id which is necessary for the refcount increase.
We can maybe implement Reflect manually and insert a refcount increase in the right places, but that is a bit annoying as well.
Or we can add ReflectClone type data that just uses the Clone impl and keeps the underlying type, instead of returning a DynamicX version of the type.