Skip to content

Commit

Permalink
✨ (luajit-bindings) implement Pushable for String and Option<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 4, 2022
1 parent 9d199cf commit 79130ba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
36 changes: 34 additions & 2 deletions crates/luajit-bindings/src/pushable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::c_int;
use std::ffi::{c_char, c_int};

use crate::ffi::{self, lua_Integer, lua_Number, lua_State};
use crate::macros::count;
Expand Down Expand Up @@ -111,7 +111,39 @@ impl Pushable for f32 {
}
}

impl<T: Pushable> Pushable for Vec<T> {
impl Pushable for String {
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
ffi::lua_pushlstring(
lstate,
self.as_ptr() as *const c_char,
self.len(),
);
Ok(1)
}
}

impl<T> Pushable for Option<T>
where
T: Pushable,
{
unsafe fn push(
self,
lstate: *mut lua_State,
) -> Result<c_int, crate::Error> {
match self {
Some(t) => t.push(lstate),
None => ().push(lstate),
}
}
}

impl<T> Pushable for Vec<T>
where
T: Pushable,
{
unsafe fn push(
self,
lstate: *mut lua_State,
Expand Down
3 changes: 3 additions & 0 deletions crates/nvim-oxi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Clone, Debug, ThisError)]
#[cfg_attr(not(feature = "mlua"), derive(Eq, PartialEq))]
pub enum Error {
#[error(transparent)]
LuaError(#[from] luajit_bindings::Error),

#[error(transparent)]
NvimError(#[from] nvim_types::Error),

Expand Down
6 changes: 3 additions & 3 deletions crates/nvim-oxi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ pub mod libuv {
pub mod mlua {
/// Returns a static reference to a
/// [`mlua::Lua`](https://docs.rs/mlua/latest/mlua/struct.Lua.html) object
/// to be able to interact with other Lua plugins.
/// which can be used to interact with Lua plugins.
pub fn lua() -> &'static mlua::Lua {
unsafe {
crate::lua::with_state(|lstate| {
mlua::Lua::init_from_ptr(lstate as *mut _).into_static()
luajit_bindings::with_state(|lua_state| {
mlua::Lua::init_from_ptr(lua_state as *mut _).into_static()
})
}
}
Expand Down

0 comments on commit 79130ba

Please sign in to comment.