Skip to content

Commit

Permalink
refactor: upgrade to mlua v0.10.0 (#1842)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Nov 8, 2024
1 parent baf062f commit 39bce0b
Show file tree
Hide file tree
Showing 95 changed files with 664 additions and 651 deletions.
44 changes: 21 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ strip = true

[workspace.dependencies]
ansi-to-tui = "7.0.0"
anyhow = "1.0.92"
anyhow = "1.0.93"
base64 = "0.22.1"
bitflags = "2.6.0"
clap = { version = "4.5.20", features = [ "derive" ] }
crossterm = { version = "0.28.1", features = [ "event-stream" ] }
dirs = "5.0.1"
futures = "0.3.31"
globset = "0.4.15"
libc = "0.2.161"
libc = "0.2.162"
md-5 = "0.10.6"
mlua = { version = "0.9.9", features = [ "lua54", "serialize", "macros", "async" ] }
mlua = { git = "https://github.com/mlua-rs/mlua.git", features = [ "anyhow", "async", "error-send", "lua54", "macros", "serialize" ] }
parking_lot = "0.12.3"
ratatui = { version = "0.29.0", features = [ "unstable-rendered-line-info" ] }
regex = "1.11.1"
scopeguard = "1.2.0"
serde = { version = "1.0.214", features = [ "derive" ] }
serde_json = "1.0.132"
shell-words = "1.1.0"
tokio = { version = "1.41.0", features = [ "full" ] }
tokio = { version = "1.41.1", features = [ "full" ] }
tokio-stream = "0.1.16"
tokio-util = "0.7.12"
tracing = { version = "0.1.40", features = [ "max_level_debug", "release_max_level_warn" ] }
Expand Down
2 changes: 1 addition & 1 deletion yazi-dds/src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<'a> Body<'a> {
pub fn with_sender(self, sender: u64) -> Payload<'a> { Payload::new(self).with_sender(sender) }
}

impl IntoLua<'_> for Body<'static> {
impl IntoLua for Body<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
match self {
Self::Hi(b) => b.into_lua(lua),
Expand Down
4 changes: 2 additions & 2 deletions yazi-dds/src/body/bulk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'a> From<BodyBulk<'a>> for Body<'a> {
fn from(value: BodyBulk<'a>) -> Self { Self::Bulk(value) }
}

impl IntoLua<'_> for BodyBulk<'static> {
impl IntoLua for BodyBulk<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
BodyBulkIter { inner: self.changes.into_iter() }.into_lua(lua)
}
Expand All @@ -48,7 +48,7 @@ pub struct BodyBulkIter {
}

impl UserData for BodyBulkIter {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(MetaMethod::Len, |_, me, ()| Ok(me.inner.len()));

methods.add_meta_function(MetaMethod::Pairs, |lua, me: AnyUserData| {
Expand Down
4 changes: 2 additions & 2 deletions yazi-dds/src/body/bye.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ impl<'a> From<BodyBye> for Body<'a> {
fn from(value: BodyBye) -> Self { Self::Bye(value) }
}

impl IntoLua<'_> for BodyBye {
fn into_lua(self, _: &Lua) -> mlua::Result<Value<'_>> {
impl IntoLua for BodyBye {
fn into_lua(self, _: &Lua) -> mlua::Result<Value> {
Err("BodyBye cannot be converted to Lua").into_lua_err()
}
}
2 changes: 1 addition & 1 deletion yazi-dds/src/body/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a> From<BodyCd<'a>> for Body<'a> {
fn from(value: BodyCd<'a>) -> Self { Self::Cd(value) }
}

impl IntoLua<'_> for BodyCd<'static> {
impl IntoLua for BodyCd<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
if let Some(Cow::Owned(url)) = Some(self.url).filter(|_| !self.dummy) {
lua.create_table_from([
Expand Down
2 changes: 1 addition & 1 deletion yazi-dds/src/body/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl From<BodyCustom> for Body<'_> {
fn from(value: BodyCustom) -> Self { Self::Custom(value) }
}

impl IntoLua<'_> for BodyCustom {
impl IntoLua for BodyCustom {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> { Sendable::data_to_value(lua, self.data) }
}

Expand Down
4 changes: 2 additions & 2 deletions yazi-dds/src/body/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ impl<'a> From<BodyDelete<'a>> for Body<'a> {
fn from(value: BodyDelete<'a>) -> Self { Self::Delete(value) }
}

impl IntoLua<'_> for BodyDelete<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value<'_>> {
impl IntoLua for BodyDelete<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
let urls = lua.create_table_with_capacity(self.urls.len(), 0)?;

// In most cases, `self.urls` will be `Cow::Owned`, so
Expand Down
4 changes: 2 additions & 2 deletions yazi-dds/src/body/hey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl From<BodyHey> for Body<'_> {
fn from(value: BodyHey) -> Self { Self::Hey(value) }
}

impl IntoLua<'_> for BodyHey {
fn into_lua(self, _: &Lua) -> mlua::Result<Value<'_>> {
impl IntoLua for BodyHey {
fn into_lua(self, _: &Lua) -> mlua::Result<Value> {
Err("BodyHey cannot be converted to Lua").into_lua_err()
}
}
4 changes: 2 additions & 2 deletions yazi-dds/src/body/hi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl<'a> From<BodyHi<'a>> for Body<'a> {
fn from(value: BodyHi<'a>) -> Self { Self::Hi(value) }
}

impl IntoLua<'_> for BodyHi<'_> {
fn into_lua(self, _: &Lua) -> mlua::Result<Value<'_>> {
impl IntoLua for BodyHi<'_> {
fn into_lua(self, _: &Lua) -> mlua::Result<Value> {
Err("BodyHi cannot be converted to Lua").into_lua_err()
}
}
2 changes: 1 addition & 1 deletion yazi-dds/src/body/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'a> From<BodyHover<'a>> for Body<'a> {
fn from(value: BodyHover<'a>) -> Self { Self::Hover(value) }
}

impl IntoLua<'_> for BodyHover<'static> {
impl IntoLua for BodyHover<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
if let Some(Cow::Owned(url)) = self.url {
lua.create_table_from([
Expand Down
4 changes: 2 additions & 2 deletions yazi-dds/src/body/move_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'a> From<BodyMove<'a>> for Body<'a> {
fn from(value: BodyMove<'a>) -> Self { Self::Move(value) }
}

impl IntoLua<'_> for BodyMove<'static> {
impl IntoLua for BodyMove<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
lua.create_table_from([("items", self.items.into_owned())])?.into_lua(lua)
}
Expand All @@ -42,7 +42,7 @@ pub struct BodyMoveItem {
pub to: Url,
}

impl IntoLua<'_> for BodyMoveItem {
impl IntoLua for BodyMoveItem {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
lua
.create_table_from([
Expand Down
2 changes: 1 addition & 1 deletion yazi-dds/src/body/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'a> From<BodyRename<'a>> for Body<'a> {
fn from(value: BodyRename<'a>) -> Self { Self::Rename(value) }
}

impl IntoLua<'_> for BodyRename<'static> {
impl IntoLua for BodyRename<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
lua
.create_table_from([
Expand Down
2 changes: 1 addition & 1 deletion yazi-dds/src/body/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<'a> From<BodyTab> for Body<'a> {
fn from(value: BodyTab) -> Self { Self::Tab(value) }
}

impl IntoLua<'_> for BodyTab {
impl IntoLua for BodyTab {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
lua.create_table_from([("idx", self.idx)])?.into_lua(lua)
}
Expand Down
4 changes: 2 additions & 2 deletions yazi-dds/src/body/trash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ impl<'a> From<BodyTrash<'a>> for Body<'a> {
fn from(value: BodyTrash<'a>) -> Self { Self::Trash(value) }
}

impl IntoLua<'_> for BodyTrash<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value<'_>> {
impl IntoLua for BodyTrash<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
let urls = lua.create_table_with_capacity(self.urls.len(), 0)?;

// In most cases, `self.urls` will be `Cow::Owned`, so
Expand Down
8 changes: 4 additions & 4 deletions yazi-dds/src/body/yank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl<'a> From<BodyYank<'a>> for Body<'a> {
fn from(value: BodyYank<'a>) -> Self { Self::Yank(value) }
}

impl IntoLua<'_> for BodyYank<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value<'_>> {
impl IntoLua for BodyYank<'static> {
fn into_lua(self, lua: &Lua) -> mlua::Result<Value> {
if let Some(Cow::Owned(urls)) = Some(self.urls).filter(|_| !self.dummy) {
BodyYankIter { cut: self.cut, urls: urls.into_iter().collect() }.into_lua(lua)
} else {
Expand All @@ -49,11 +49,11 @@ pub struct BodyYankIter {
}

impl UserData for BodyYankIter {
fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) {
fn add_fields<F: mlua::UserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("cut", |_, me| Ok(me.cut));
}

fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
methods.add_meta_method(MetaMethod::Len, |_, me, ()| Ok(me.urls.len()));

methods.add_meta_method(MetaMethod::Index, |lua, me, idx: usize| {
Expand Down
14 changes: 5 additions & 9 deletions yazi-dds/src/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ use yazi_shared::{Id, RoCell, fs::Url};

use crate::{Client, ID, PEERS, body::{Body, BodyBulk, BodyCd, BodyDelete, BodyHi, BodyHover, BodyMove, BodyMoveItem, BodyRename, BodyTab, BodyTrash, BodyYank}};

pub static LOCAL: RoCell<RwLock<HashMap<String, HashMap<String, Function<'static>>>>> =
RoCell::new();
pub static LOCAL: RoCell<RwLock<HashMap<String, HashMap<String, Function>>>> = RoCell::new();

pub static REMOTE: RoCell<RwLock<HashMap<String, HashMap<String, Function<'static>>>>> =
RoCell::new();
pub static REMOTE: RoCell<RwLock<HashMap<String, HashMap<String, Function>>>> = RoCell::new();

macro_rules! sub {
($var:ident) => {
|plugin: &str, kind: &str, f: Function<'static>| {
|plugin: &str, kind: &str, f: Function| {
let mut var = $var.write();
let Some(map) = var.get_mut(kind) else {
var.insert(kind.to_owned(), HashMap::from_iter([(plugin.to_owned(), f)]));
Expand Down Expand Up @@ -51,11 +49,9 @@ macro_rules! unsub {
pub struct Pubsub;

impl Pubsub {
pub fn sub(plugin: &str, kind: &str, f: Function<'static>) -> bool {
sub!(LOCAL)(plugin, kind, f)
}
pub fn sub(plugin: &str, kind: &str, f: Function) -> bool { sub!(LOCAL)(plugin, kind, f) }

pub fn sub_remote(plugin: &str, kind: &str, f: Function<'static>) -> bool {
pub fn sub_remote(plugin: &str, kind: &str, f: Function) -> bool {
sub!(REMOTE)(plugin, kind, f) && Self::pub_from_hi()
}

Expand Down
2 changes: 2 additions & 0 deletions yazi-dds/src/sendable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Sendable {
}
}
Value::Error(_) => Err("error is not supported".into_lua_err())?,
Value::Other(..) => Err("unknown data is not supported".into_lua_err())?,
})
}

Expand Down Expand Up @@ -117,6 +118,7 @@ impl Sendable {
}
}
Value::Error(_) => Err("error is not supported".into_lua_err())?,
Value::Other(..) => Err("unknown data is not supported".into_lua_err())?,
})
}

Expand Down
4 changes: 2 additions & 2 deletions yazi-fm/src/app/commands/accept_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ impl App {
return;
};

_ = Lives::scope(&self.cx, |_| {
_ = Lives::scope(&self.cx, || {
let body = payload.body.into_lua(&LUA)?;
for f in map.values() {
if let Err(e) = f.call::<_, ()>(body.clone()) {
if let Err(e) = f.call::<()>(body.clone()) {
error!("Failed to call `{kind}` handler: {e}");
}
}
Expand Down
6 changes: 3 additions & 3 deletions yazi-fm/src/app/commands/mouse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crossterm::event::{MouseEvent, MouseEventKind};
use mlua::{Table, TableExt};
use mlua::{ObjectLike, Table};
use tracing::error;
use yazi_config::MANAGER;
use yazi_plugin::{LUA, bindings::Cast};
Expand All @@ -21,9 +21,9 @@ impl App {
let Some(size) = self.term.as_ref().and_then(|t| t.size().ok()) else { return };
let Ok(evt) = yazi_plugin::bindings::MouseEvent::cast(&LUA, event) else { return };

let res = Lives::scope(&self.cx, move |_| {
let res = Lives::scope(&self.cx, move || {
let area = yazi_plugin::elements::Rect::from(size);
let root = LUA.globals().raw_get::<_, Table>("Root")?.call_method::<_, Table>("new", area)?;
let root = LUA.globals().raw_get::<Table>("Root")?.call_method::<Table>("new", area)?;

if matches!(event.kind, MouseEventKind::Down(_) if MANAGER.mouse_events.draggable()) {
root.raw_set("_drag_start", evt.clone())?;
Expand Down
Loading

0 comments on commit 39bce0b

Please sign in to comment.