1
0
Fork 0

Fix(lua_macros): Add sender trough app_data

This commit is contained in:
Benedikt Peetz 2023-07-18 08:03:16 +02:00
parent c3a2b2d566
commit 6412650686
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
2 changed files with 9 additions and 10 deletions

View File

@ -88,7 +88,7 @@ fn append_tx_send_code(input: &mut syn::ItemFn) -> &mut syn::ItemFn {
.expect("This is valid."); .expect("This is valid.");
return_type_as_return_type return_type_as_return_type
} else { } else {
// There is only rlua // There is only mlua::Error left
ReturnType::Default ReturnType::Default
} }
} }
@ -108,23 +108,22 @@ fn append_tx_send_code(input: &mut syn::ItemFn) -> &mut syn::ItemFn {
ReturnType::Type(_, _) => { ReturnType::Type(_, _) => {
quote! { quote! {
{ {
Event::CommandEvent(Command::#function_name_pascal(input_str)) Event::CommandEvent(Command::#function_name_pascal(input_str.clone()))
} }
} }
} }
}; };
quote! { quote! {
{ {
let tx: std::sync::Arc<std::sync::Mutex<tokio::sync::mpsc::Sender<crate::app::events::event_types::Event>>> = let tx:
core::cell::Ref<
tokio::sync::mpsc::Sender<crate::app::events::event_types::Event>
> =
lua lua
.named_registry_value("sender_for_ci_commands") .app_data_ref()
.expect("This exists, it was set before"); .expect("This exists, it was set before");
tx (*tx)
// FIXME: This is sync code, it needs to be run in a blocking allowed
// executor
.lock()
.expect("This should work, as only one function is executed. It wil however fail, when concurrency is added");
.send(#send_data) .send(#send_data)
.await .await
.expect("This should work, as the reciever is not dropped"); .expect("This should work, as the reciever is not dropped");

View File

@ -44,7 +44,7 @@ pub fn generate_generate_ci_function(input: &syn::DeriveInput) -> TokenStream2 {
lua: &mut mlua::Lua, lua: &mut mlua::Lua,
tx: tokio::sync::mpsc::Sender<crate::app::events::event_types::Event>) tx: tokio::sync::mpsc::Sender<crate::app::events::event_types::Event>)
{ {
lua.set_named_registry_value("sender_for_ci_commands", std::sync::Arc::new(std::sync::Mutex::new(tx))).expect("This should always work, as the value is added before all else"); lua.set_app_data(tx);
let globals = lua.globals(); let globals = lua.globals();
#input_tokens #input_tokens
} }