No more cast_mut()
This commit is contained in:
parent
6c797820dc
commit
3fd8dddeb2
|
@ -6,7 +6,6 @@ use std::marker::Unsize;
|
|||
|
||||
pub trait Cast<P: ?Sized> {
|
||||
fn cast(&self) -> Option<&P>;
|
||||
fn cast_mut(&mut self) -> Option<&mut P>;
|
||||
}
|
||||
|
||||
impl<D, P> Cast<P> 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<D, P> Cast<P> for D
|
||||
|
@ -30,8 +25,4 @@ where
|
|||
fn cast(&self) -> Option<&P> {
|
||||
Some(self)
|
||||
}
|
||||
|
||||
fn cast_mut(&mut self) -> Option<&mut P> {
|
||||
Some(self)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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<Trait>, 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)*
|
||||
|
@ -556,7 +556,7 @@ pub fn traits(item: TokenStream) -> TokenStream {
|
|||
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 {
|
||||
#(#execute)*
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user