Code Monkey home page Code Monkey logo

Comments (3)

khvzak avatar khvzak commented on July 17, 2024 3

Probably the easiest option would be passing Rc<Lua> to MyUserdata to create MyUserdataHander then.

Alternalively you can store temporary values in Registry and create via Lua::create_registry_value.
RegistryKey has Drop implemented to remove data from the Registry on drop (but you still need to call Lua:: expire_registry_values)

I maybe implement a better way how to attach custom destructors to UserData with executing arbitrary Lua code. Need to think about it.

from mlua.

khvzak avatar khvzak commented on July 17, 2024

Could you provide a code example?

__gc is reserved for internal use (for Drop especially) but you should be able to do cleanup by implementing drop().

If you want in particular to execute arbitrary Lua code on drop (== __gc) you can add Lua instance to your userdata.

    struct MyUserdata {
        lua: Rc<Lua>,
    }

    impl LuaUserData for MyUserdata {}

    impl Drop for MyUserdata {
        fn drop(&mut self) {
            let _ = self.lua.load("print(\"userdata dropped\")").exec();
        }
    }

from mlua.

attila-lin avatar attila-lin commented on July 17, 2024
struct MyUserdataHander {
    idx: u64
}

struct MyUserdata;

impl UserData for MyUserdata {
    methods.add_function("Some", |lua, value: Value| {
        let router_ud = lua.globals().get::<_, AnyUserData>("OptionRouterUD")?;
                let idx = OptionalRouter::register(lua, (router_ud, Some(value)))?;
                Ok(MyUserdataHander { idx })
    });
}

I thought about the Rc of Lua in the userdata, but I will have to struggle Rc<&lua> lifetime because the add_function provides the &Lua.

from mlua.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.