Added generic action callback

This commit is contained in:
Dreaded_X 2024-11-30 05:44:23 +01:00
parent 9719c46136
commit 157bbf923f
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
2 changed files with 40 additions and 0 deletions

39
src/action_callback.rs Normal file
View File

@ -0,0 +1,39 @@
use std::marker::PhantomData;
use mlua::{FromLua, IntoLua};
#[derive(Debug, Clone)]
pub struct ActionCallback<T> {
uuid: uuid::Uuid,
lua: mlua::Lua,
phantom: PhantomData<T>,
}
impl<T> FromLua for ActionCallback<T> {
fn from_lua(value: mlua::Value, lua: &mlua::Lua) -> mlua::Result<Self> {
let uuid = uuid::Uuid::new_v4();
lua.set_named_registry_value(&uuid.to_string(), value)?;
Ok(ActionCallback {
uuid,
lua: lua.clone(),
phantom: PhantomData::<T>,
})
}
}
// TODO: Return proper error here
impl<T> ActionCallback<T>
where
T: IntoLua + Sync + Send + Clone + Copy + 'static,
{
pub async fn call(&self, state: T) {
let uuid = self.uuid;
let callback: mlua::Value = self.lua.named_registry_value(&uuid.to_string()).unwrap();
match callback {
mlua::Value::Function(f) => f.call_async::<()>(state).await.unwrap(),
_ => todo!("Only functions are currently supported"),
}
}
}

View File

@ -2,6 +2,7 @@
#![feature(specialization)]
#![feature(let_chains)]
pub mod action_callback;
pub mod auth;
pub mod config;
pub mod device_manager;