No more cast_mut()

This commit is contained in:
Dreaded_X 2024-07-26 00:37:53 +02:00
parent 6c797820dc
commit 3fd8dddeb2
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
7 changed files with 29 additions and 39 deletions

View File

@ -6,7 +6,6 @@ use std::marker::Unsize;
pub trait Cast<P: ?Sized> { pub trait Cast<P: ?Sized> {
fn cast(&self) -> Option<&P>; fn cast(&self) -> Option<&P>;
fn cast_mut(&mut self) -> Option<&mut P>;
} }
impl<D, P> Cast<P> for D impl<D, P> Cast<P> for D
@ -16,10 +15,6 @@ where
default fn cast(&self) -> Option<&P> { default fn cast(&self) -> Option<&P> {
None None
} }
default fn cast_mut(&mut self) -> Option<&mut P> {
None
}
} }
impl<D, P> Cast<P> for D impl<D, P> Cast<P> for D
@ -30,8 +25,4 @@ where
fn cast(&self) -> Option<&P> { fn cast(&self) -> Option<&P> {
Some(self) Some(self)
} }
fn cast_mut(&mut self) -> Option<&mut P> {
Some(self)
}
} }

View File

@ -56,7 +56,7 @@ pub trait Device: DeviceFulfillment {
device device
} }
async fn execute(&mut self, command: Command) -> Result<(), ErrorCode> { async fn execute(&self, command: Command) -> Result<(), ErrorCode> {
DeviceFulfillment::execute(self, command.clone()) DeviceFulfillment::execute(self, command.clone())
.await .await
.unwrap(); .unwrap();

View File

@ -138,7 +138,7 @@ impl GoogleHome {
let execution = command.execution.clone(); let execution = command.execution.clone();
async move { async move {
if let Some(device) = devices.get(id.as_str()) 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() { if !device.is_online() {
return (id, Ok(false)); return (id, Ok(false));

View File

@ -501,7 +501,7 @@ pub fn traits(item: TokenStream) -> TokenStream {
Some(quote! { Some(quote! {
Command::#command_name {#(#parameters,)*} => { 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; t.#f_name(#(#parameters,)*) #asyncness #errors;
serde_json::to_value(t.get_state().await?)? serde_json::to_value(t.get_state().await?)?
} else { } else {
@ -528,7 +528,7 @@ pub fn traits(item: TokenStream) -> TokenStream {
pub trait #fulfillment: Sync + Send { pub trait #fulfillment: Sync + Send {
async fn sync(&self) -> Result<(Vec<Trait>, serde_json::Value), Box<dyn ::std::error::Error>>; async fn sync(&self) -> Result<(Vec<Trait>, serde_json::Value), Box<dyn ::std::error::Error>>;
async fn query(&self) -> Result<serde_json::Value, Box<dyn ::std::error::Error>>; async fn query(&self) -> Result<serde_json::Value, Box<dyn ::std::error::Error>>;
async fn execute(&mut self, command: Command) -> Result<serde_json::Value, Box<dyn std::error::Error>>; async fn execute(&self, command: Command) -> Result<serde_json::Value, Box<dyn std::error::Error>>;
} }
#(#structs)* #(#structs)*
@ -556,7 +556,7 @@ pub fn traits(item: TokenStream) -> TokenStream {
Ok(state) Ok(state)
} }
async fn execute(&mut self, command: Command) -> Result<serde_json::Value, Box<dyn std::error::Error>> { async fn execute(&self, command: Command) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
let value = match command { let value = match command {
#(#execute)* #(#execute)*
}; };

View File

@ -45,10 +45,10 @@ impl mlua::UserData for WrappedDevice {
}); });
methods.add_async_method("set_on", |_lua, this, on: bool| async move { methods.add_async_method("set_on", |_lua, this, on: bool| async move {
let mut device = this.0.write().await; let device = this.0.write().await;
let device = device.as_mut(); 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() device.set_on(on).await.unwrap()
}; };
@ -127,8 +127,8 @@ impl DeviceManager {
let iter = devices.iter().map(|(id, device)| { let iter = devices.iter().map(|(id, device)| {
let message = message.clone(); let message = message.clone();
async move { async move {
let mut device = device.write().await; let device = device.write().await;
let device: Option<&mut dyn OnMqtt> = device.as_mut().cast_mut(); let device: Option<&dyn OnMqtt> = device.as_ref().cast();
if let Some(device) = device { if let Some(device) = device {
// let subscribed = device // let subscribed = device
// .topics() // .topics()
@ -149,8 +149,8 @@ impl DeviceManager {
Event::Darkness(dark) => { Event::Darkness(dark) => {
let devices = self.devices.read().await; let devices = self.devices.read().await;
let iter = devices.iter().map(|(id, device)| async move { let iter = devices.iter().map(|(id, device)| async move {
let mut device = device.write().await; let device = device.write().await;
let device: Option<&mut dyn OnDarkness> = device.as_mut().cast_mut(); let device: Option<&dyn OnDarkness> = device.as_ref().cast();
if let Some(device) = device { if let Some(device) = device {
trace!(id, "Handling"); trace!(id, "Handling");
device.on_darkness(dark).await; device.on_darkness(dark).await;
@ -163,8 +163,8 @@ impl DeviceManager {
Event::Presence(presence) => { Event::Presence(presence) => {
let devices = self.devices.read().await; let devices = self.devices.read().await;
let iter = devices.iter().map(|(id, device)| async move { let iter = devices.iter().map(|(id, device)| async move {
let mut device = device.write().await; let device = device.write().await;
let device: Option<&mut dyn OnPresence> = device.as_mut().cast_mut(); let device: Option<&dyn OnPresence> = device.as_ref().cast();
if let Some(device) = device { if let Some(device) = device {
trace!(id, "Handling"); trace!(id, "Handling");
device.on_presence(presence).await; device.on_presence(presence).await;
@ -179,8 +179,8 @@ impl DeviceManager {
let iter = devices.iter().map(|(id, device)| { let iter = devices.iter().map(|(id, device)| {
let notification = notification.clone(); let notification = notification.clone();
async move { async move {
let mut device = device.write().await; let device = device.write().await;
let device: Option<&mut dyn OnNotification> = device.as_mut().cast_mut(); let device: Option<&dyn OnNotification> = device.as_ref().cast();
if let Some(device) = device { if let Some(device) = device {
trace!(id, "Handling"); trace!(id, "Handling");
device.on_notification(notification).await; device.on_notification(notification).await;

View File

@ -81,11 +81,11 @@ impl OnMqtt for AudioSetup {
} }
}; };
let mut mixer = self.config.mixer.write().await; let mixer = self.config.mixer.write().await;
let mut speakers = self.config.speakers.write().await; let speakers = self.config.speakers.write().await;
if let (Some(mixer), Some(speakers)) = ( if let (Some(mixer), Some(speakers)) = (
mixer.as_mut().cast_mut() as Option<&mut dyn OnOff>, mixer.as_ref().cast() as Option<&dyn OnOff>,
speakers.as_mut().cast_mut() as Option<&mut dyn OnOff>, speakers.as_ref().cast() as Option<&dyn OnOff>,
) { ) {
match action { match action {
RemoteAction::On => { RemoteAction::On => {
@ -116,12 +116,12 @@ impl OnMqtt for AudioSetup {
#[async_trait] #[async_trait]
impl OnPresence for AudioSetup { impl OnPresence for AudioSetup {
async fn on_presence(&self, presence: bool) { async fn on_presence(&self, presence: bool) {
let mut mixer = self.config.mixer.write().await; let mixer = self.config.mixer.write().await;
let mut speakers = self.config.speakers.write().await; let speakers = self.config.speakers.write().await;
if let (Some(mixer), Some(speakers)) = ( if let (Some(mixer), Some(speakers)) = (
mixer.as_mut().cast_mut() as Option<&mut dyn OnOff>, mixer.as_ref().cast() as Option<&dyn OnOff>,
speakers.as_mut().cast_mut() as Option<&mut dyn OnOff>, speakers.as_ref().cast() as Option<&dyn OnOff>,
) { ) {
// Turn off the audio setup when we leave the house // Turn off the audio setup when we leave the house
if !presence { if !presence {

View File

@ -160,8 +160,8 @@ impl OnMqtt for ContactSensor {
.iter() .iter()
.zip(self.state_mut().await.previous.iter_mut()) .zip(self.state_mut().await.previous.iter_mut())
{ {
let mut light = light.write().await; let light = light.write().await;
if let Some(light) = light.as_mut().cast_mut() as Option<&mut dyn OnOff> { if let Some(light) = light.as_ref().cast() as Option<&dyn OnOff> {
*previous = light.on().await.unwrap(); *previous = light.on().await.unwrap();
light.set_on(true).await.ok(); light.set_on(true).await.ok();
} }
@ -172,16 +172,15 @@ impl OnMqtt for ContactSensor {
.iter() .iter()
.zip(self.state_mut().await.previous.iter()) .zip(self.state_mut().await.previous.iter())
{ {
let mut light = light.write().await; let light = light.write().await;
if !previous { if !previous {
// If the timeout is zero just turn the light off directly // If the timeout is zero just turn the light off directly
if trigger.timeout.is_none() 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(); light.set_on(false).await.ok();
} else if let Some(timeout) = trigger.timeout } else if let Some(timeout) = trigger.timeout
&& let Some(light) = && let Some(light) = light.as_ref().cast() as Option<&dyn Timeout>
light.as_mut().cast_mut() as Option<&mut dyn Timeout>
{ {
light.start_timeout(timeout).await.unwrap(); light.start_timeout(timeout).await.unwrap();
} }