Giter Club home page Giter Club logo

Comments (4)

patata3000 avatar patata3000 commented on July 4, 2024 1

Ok I'm answering my own question. Time gave me open mindedness. This was something I kind of realised already but with new information, it got fuzzy in my head. I explain it here for myself and anybody else that could be interested.

So to interact in lua, you've got 2 ways (back and forth). Either:

  1. You need to run some Lua function, get back some lua values (tables, boolean, integers...) in your Rust code.
    Here, in general, you can use mlua crate. You get global lua state with lua.globals() and you require modules, functions, tables... that you can translate into Rust data types and interact with. So this is how you get Lua into Rust.
    However, what have done our dear friend here @noib3 is to give access to the nvim API. Being able to manipulate buffers, windows and whatnot in the nvim world without using the (kinda heavy) mlua interface (it's using C bindings directly)

  2. Lua calls some rust code that needs to be made accessible with #[nvim_oxi::module],
    For this part, we're using Rust from the Lua code. We get from nvim_oxi some nice bindings that let us convert Rust into Lua easily with: Function::from_fn, Object::from, Dictionary::from_iter...

Ok so now there is a bit more. When you expose a function using with Function::from_fn(my_func) , you may want to let the Lua code to send and receive some data as parameters to my_func (which is written in Rust).

  • You may want this data parameter to fill a Rust struct that you can use idiomatically in your Rust code.
  • You may want to return this data struct to Lua.
    This is why FromObject with Poppable and, ToObject with Pushable traits have been created. This is demonstrated in the mechanic example.

Now, mlua crate offers Rust code writers to implement FromLua, FromLuaMulti and IntoLua, IntoLuaMulti. IntoLua is the traits I need to implement if I want to be able to write this line:

popup.call(popup_opts)?;

Now I'm still a troubled man you see. Both FromLua (in mlua) and FromObject/Poppable (in nvim_oxi) use Serialize and Deserialize traits. I wish to know if there is a way to combine the implementations of the traits from nvim_oxi with the ones from mlua already. If not, would it be possible? Soon? At some point? Never? Does it even make sense?

About the answer of my question more specifically, this works nicely and it makes me think that my questions are probably not worth it:

impl<'lua> IntoLua<'lua> for PopupOpts {
    fn into_lua(self, lua: &'lua Lua) -> LuaResult<LuaValue<'lua>> {
        lua.to_value(&self)
    }
}

from nvim-oxi.

noib3 avatar noib3 commented on July 4, 2024 1

We'd need specialization for that to work.

from nvim-oxi.

noib3 avatar noib3 commented on July 4, 2024

As you noted, both our traits and the ones exposed by mlua can be implemented on top of the type's Serialize and Deserialize impls.

I wish to know if there is a way to combine the implementations of the traits from nvim_oxi with the ones from mlua already

If I understand you correctly you'd like to have something like this:

#[cfg(feature = "mlua")]
mod mlua_impls {
    impl<T: ToObject> IntoLua for T { .. }
    impl<T: FromObject> FromLua for T { .. }
}

unfortunately this results in a compiler error because of the orphan rule.

from nvim-oxi.

patata3000 avatar patata3000 commented on July 4, 2024

Yes, it would be something like this but I don't know what would be the right way to do it.

And what about the other way around? Not sure it makes sense again

#[cfg(feature = "mlua")]
mod mlua_impls {
    impl<T: IntoLua> ToObject for T { .. }
    impl<T: FromLua> FromObject for T { .. }
}

from nvim-oxi.

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.