diff --git a/automation_cast/src/lib.rs b/automation_cast/src/lib.rs index 0ffdc6a..a242d43 100644 --- a/automation_cast/src/lib.rs +++ b/automation_cast/src/lib.rs @@ -6,7 +6,6 @@ use std::marker::Unsize; pub trait Cast { fn cast(&self) -> Option<&P>; - fn cast_mut(&mut self) -> Option<&mut P>; } impl Cast

for D @@ -16,10 +15,6 @@ where default fn cast(&self) -> Option<&P> { None } - - default fn cast_mut(&mut self) -> Option<&mut P> { - None - } } impl Cast

for D @@ -30,8 +25,4 @@ where fn cast(&self) -> Option<&P> { Some(self) } - - fn cast_mut(&mut self) -> Option<&mut P> { - Some(self) - } } diff --git a/google_home/google_home/src/device.rs b/google_home/google_home/src/device.rs index 03d0ae8..75027ea 100644 --- a/google_home/google_home/src/device.rs +++ b/google_home/google_home/src/device.rs @@ -56,7 +56,7 @@ pub trait Device: DeviceFulfillment { device } - async fn execute(&mut self, command: Command) -> Result<(), ErrorCode> { + async fn execute(&self, command: Command) -> Result<(), ErrorCode> { DeviceFulfillment::execute(self, command.clone()) .await .unwrap(); diff --git a/google_home/google_home/src/fulfillment.rs b/google_home/google_home/src/fulfillment.rs index f7f421a..e9746db 100644 --- a/google_home/google_home/src/fulfillment.rs +++ b/google_home/google_home/src/fulfillment.rs @@ -138,7 +138,7 @@ impl GoogleHome { let execution = command.execution.clone(); async move { if let Some(device) = devices.get(id.as_str()) - && let Some(device) = device.write().await.as_mut().cast_mut() + && let Some(device) = device.write().await.as_ref().cast() { if !device.is_online() { return (id, Ok(false)); diff --git a/google_home/google_home_macro/src/lib.rs b/google_home/google_home_macro/src/lib.rs index 67c11c6..d0da73a 100644 --- a/google_home/google_home_macro/src/lib.rs +++ b/google_home/google_home_macro/src/lib.rs @@ -501,7 +501,7 @@ pub fn traits(item: TokenStream) -> TokenStream { Some(quote! { Command::#command_name {#(#parameters,)*} => { - if let Some(t) = self.cast_mut() as Option<&mut dyn #ident> { + if let Some(t) = self.cast() as Option<&dyn #ident> { t.#f_name(#(#parameters,)*) #asyncness #errors; serde_json::to_value(t.get_state().await?)? } else { @@ -528,7 +528,7 @@ pub fn traits(item: TokenStream) -> TokenStream { pub trait #fulfillment: Sync + Send { async fn sync(&self) -> Result<(Vec, serde_json::Value), Box>; async fn query(&self) -> Result>; - async fn execute(&mut self, command: Command) -> Result>; + async fn execute(&self, command: Command) -> Result>; } #(#structs)* @@ -556,7 +556,7 @@ pub fn traits(item: TokenStream) -> TokenStream { Ok(state) } - async fn execute(&mut self, command: Command) -> Result> { + async fn execute(&self, command: Command) -> Result> { let value = match command { #(#execute)* }; diff --git a/src/device_manager.rs b/src/device_manager.rs index 5d3143e..603e6ee 100644 --- a/src/device_manager.rs +++ b/src/device_manager.rs @@ -45,10 +45,10 @@ impl mlua::UserData for WrappedDevice { }); methods.add_async_method("set_on", |_lua, this, on: bool| async move { - let mut device = this.0.write().await; - let device = device.as_mut(); + let device = this.0.write().await; + let device = device.as_ref(); - if let Some(device) = device.cast_mut() as Option<&mut dyn OnOff> { + if let Some(device) = device.cast() as Option<&dyn OnOff> { device.set_on(on).await.unwrap() }; @@ -127,8 +127,8 @@ impl DeviceManager { let iter = devices.iter().map(|(id, device)| { let message = message.clone(); async move { - let mut device = device.write().await; - let device: Option<&mut dyn OnMqtt> = device.as_mut().cast_mut(); + let device = device.write().await; + let device: Option<&dyn OnMqtt> = device.as_ref().cast(); if let Some(device) = device { // let subscribed = device // .topics() @@ -149,8 +149,8 @@ impl DeviceManager { Event::Darkness(dark) => { let devices = self.devices.read().await; let iter = devices.iter().map(|(id, device)| async move { - let mut device = device.write().await; - let device: Option<&mut dyn OnDarkness> = device.as_mut().cast_mut(); + let device = device.write().await; + let device: Option<&dyn OnDarkness> = device.as_ref().cast(); if let Some(device) = device { trace!(id, "Handling"); device.on_darkness(dark).await; @@ -163,8 +163,8 @@ impl DeviceManager { Event::Presence(presence) => { let devices = self.devices.read().await; let iter = devices.iter().map(|(id, device)| async move { - let mut device = device.write().await; - let device: Option<&mut dyn OnPresence> = device.as_mut().cast_mut(); + let device = device.write().await; + let device: Option<&dyn OnPresence> = device.as_ref().cast(); if let Some(device) = device { trace!(id, "Handling"); device.on_presence(presence).await; @@ -179,8 +179,8 @@ impl DeviceManager { let iter = devices.iter().map(|(id, device)| { let notification = notification.clone(); async move { - let mut device = device.write().await; - let device: Option<&mut dyn OnNotification> = device.as_mut().cast_mut(); + let device = device.write().await; + let device: Option<&dyn OnNotification> = device.as_ref().cast(); if let Some(device) = device { trace!(id, "Handling"); device.on_notification(notification).await; diff --git a/src/devices/audio_setup.rs b/src/devices/audio_setup.rs index 8a01359..6f3385d 100644 --- a/src/devices/audio_setup.rs +++ b/src/devices/audio_setup.rs @@ -81,11 +81,11 @@ impl OnMqtt for AudioSetup { } }; - let mut mixer = self.config.mixer.write().await; - let mut speakers = self.config.speakers.write().await; + let mixer = self.config.mixer.write().await; + let speakers = self.config.speakers.write().await; if let (Some(mixer), Some(speakers)) = ( - mixer.as_mut().cast_mut() as Option<&mut dyn OnOff>, - speakers.as_mut().cast_mut() as Option<&mut dyn OnOff>, + mixer.as_ref().cast() as Option<&dyn OnOff>, + speakers.as_ref().cast() as Option<&dyn OnOff>, ) { match action { RemoteAction::On => { @@ -116,12 +116,12 @@ impl OnMqtt for AudioSetup { #[async_trait] impl OnPresence for AudioSetup { async fn on_presence(&self, presence: bool) { - let mut mixer = self.config.mixer.write().await; - let mut speakers = self.config.speakers.write().await; + let mixer = self.config.mixer.write().await; + let speakers = self.config.speakers.write().await; if let (Some(mixer), Some(speakers)) = ( - mixer.as_mut().cast_mut() as Option<&mut dyn OnOff>, - speakers.as_mut().cast_mut() as Option<&mut dyn OnOff>, + mixer.as_ref().cast() as Option<&dyn OnOff>, + speakers.as_ref().cast() as Option<&dyn OnOff>, ) { // Turn off the audio setup when we leave the house if !presence { diff --git a/src/devices/contact_sensor.rs b/src/devices/contact_sensor.rs index 0daa13a..591b100 100644 --- a/src/devices/contact_sensor.rs +++ b/src/devices/contact_sensor.rs @@ -160,8 +160,8 @@ impl OnMqtt for ContactSensor { .iter() .zip(self.state_mut().await.previous.iter_mut()) { - let mut light = light.write().await; - if let Some(light) = light.as_mut().cast_mut() as Option<&mut dyn OnOff> { + let light = light.write().await; + if let Some(light) = light.as_ref().cast() as Option<&dyn OnOff> { *previous = light.on().await.unwrap(); light.set_on(true).await.ok(); } @@ -172,16 +172,15 @@ impl OnMqtt for ContactSensor { .iter() .zip(self.state_mut().await.previous.iter()) { - let mut light = light.write().await; + let light = light.write().await; if !previous { // If the timeout is zero just turn the light off directly if trigger.timeout.is_none() - && let Some(light) = light.as_mut().cast_mut() as Option<&mut dyn OnOff> + && let Some(light) = light.as_ref().cast() as Option<&dyn OnOff> { light.set_on(false).await.ok(); } else if let Some(timeout) = trigger.timeout - && let Some(light) = - light.as_mut().cast_mut() as Option<&mut dyn Timeout> + && let Some(light) = light.as_ref().cast() as Option<&dyn Timeout> { light.start_timeout(timeout).await.unwrap(); }