utils: add cast_* methods to allow more type-safe casting

Relying on just `as_any` was error prone and will become redundant in
the future, once upcasting will be stable, we also won't to impose a
restriction on to which concrete type we're casting, since casting
to a type that doesn't implement a base trait doesn't make much
sense.

Co-authored-by: Kirill Chibisov <contact@kchibisov.com>
This commit is contained in:
Mads Marquart 2025-03-11 14:35:25 +01:00 committed by GitHub
parent 5cada36ae8
commit 16d5f46db1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 158 additions and 136 deletions

View file

@ -23,7 +23,6 @@ use crate::event::{Ime, WindowEvent};
use crate::event_loop::AsyncRequestSerial;
use crate::monitor::{Fullscreen, MonitorHandle as CoreMonitorHandle};
use crate::platform_impl::wayland::output;
use crate::utils::AsAny;
use crate::window::{
Cursor, CursorGrabMode, ImePurpose, ResizeDirection, Theme, UserAttentionType,
Window as CoreWindow, WindowAttributes, WindowButtons, WindowId, WindowLevel,
@ -146,10 +145,7 @@ impl Window {
#[cfg_attr(not(x11_platform), allow(clippy::bind_instead_of_map))]
Some(Fullscreen::Borderless(monitor)) => {
let output = monitor.as_ref().and_then(|monitor| {
monitor
.as_any()
.downcast_ref::<output::MonitorHandle>()
.map(|handle| &handle.proxy)
monitor.cast_ref::<output::MonitorHandle>().map(|handle| &handle.proxy)
});
window.set_fullscreen(output)
@ -446,10 +442,7 @@ impl CoreWindow for Window {
#[cfg_attr(not(x11_platform), allow(clippy::bind_instead_of_map))]
Some(Fullscreen::Borderless(monitor)) => {
let output = monitor.as_ref().and_then(|monitor| {
monitor
.as_any()
.downcast_ref::<output::MonitorHandle>()
.map(|handle| &handle.proxy)
monitor.cast_ref::<output::MonitorHandle>().map(|handle| &handle.proxy)
});
self.window.set_fullscreen(output)

View file

@ -1067,13 +1067,11 @@ impl UnownedWindow {
let (monitor, video_mode): (Cow<'_, X11MonitorHandle>, Option<&VideoMode>) =
match &fullscreen {
Fullscreen::Exclusive(monitor, video_mode) => {
let monitor =
monitor.as_any().downcast_ref::<X11MonitorHandle>().unwrap();
let monitor = monitor.cast_ref::<X11MonitorHandle>().unwrap();
(Cow::Borrowed(monitor), Some(video_mode))
},
Fullscreen::Borderless(Some(monitor)) => {
let monitor =
monitor.as_any().downcast_ref::<X11MonitorHandle>().unwrap();
let monitor = monitor.cast_ref::<X11MonitorHandle>().unwrap();
(Cow::Borrowed(monitor), None)
},
Fullscreen::Borderless(None) => {