From 1db06d90067ad8ce1e6e5a808acddd16aac3adec Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Sun, 6 Apr 2025 17:35:16 +0300 Subject: use dyn trait upcasting --- src/shared_state.rs | 12 +++++++++--- src/wm_info_provider.rs | 5 +---- src/wm_info_provider/dummy.rs | 6 +----- src/wm_info_provider/hyprland.rs | 4 ---- src/wm_info_provider/niri.rs | 4 ---- src/wm_info_provider/river.rs | 4 ---- 6 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/shared_state.rs b/src/shared_state.rs index f2b8144..169107b 100644 --- a/src/shared_state.rs +++ b/src/shared_state.rs @@ -1,3 +1,5 @@ +use std::any::Any; + use crate::{ blocks_cache::BlocksCache, config::Config, @@ -16,15 +18,19 @@ pub struct SharedState { } impl SharedState { + fn downcast_provider(&mut self) -> Option<&mut T> { + ::downcast_mut(self.wm_info_provider.as_mut()) + } + pub fn get_river(&mut self) -> Option<&mut wm_info_provider::RiverInfoProvider> { - self.wm_info_provider.as_any().downcast_mut() + self.downcast_provider() } pub fn get_hyprland(&mut self) -> Option<&mut wm_info_provider::HyprlandInfoProvider> { - self.wm_info_provider.as_any().downcast_mut() + self.downcast_provider() } pub fn get_niri(&mut self) -> Option<&mut wm_info_provider::NiriInfoProvider> { - self.wm_info_provider.as_any().downcast_mut() + self.downcast_provider() } } diff --git a/src/wm_info_provider.rs b/src/wm_info_provider.rs index 0e3252c..4c5e904 100644 --- a/src/wm_info_provider.rs +++ b/src/wm_info_provider.rs @@ -21,7 +21,7 @@ pub use hyprland::*; mod niri; pub use niri::*; -pub trait WmInfoProvider { +pub trait WmInfoProvider: Any { fn register(&self, _: &mut EventLoop) {} fn new_ouput(&mut self, _: &mut Connection, _: &Output) {} @@ -46,9 +46,6 @@ pub trait WmInfoProvider { _btn: PointerBtn, ) { } - - // TODO: remove once RFC3324 (dyn upcasting coercion) is stabilized (hopefully in Rust 1.86.0) - fn as_any(&mut self) -> &mut dyn Any; } pub fn bind(conn: &mut Connection, config: &WmConfig) -> Box { diff --git a/src/wm_info_provider/dummy.rs b/src/wm_info_provider/dummy.rs index 72903c7..9b1ad9a 100644 --- a/src/wm_info_provider/dummy.rs +++ b/src/wm_info_provider/dummy.rs @@ -2,8 +2,4 @@ use super::*; pub struct DummyInfoProvider; -impl WmInfoProvider for DummyInfoProvider { - fn as_any(&mut self) -> &mut dyn Any { - self - } -} +impl WmInfoProvider for DummyInfoProvider {} diff --git a/src/wm_info_provider/hyprland.rs b/src/wm_info_provider/hyprland.rs index 6deeec1..52fd4eb 100644 --- a/src/wm_info_provider/hyprland.rs +++ b/src/wm_info_provider/hyprland.rs @@ -104,10 +104,6 @@ impl WmInfoProvider for HyprlandInfoProvider { _ => (), } } - - fn as_any(&mut self) -> &mut dyn Any { - self - } } fn hyprland_cb(conn: &mut Connection, state: &mut State) -> io::Result<()> { diff --git a/src/wm_info_provider/niri.rs b/src/wm_info_provider/niri.rs index 9f37c3b..48addeb 100644 --- a/src/wm_info_provider/niri.rs +++ b/src/wm_info_provider/niri.rs @@ -114,10 +114,6 @@ impl WmInfoProvider for NiriInfoProvider { _ => (), } } - - fn as_any(&mut self) -> &mut dyn Any { - self - } } fn niri_cb(conn: &mut Connection, state: &mut State) -> io::Result<()> { diff --git a/src/wm_info_provider/river.rs b/src/wm_info_provider/river.rs index 9af3385..c0321ee 100644 --- a/src/wm_info_provider/river.rs +++ b/src/wm_info_provider/river.rs @@ -144,10 +144,6 @@ impl WmInfoProvider for RiverInfoProvider { _ => (), } } - - fn as_any(&mut self) -> &mut dyn Any { - self - } } fn output_status_cb(ctx: EventCtx) { -- cgit v1.3.1