Skip to content

Commit

Permalink
luvit: sketch it
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 2, 2023
1 parent c205268 commit b3e8f2d
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 49 deletions.
12 changes: 6 additions & 6 deletions crates/nvim-oxi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords.workspace = true
readme.workspace = true

[package.metadata.docs.rs]
features = ["neovim-0-9", "libuv", "mlua", "test"]
features = ["neovim-0-9", "luvit", "mlua", "test"]
rustdoc-args = ["--cfg", "docsrs"]

[features]
Expand All @@ -19,14 +19,14 @@ neovim-0-9 = ["oxi-api/neovim-0-9"]
neovim-nightly = ["oxi-api/neovim-nightly"]

# diagnostic = ["oxi-diagnostic"]
libuv = ["oxi-libuv"]
luvit = ["oxi-luvit"]
mlua = ["dep:mlua"]
test = ["oxi-macros/test", "miniserde"]

[dependencies]
oxi-api = { workspace = true }
# oxi-diagnostic = { workspace = true }
oxi-libuv = { version = "0.3.0", path = "../oxi-libuv", optional = true }
oxi-luvit = { version = "0.3.0", path = "../oxi-luvit", optional = true }
oxi-luajit = { workspace = true }
oxi-macros = { workspace = true }
oxi-types = { workspace = true }
Expand All @@ -51,10 +51,10 @@ path = "../../examples/calc.rs"
crate-type = ["cdylib"]

[[example]]
name = "libuv"
path = "../../examples/libuv.rs"
name = "luvit"
path = "../../examples/luvit.rs"
crate-type = ["cdylib"]
required-features = ["libuv"]
required-features = ["luvit"]

