From f1eda67c1e40672080597db67e2f39aa1ab82cc2 Mon Sep 17 00:00:00 2001 From: Tristan Druyen Date: Fri, 31 Jan 2025 12:28:54 +0100 Subject: [PATCH] More redvault_el prototyping --- Cargo.lock | 15 ++++++++---- Cargo.toml | 2 +- redvault_el_rs/Cargo.toml | 2 ++ redvault_el_rs/README.md | 12 ++++++++-- redvault_el_rs/src/lib.rs | 50 ++++++++++++++++++++++++++++++++++----- 5 files changed, 68 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 59b898b..badbf39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -170,9 +170,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.89" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" dependencies = [ "backtrace", ] @@ -2374,6 +2374,10 @@ dependencies = [ "byteorder", ] +[[package]] +name = "garnix" +version = "0.1.1" + [[package]] name = "gdk" version = "0.18.0" @@ -6156,8 +6160,10 @@ dependencies = [ name = "redvault_el_rs" version = "0.1.1" dependencies = [ + "anyhow", "emacs", "emacs-rs-module", + "tokio", ] [[package]] @@ -8148,9 +8154,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.40.0" +version = "1.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" dependencies = [ "backtrace", "bytes", @@ -8161,6 +8167,7 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", + "tracing", "windows-sys 0.52.0", ] diff --git a/Cargo.toml b/Cargo.toml index 486cdd0..54e9c94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ lto = "fat" panic = "abort" [workspace] -members = ["llama_forge_rs", "leptos_stub", "frozen_llama", "llama_proxy_man", "redvault_el_rs"] +members = ["llama_forge_rs", "frozen_llama", "llama_proxy_man", "redvault_el_rs", "garnix"] resolver = "2" [workspace.package] diff --git a/redvault_el_rs/Cargo.toml b/redvault_el_rs/Cargo.toml index 10351a6..5e8432d 100644 --- a/redvault_el_rs/Cargo.toml +++ b/redvault_el_rs/Cargo.toml @@ -13,8 +13,10 @@ edition.workspace = true crate-type = ["cdylib"] [dependencies] +anyhow = "1.0.93" emacs = "0.19" emacs-rs-module = { version = "0.19.0" } +tokio = { version = "1.41.1", features = ["full", "tracing"] } [dev-dependencies] emacs-rs-module = { version = "0.19.0" } diff --git a/redvault_el_rs/README.md b/redvault_el_rs/README.md index 5d15b01..cc0af9c 100644 --- a/redvault_el_rs/README.md +++ b/redvault_el_rs/README.md @@ -3,12 +3,20 @@ ## Hot reload for dev ```elisp -(module-load "~/code/redvault-ai/target/release/deps/libemacs_rs_module-9ad53dadcc38727d.so") +(module-load "/home/tristand/code/redvault-ai/target/release/deps/libemacs_rs_module-9ad53dadcc38727d.so") (defun hotreload-el-rs () (interactive) - (rs-module/load "~/code/redvault-ai/target/debug/libredvault_el_rs.so") + (rs-module/load "/home/tristand/code/redvault-ai/target/release/libredvault_el_rs.so")) + + +(defun el-res-say-hello-2 () + (interactive) + (asd/say-hello "Asd")) + +(defun el-res-say-hello () + (interactive) (redvault-el-rs/say-hello "Asd")) ``` diff --git a/redvault_el_rs/src/lib.rs b/redvault_el_rs/src/lib.rs index b48aa2a..310a51b 100644 --- a/redvault_el_rs/src/lib.rs +++ b/redvault_el_rs/src/lib.rs @@ -1,11 +1,36 @@ use emacs::{defun, Env, IntoLisp, Result, Value}; +use std::sync::Once; +use std::sync::OnceLock; +use tokio::runtime::{Builder, Runtime}; // Emacs won't load the module without this. emacs::plugin_is_GPL_compatible!(); +// Global static runtime that's lazily initialized +static RUNTIME: OnceLock = OnceLock::new(); + +// Initialization function for the Emacs module +fn initialize_tokio_runtime() -> &'static Runtime { + RUNTIME.get_or_init(|| { + Builder::new_multi_thread() + .enable_all() + .worker_threads(4) // Adjust number of worker threads as needed + .build() + .expect("Failed to create Tokio runtime") + }) +} + // Register the initialization hook that Emacs will call when it loads the module. #[emacs::module(separator = "/")] fn init(env: &Env) -> Result> { + let runtime = initialize_tokio_runtime(); + + // Spawn a task without blocking + runtime.spawn(async { + // Your async code here + println!("Running an async task"); + }); + env.message("Test loading!")?; env.message("Done loading!") @@ -26,11 +51,24 @@ fn say_hello(env: &Env, name: String) -> Result> { // env.message(&format!("Helloo Broooooooo, {}!", name)) env.call( "message", - [format!("Henlo whatup, {}!", name).as_str().into_lisp(env)?], - ) + [format!("Henlo whatsup, {}!!!!", name) + .as_str() + .into_lisp(env)?], + )?; + RUNTIME + .get() + .ok_or_else(|| anyhow::anyhow!("No runtime"))? + .spawn(async { + for _i in 1..5 { + println!("A"); + std::thread::sleep(std::time::Duration::from_millis(1000)); + println!("B"); + } + }); + ().into_lisp(env) } -#[defun] -fn open_test_overlay(_env: &Env) -> Result> { - todo!() -} +// #[defun] +// fn open_test_overlay(_env: &Env) -> Result> { +// todo!() +// }