Comments (4)
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:
-
You need to run some Lua function, get back some lua values (tables, boolean, integers...) in your Rust code.
Here, in general, you can usemlua
crate. You getglobal
lua state withlua.globals()
and yourequire
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) -
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 fromnvim_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 whyFromObject
withPoppable
and,ToObject
withPushable
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.
We'd need specialization for that to work.
from nvim-oxi.
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 frommlua
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.
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)
- [Windows][Powershell7] cannot find type `PathBuf` in this scope HOT 3
- [Windows][Powershell7] linking with `link.exe` failed: exit code: 1120 HOT 5
- [Ubuntu] Error while calling lua chunk... undefined symbol: luaopen_... HOT 1
- support derive
- [help] How to call other lua plugin exposed methods HOT 4
- Crash when loading the example plugin on Windows HOT 29
- Test macro doesn't seem to pick up on failures HOT 6
- nvim_oxi::api::types::Mode doesn't allow for Remap ("m") mode HOT 2
- Add Installation Instructions HOT 1
- `println!` writes to nvim process HOT 4
- Not sure you answered my question HOT 2
- Can't find buffer number from `Buffer` HOT 6
- Use LuaFunction as `rhs` for `buffer_keymap_set` that should capture local variable HOT 2
- [Question] Handling custom errors HOT 4
- f32 conversion issue? HOT 2
- Failed to resolve: could not find `plugin` in `nvim_oxi` HOT 1
- "Validation complete used without nargs" error on stable
- Version `0.4.2` gives me compilation errors HOT 3
- Api nvim_cmd not working HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nvim-oxi.