refactor: Switch to async closures

This commit is contained in:
2025-09-01 03:18:56 +02:00
parent e21ea0f34e
commit 1b8566e593
8 changed files with 109 additions and 124 deletions

View File

@@ -73,22 +73,19 @@ impl DeviceManager {
match event {
Event::MqttMessage(message) => {
let devices = self.devices.read().await;
let iter = devices.iter().map(|(id, device)| {
let message = message.clone();
async move {
let device: Option<&dyn OnMqtt> = device.cast();
if let Some(device) = device {
// let subscribed = device
// .topics()
// .iter()
// .any(|topic| matches(&message.topic, topic));
//
// if subscribed {
trace!(id, "Handling");
device.on_mqtt(message).await;
trace!(id, "Done");
// }
}
let iter = devices.iter().map(async |(id, device)| {
let device: Option<&dyn OnMqtt> = device.cast();
if let Some(device) = device {
// let subscribed = device
// .topics()
// .iter()
// .any(|topic| matches(&message.topic, topic));
//
// if subscribed {
trace!(id, "Handling");
device.on_mqtt(message.clone()).await;
trace!(id, "Done");
// }
}
});
@@ -100,7 +97,7 @@ impl DeviceManager {
impl mlua::UserData for DeviceManager {
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
methods.add_async_method("add", |_lua, this, device: Box<dyn Device>| async move {
methods.add_async_method("add", async |_lua, this, device: Box<dyn Device>| {
this.add(device).await;
Ok(())
@@ -108,7 +105,7 @@ impl mlua::UserData for DeviceManager {
methods.add_async_method(
"schedule",
|lua, this, (schedule, f): (String, mlua::Function)| async move {
async |lua, this, (schedule, f): (String, mlua::Function)| {
debug!("schedule = {schedule}");
// This creates a function, that returns the actual job we want to run
let create_job = {

View File

@@ -29,7 +29,7 @@ impl mlua::UserData for Timeout {
methods.add_async_method(
"start",
|_lua, this, (timeout, callback): (f32, ActionCallback<mlua::Value, bool>)| async move {
async |_lua, this, (timeout, callback): (f32, ActionCallback<mlua::Value, bool>)| {
if let Some(handle) = this.state.write().await.handle.take() {
handle.abort();
}
@@ -50,7 +50,7 @@ impl mlua::UserData for Timeout {
},
);
methods.add_async_method("cancel", |_lua, this, ()| async move {
methods.add_async_method("cancel", async |_lua, this, ()| {
debug!("Canceling timeout callback");
if let Some(handle) = this.state.write().await.handle.take() {
@@ -60,7 +60,7 @@ impl mlua::UserData for Timeout {
Ok(())
});
methods.add_async_method("is_waiting", |_lua, this, ()| async move {
methods.add_async_method("is_waiting", async |_lua, this, ()| {
debug!("Canceling timeout callback");
if let Some(handle) = this.state.read().await.handle.as_ref() {

View File

@@ -7,13 +7,13 @@ pub trait OnOff {
where
Self: Sized + google_home::traits::OnOff + 'static,
{
methods.add_async_method("set_on", |_lua, this, on: bool| async move {
methods.add_async_method("set_on", async |_lua, this, on: bool| {
this.deref().set_on(on).await.unwrap();
Ok(())
});
methods.add_async_method("on", |_lua, this, ()| async move {
methods.add_async_method("on", async |_lua, this, ()| {
Ok(this.deref().on().await.unwrap())
});
}
@@ -25,13 +25,13 @@ pub trait Brightness {
where
Self: Sized + google_home::traits::Brightness + 'static,
{
methods.add_async_method("set_brightness", |_lua, this, brightness: u8| async move {
methods.add_async_method("set_brightness", async |_lua, this, brightness: u8| {
this.set_brightness(brightness).await.unwrap();
Ok(())
});
methods.add_async_method("brightness", |_lua, this, _: ()| async move {
methods.add_async_method("brightness", async |_lua, this, _: ()| {
Ok(this.brightness().await.unwrap())
});
}
@@ -45,7 +45,7 @@ pub trait ColorSetting {
{
methods.add_async_method(
"set_color_temperature",
|_lua, this, temperature: u32| async move {
async |_lua, this, temperature: u32| {
this.set_color(google_home::traits::Color { temperature })
.await
.unwrap();
@@ -54,7 +54,7 @@ pub trait ColorSetting {
},
);
methods.add_async_method("color_temperature", |_lua, this, ()| async move {
methods.add_async_method("color_temperature", async |_lua, this, ()| {
Ok(this.color().await.temperature)
});
}
@@ -66,16 +66,13 @@ pub trait OpenClose {
where
Self: Sized + google_home::traits::OpenClose + 'static,
{
methods.add_async_method(
"set_open_percent",
|_lua, this, open_percent: u8| async move {
this.set_open_percent(open_percent).await.unwrap();
methods.add_async_method("set_open_percent", async |_lua, this, open_percent: u8| {
this.set_open_percent(open_percent).await.unwrap();
Ok(())
},
);
Ok(())
});
methods.add_async_method("open_percent", |_lua, this, _: ()| async move {
methods.add_async_method("open_percent", async |_lua, this, _: ()| {
Ok(this.open_percent().await.unwrap())
});
}

View File

@@ -27,7 +27,7 @@ impl mlua::UserData for WrappedAsyncClient {
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
methods.add_async_method(
"send_message",
|_lua, this, (topic, message): (String, mlua::Value)| async move {
async |_lua, this, (topic, message): (String, mlua::Value)| {
let message = serde_json::to_string(&message).unwrap();
debug!("message = {message}");