[[example]]
name = "mechanic"
Expand Down
3 changes: 0 additions & 3 deletions crates/nvim-oxi/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ where
{
lua::init(lua_state);

#[cfg(feature = "libuv")]
oxi_libuv::init(lua_state);

match body() {
Ok(api) => api.push(lua_state).unwrap(),
Err(err) => lua::utils::handle_error(lua_state, &err),
Expand Down
4 changes: 2 additions & 2 deletions crates/nvim-oxi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub enum Error {
#[error(transparent)]
Serde(#[from] oxi_types::serde::Error),

#[cfg(feature = "libuv")]
#[cfg(feature = "luvit")]
#[error(transparent)]
Libuv(#[from] oxi_libuv::Error),
Luvit(#[from] oxi_luvit::Error),

#[cfg(feature = "mlua")]
#[error(transparent)]
Expand Down
12 changes: 6 additions & 6 deletions crates/nvim-oxi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ pub mod api {
pub use oxi_api::*;
}

#[cfg(feature = "libuv")]
#[cfg_attr(docsrs, doc(cfg(feature = "libuv")))]
pub mod libuv {
//! Bindings to the [Neovim event loop][loop] powered by [libuv].
#[cfg(feature = "luvit")]
#[cfg_attr(docsrs, doc(cfg(feature = "luvit")))]
pub mod luvit {
//! Bindings to the [Neovim event loop][loop] powered by [luvit].
//!
//! [loop]: https://neovim.io/doc/user/lua.html#vim.loop
//! [libuv]: https://libuv.org/
//! [luvit]: https://luvit.org/
#[doc(inline)]
pub use oxi_libuv::*;
pub use oxi_luvit::*;
}

pub mod lua {
Expand Down
13 changes: 0 additions & 13 deletions crates/oxi-libuv/src/lib.rs

This file was deleted.

6 changes: 2 additions & 4 deletions crates/oxi-libuv/Cargo.toml → crates/oxi-luvit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "oxi-libuv"
description = "Rust bindings to libuv for nvim-oxi"
name = "oxi-luvit"
description = "Rust bindings to luvit for nvim-oxi"
version.workspace = true
edition.workspace = true
repository.workspace = true
Expand All @@ -10,6 +10,4 @@ license.workspace = true
[dependencies]
oxi-luajit = { workspace = true }

libuv-sys2 = "1.44"
once_cell = "1.17"
thiserror = "1.0"
10 changes: 5 additions & 5 deletions crates/oxi-libuv/src/async.rs → crates/oxi-luvit/src/async.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::error::Error as StdError;

use libuv_sys2::{self as ffi, uv_async_t};
use luvit_sys2::{self as ffi, uv_async_t};

use crate::{Error, Handle};

type Callback = Box<dyn FnMut() -> Result<(), Box<dyn StdError>> + 'static>;

/// Binding to libuv's [Async handle][1] used to trigger the execution of a
/// Binding to luvit's [Async handle][1] used to trigger the execution of a
/// callback in the Neovim thread.
///
/// [1]: http://docs.libuv.org/en/v1.x/async.html
/// [1]: http://docs.luvit.org/en/v1.x/async.html
#[derive(Clone)]
pub struct AsyncHandle {
handle: Handle<uv_async_t, Callback>,
Expand Down Expand Up @@ -49,14 +49,14 @@ impl AsyncHandle {
/// this handle. It is safe to call this function from any thread. The
/// callback will be called on the main thread.
///
/// NOTE: [libuv] will coalesce calls to [`AsyncHandle::send`], that is,
/// NOTE: [luvit] will coalesce calls to [`AsyncHandle::send`], that is,
/// not every call to it will yield an execution of the callback. For
/// example: if [`AsyncHandle::send`] is called 5 times in a row before the
/// callback is called, the callback will only be called once. If
/// [`AsyncHandle::send`] is called again after the callback was called, it
/// will be called again.
///
/// [libuv]: https://libuv.org/
/// [luvit]: https://luvit.org/
pub fn send(&self) -> Result<(), Error> {
let retv =
unsafe { ffi::uv_async_send(self.handle.as_ptr() as *mut _) };
Expand Down
20 changes: 20 additions & 0 deletions crates/oxi-luvit/src/async_handle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::luajit::{Function, Userdata};

pub struct AsyncHandle {
handle: Userdata,
}

impl AsyncHandle {
pub fn new<Cb>(callback: Cb) -> Self
where
Cb: FnMut() -> () + 'static,
{
let callback = Function::from_fn_mut(callback);
let handle = luv!(new_async, callback, Userdata);
Self { handle }
}

pub fn send(&self) {
uv!(async_send, self.handle);
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::alloc::{self, Layout};
use std::ffi::c_void;
use std::marker::PhantomData;

use libuv_sys2::{self as ffi, uv_handle_t, uv_loop_t};
use luvit_sys2::{self as ffi, uv_handle_t, uv_loop_t};

use crate::{Error, Result};

Expand Down
8 changes: 8 additions & 0 deletions crates/oxi-luvit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod async_handle;
mod luv;
mod timer_handle;

pub use async_handle::AsyncHandle;
use luv::Luv;
use oxi_luajit as luajit;
pub use timer_handle::TimerHandle;
4 changes: 2 additions & 2 deletions crates/oxi-libuv/src/loop.rs → crates/oxi-luvit/src/loop.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use libuv_sys2::uv_loop_t;
use luvit_sys2::uv_loop_t;
use once_cell::unsync::OnceCell;
use oxi_luajit::ffi::lua_State;

Expand All @@ -20,7 +20,7 @@ pub unsafe fn init(lua_state: *mut lua_State) {
LOOP.with(|uv_loop| uv_loop.set(luv_loop(lua_state))).unwrap_unchecked();
}

/// Executes a function with access to the libuv loop.
/// Executes a function with access to the luvit loop.
///
/// NOTE: this will segfault if the loop has not been initialized by calling
/// [init].
Expand Down
8 changes: 8 additions & 0 deletions crates/oxi-luvit/src/luv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// pub(crate) static LUV: todo!() = todo!();

#[macro_export]
macro_rules! luv {
($($t:tt)*) => {
todo!()
};
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::error::Error as StdError;
use std::time::Duration;

use libuv_sys2::{self as ffi, uv_timer_t};
use luvit_sys2::{self as ffi, uv_timer_t};

use crate::{Error, Handle};

pub(crate) type Callback = Box<
dyn FnMut(&mut TimerHandle) -> Result<(), Box<dyn StdError>> + 'static,
>;

/// Binding to libuv's [Timer handle][1] used to schedule callbacks to be
/// Binding to luvit's [Timer handle][1] used to schedule callbacks to be
/// called in the future.
///
/// [1]: http://docs.libuv.org/en/v1.x/timer.html
/// [1]: http://docs.luvit.org/en/v1.x/timer.html
pub struct TimerHandle {
handle: Handle<uv_timer_t, Callback>,
}
Expand Down
5 changes: 5 additions & 0 deletions crates/oxi-luvit/src/timer_handle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use crate::luajit::{Function, Userdata};

pub struct TimerHandle {
handle: Userdata,
}
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ assert(fixed.problem == nil)
Shows how to integrate `nvim-oxi` with the
[`mlua`](https://github.com/khvzak/mlua) crate.

## [`libuv`](./libuv.rs)
## [`luvit`](./luvit.rs)

Shows how to use the `nvim_oxi::libuv` module to trigger a callback registered
Shows how to use the `nvim_oxi::luvit` module to trigger a callback registered
on the Neovim thread from other threads.

# Crate setup
Expand Down
4 changes: 2 additions & 2 deletions examples/libuv.rs → examples/luvit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::thread;
use std::time::Duration;

use nvim_oxi as oxi;
use oxi::libuv::{AsyncHandle, TimerHandle};
use oxi::luvit::{AsyncHandle, TimerHandle};
use oxi::print;
use tokio::sync::mpsc::{self, UnboundedSender};
use tokio::time;

#[oxi::module]
fn libuv() -> oxi::Result<()> {
fn luvit() -> oxi::Result<()> {
// --
let mut n = 0;

Expand Down

0 comments on commit b3e8f2d

Please sign in to comment.