#Take ownership of a field from reflected Struct?
31 messages · Page 1 of 1 (latest)
according to the docs.rs search, the only methods in bevy_reflect takng a Box are PartialReflect::reflect_owned and all the into_anys
I use the box to do reflect_owned and into_struct, giving me an owned struct box
lemme edit the text
i mean, you could use DynamicStruct::into_iter, but to_dynamic_struct clones the fields anyway
what struct field do you want to extract anyway? could try a reflect type registration or a downcast
I have a generic type
```rs
#[derive(Debug, Clone, Reflect)]
#[reflect(@TraverseKind::Entry)]
pub struct Entry<T: Sized> {
id: RawId,
data: T,
}
and I want to extract the `data` field
I can't downcast directly since it's generic
swap the order and repr(C) for the easy unsafe way, use a TypeData for the moderate safe way
or actually, repr(C) and the size of RawId would also work
huh, how would I do that?
Do I need to get a pointer from the Box and repr c allows me to read a field from it?
sorry, I'm not very familiar with raw pointer manip so would appreciate some pointers
you can cast the first field of a repr(C) struct from the struct, since they start in the same place
though you'd need the vtable ptr or something too
which the other way makes easier
the other way being reflect_trait with a trait that's just impl'd for the struct, or equivalent custom typedata, fyi
hmm, custom trait can work
tho I will need to find a way to take data out without owned receiver since that would make trait not dyn safe
self:Box<Self> -> Box<T> ?
... wonder if it'd be worth trying to reuse the allocation
might actually be automatic, since Box is technically a primitive
have you tried Reflect::into_any and then downcasting it into your Entry type? https://docs.rs/bevy/latest/bevy/prelude/trait.Reflect.html#tymethod.into_any
A core trait of bevy_reflect, used for downcasting to concrete types.
it's generic, TypeId::of::<Entry<()>>() != TypeId::of::<Entry<u8>>()
when you retrieve the field, you don't know it's concrete type yet?
you want it as a dyn PartialReflect ?
would it fit your use case to change your Entry's data to a Box<dyn Reflect>? if that works for you, it'd be simpler than messing around with the pointers yourself
you can also try using something like the yoke crate to pass an owning reference to the field as a dyn PartialReflect that hides the rest of the struct but it won't actually discard it
Nope
Box<dyn Reflect>, yeah
Nope, Entry is part of deserialization process that needs to know concrete type