No more cast_mut()
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user