mlua icon indicating copy to clipboard operation
mlua copied to clipboard

Mix `UserData` and serde fields automatically.

Open JSH32 opened this issue 3 years ago • 3 comments

I have something like this

#[derive(Serialize, Deserialize, Clone)]
pub struct Uri {
    scheme: Option<String>,
    authority: Option<Authority>,
    path: String,
    query: Option<String>,
}

impl UserData for Uri {
    fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
        methods.add_method("tostring", |_, this, ()| {
            let uri: http::Result<hyper::Uri> = this.to_owned().into();
            Ok(uri.to_lua_err()?.to_string())
        })
    }
}

I'm facing an issue where I want lua to be able to access all the fields on Uri while also being able to access UserData methods. I've attempted using create_ser_userdata which gives me access to UserData but prevents me from accessing the fields once again. Upon re-serializing the object made with create_ser_userdata I still see the proper fields present.

Is there a way to mix and match UserData and regular fields in a serializable struct?

JSH32 avatar Jan 02 '23 19:01 JSH32

so you basically want a Derive macro for add_fields

walksanatora avatar Jan 31 '23 19:01 walksanatora

so that you dont have to write the add_fields function yourself

walksanatora avatar Jan 31 '23 19:01 walksanatora