It would be useful to allow access to UserData across different dynamic library modules for structs that are guaranteed to have the same size and alignment. The bytemuck crate exports a few traits that can be implemented using declarative macros in a safe interface that could be used for that purpose.
mlua could export something like:
impl AnyUserData {
pub fn from_bytes<T : bytemuck::AnyBitPattern>(&self) -> Result<&T,mlua::Error> { ... }
}
If dependence on bytemuck is undesirable, perhaps AnyUserData could export access to the raw byte slice, done after a few runtime checks (see here an implementation), in which case the user would be responsible for the explicit cast:
impl AnyUserData {
pub fn as_bytes<T : Sized>(&self) -> Result<&[u8],mlua::Error> { ... }
}
I don't know how viable this is, however, since mlua would need to keep some information at runtime about the size and alignment of any registered types, and I don't know if this is the case.