Skip to content

Is it possible for a UserData method to return a reference to this (self)? #357

Closed Answered by khvzak
drauschenbach asked this question in Q&A
Discussion options

You must be logged in to vote

You need to use add_function which will receive userdata as first arg in form of AnyUserData to pass back.

impl mlua::UserData for MessageBuilder {
    fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
        methods.add_function("partition", |_, (ud, partition): (AnyUserData, _)| {
            let mut this = ud.borrow_mut::<Self>()?;
            this.partition = Some(partition);
            Ok(ud)
        });
    }
}

Also you don't really need to mutex to simply change struct fields, as can always borrow underlying data mutably.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@drauschenbach
Comment options

Answer selected by drauschenbach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants