Code Monkey home page Code Monkey logo

Comments (4)

khvzak avatar khvzak commented on July 17, 2024

UserData needs 'static due to TypeId::of::<T> requirements.
TypeId is used for generating and attaching shared metatable to UserData.

As an alternative, you could use Scope::create_nonstatic_userdata, it does not have 'static requirements and as a tradeoff uses unique metatable per instance (which is less efficient).

from mlua.

rise0chen avatar rise0chen commented on July 17, 2024
let uart = lua.scope(|scope| {
    scope.create_nonstatic_userdata(Uart {
        on_message: lua.create_function(|_, msg: String| Ok(()))?,
    })
})?;
pub fn scope<'lua, 'scope, R, F>(&'lua self, f: F) -> Result<R>
where
    'lua: 'scope,
    R: 'static,
    F: FnOnce<(&Scope<'lua, 'scope>,), Output = Result<R>>,

Oh no, Result<R>> require 'static.

from mlua.

khvzak avatar khvzak commented on July 17, 2024

This is how Scope works - it drops scoped objects on function leave.

  1. You could place logic inside Scope
  2. Or alternatively store Lua objects in Registry using Lua::create_registry_value and keep RegistryKey which is 'static
    You can retrieve values back using Lua::registry_value

from mlua.

rise0chen avatar rise0chen commented on July 17, 2024

thanks, it's perfect

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.