hlua icon indicating copy to clipboard operation
hlua copied to clipboard

Macros for easier userdata

Open tomaka opened this issue 11 years ago • 4 comments

Something like would be great:

#[lua-userdata]
struct Foo {
    value: int,
}

#[lua-userdata]
impl Foo {
    pub fn get_value(&self) -> int {
        self.value
    }
}

lua.set("foo", Foo{ value: 5 });   // automatically fills the metatable
lua.execute("return foo:get_value()");  // would return 5

There are probably technical limitations, limiting this syntax extension to a single impl block, but maybe there's a trick that I didn't think about.

tomaka avatar Jul 30 '14 07:07 tomaka

The trick is processing an entire module at once. I might make an attempt at implementation.

#[lua-userdata]
mod bar {
    struct Foo {
        value: int,
    }

    impl Foo {
        pub fn get_value(&self) -> int {
            self.value
        }
    }
}

pczarn avatar Aug 07 '14 20:08 pczarn

That would be very nice!

However the rest of the library is not ready to support this just yet. The parameters of a callback are currently loaded by copy only, and you don't want to make a copy of your object when you call a method.

tomaka avatar Aug 08 '14 06:08 tomaka

This stuff would be nightly-only, as it would be a compiler plugin, right?

mkpankov avatar Apr 18 '17 20:04 mkpankov

@mkpankov Yes

tomaka avatar Apr 19 '17 06:04 tomaka