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

@ -81,7 +81,7 @@ impl WinitUIWindow {
match window_attributes.fullscreen.clone() {
Some(Fullscreen::Exclusive(monitor, ref video_mode)) => {
let monitor = monitor.as_any().downcast_ref::<MonitorHandle>().unwrap();
let monitor = monitor.cast_ref::<MonitorHandle>().unwrap();
let screen = monitor.ui_screen(mtm);
if let Some(video_mode) =
monitor.video_modes_handles().find(|mode| &mode.mode == video_mode)
@ -91,7 +91,7 @@ impl WinitUIWindow {
this.setScreen(screen);
},
Some(Fullscreen::Borderless(Some(ref monitor))) => {
let monitor = monitor.as_any().downcast_ref::<MonitorHandle>().unwrap();
let monitor = monitor.cast_ref::<MonitorHandle>().unwrap();
let screen = monitor.ui_screen(mtm);
this.setScreen(screen);
},
@ -306,7 +306,7 @@ impl Inner {
let mtm = MainThreadMarker::new().unwrap();
let uiscreen = match &monitor {
Some(Fullscreen::Exclusive(monitor, video_mode)) => {
let monitor = monitor.as_any().downcast_ref::<MonitorHandle>().unwrap();
let monitor = monitor.cast_ref::<MonitorHandle>().unwrap();
let uiscreen = monitor.ui_screen(mtm);
if let Some(video_mode) =
monitor.video_modes_handles().find(|mode| &mode.mode == video_mode)
@ -316,7 +316,7 @@ impl Inner {
uiscreen.clone()
},
Some(Fullscreen::Borderless(Some(monitor))) => {
monitor.as_any().downcast_ref::<MonitorHandle>().unwrap().ui_screen(mtm).clone()
monitor.cast_ref::<MonitorHandle>().unwrap().ui_screen(mtm).clone()
},
Some(Fullscreen::Borderless(None)) => {
self.current_monitor_inner().ui_screen(mtm).clone()
@ -492,7 +492,7 @@ impl Window {
let screen = match fullscreen {
Some(Fullscreen::Exclusive(ref monitor, _))
| Some(Fullscreen::Borderless(Some(ref monitor))) => {
let monitor = monitor.as_any().downcast_ref::<MonitorHandle>().unwrap();
let monitor = monitor.cast_ref::<MonitorHandle>().unwrap();
monitor.ui_screen(mtm)
},
Some(Fullscreen::Borderless(None)) | None => &main_screen,