Skip to content

Allow access to UserData content across the FFI boundary for compact (#[repr(C)]) structures #309

@limads

Description

@limads

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions