Skip to content

Commit b478a94

Browse files
committed
Core: replace the use of WindowAdapterInternal::as_any with trait inheritance
This is now possible since Rust 1.86
1 parent d3b7bb6 commit b478a94

File tree

8 files changed

+8
-30
lines changed

8 files changed

+8
-30
lines changed

internal/backends/qt/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl QtWidgetAccessor for i_slint_core::api::Window {
337337
i_slint_core::window::WindowInner::from_pub(self)
338338
.window_adapter()
339339
.internal(i_slint_core::InternalToken)
340-
.and_then(|wa| wa.as_any().downcast_ref::<qt_window::QtWindow>())
340+
.and_then(|wa| (wa as &dyn core::any::Any).downcast_ref::<qt_window::QtWindow>())
341341
.map(qt_window::QtWindow::widget_ptr)
342342
}
343343
}

internal/backends/qt/qt_window.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,10 +2045,6 @@ impl WindowAdapterInternal for QtWindow {
20452045
}};
20462046
}
20472047

2048-
fn as_any(&self) -> &dyn std::any::Any {
2049-
self
2050-
}
2051-
20522048
fn handle_focus_change(&self, _old: Option<ItemRc>, new: Option<ItemRc>) {
20532049
let widget_ptr = self.widget_ptr();
20542050
if let Some(ai) = accessible_item(new) {
@@ -2368,7 +2364,7 @@ pub(crate) mod ffi {
23682364
) -> *mut c_void {
23692365
window_adapter
23702366
.internal(i_slint_core::InternalToken)
2371-
.and_then(|wa| <dyn std::any::Any>::downcast_ref(wa.as_any()))
2367+
.and_then(|wa| <dyn std::any::Any>::downcast_ref(wa))
23722368
.map_or(std::ptr::null_mut(), |win: &QtWindow| {
23732369
win.widget_ptr().cast::<c_void>().as_ptr()
23742370
})

internal/backends/testing/internal_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub fn access_testing_window<R>(
7676
i_slint_core::window::WindowInner::from_pub(window)
7777
.window_adapter()
7878
.internal(i_slint_core::InternalToken)
79-
.and_then(|wa| wa.as_any().downcast_ref::<TestingWindow>())
79+
.and_then(|wa| (wa as &dyn core::any::Any).downcast_ref::<TestingWindow>())
8080
.map(callback)
8181
.expect("access_testing_window called without testing backend/adapter")
8282
}

internal/backends/testing/testing_backend.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ pub struct TestingWindow {
111111
}
112112

113113
impl WindowAdapterInternal for TestingWindow {
114-
fn as_any(&self) -> &dyn std::any::Any {
115-
self
116-
}
117-
118114
fn input_method_request(&self, request: i_slint_core::window::InputMethodRequest) {
119115
self.ime_requests.borrow_mut().push(request)
120116
}

internal/backends/winit/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ impl WinitWindowAccessor for i_slint_core::api::Window {
968968
i_slint_core::window::WindowInner::from_pub(self)
969969
.window_adapter()
970970
.internal(i_slint_core::InternalToken)
971-
.and_then(|wa| wa.as_any().downcast_ref::<WinitWindowAdapter>())
971+
.and_then(|wa| (wa as &dyn core::any::Any).downcast_ref::<WinitWindowAdapter>())
972972
.is_some_and(|adapter| adapter.winit_window().is_some())
973973
}
974974

@@ -979,7 +979,7 @@ impl WinitWindowAccessor for i_slint_core::api::Window {
979979
i_slint_core::window::WindowInner::from_pub(self)
980980
.window_adapter()
981981
.internal(i_slint_core::InternalToken)
982-
.and_then(|wa| wa.as_any().downcast_ref::<WinitWindowAdapter>())
982+
.and_then(|wa| (wa as &dyn core::any::Any).downcast_ref::<WinitWindowAdapter>())
983983
.and_then(|adapter| adapter.winit_window().map(|w| callback(&w)))
984984
}
985985

@@ -990,7 +990,7 @@ impl WinitWindowAccessor for i_slint_core::api::Window {
990990
let adapter_weak = i_slint_core::window::WindowInner::from_pub(self)
991991
.window_adapter()
992992
.internal(i_slint_core::InternalToken)
993-
.and_then(|wa| wa.as_any().downcast_ref::<WinitWindowAdapter>())
993+
.and_then(|wa| (wa as &dyn core::any::Any).downcast_ref::<WinitWindowAdapter>())
994994
.map(|wa| wa.self_weak.clone())
995995
.ok_or_else(|| {
996996
PlatformError::OtherError(
@@ -1009,7 +1009,7 @@ impl WinitWindowAccessor for i_slint_core::api::Window {
10091009
if let Some(adapter) = i_slint_core::window::WindowInner::from_pub(self)
10101010
.window_adapter()
10111011
.internal(i_slint_core::InternalToken)
1012-
.and_then(|wa| wa.as_any().downcast_ref::<WinitWindowAdapter>())
1012+
.and_then(|wa| (wa as &dyn core::any::Any).downcast_ref::<WinitWindowAdapter>())
10131013
{
10141014
adapter
10151015
.window_event_filter

internal/backends/winit/winitwindowadapter.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,10 +1407,6 @@ impl WindowAdapterInternal for WinitWindowAdapter {
14071407
};
14081408
}
14091409

1410-
fn as_any(&self) -> &dyn std::any::Any {
1411-
self
1412-
}
1413-
14141410
fn color_scheme(&self) -> ColorScheme {
14151411
self.color_scheme
14161412
.get_or_init(|| {

internal/core/window.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub trait WindowAdapter {
165165
/// users to call or re-implement these functions.
166166
// TODO: add events for window receiving and loosing focus
167167
#[doc(hidden)]
168-
pub trait WindowAdapterInternal {
168+
pub trait WindowAdapterInternal: core::any::Any {
169169
/// This function is called by the generated code when a component and therefore its tree of items are created.
170170
fn register_item_tree(&self) {}
171171

@@ -195,12 +195,6 @@ pub trait WindowAdapterInternal {
195195
/// This method allow editable input field to communicate with the platform about input methods
196196
fn input_method_request(&self, _: InputMethodRequest) {}
197197

198-
/// Return self as any so the backend can upcast
199-
// TODO: consider using the as_any crate, or deriving the trait from Any to provide a better default
200-
fn as_any(&self) -> &dyn core::any::Any {
201-
&()
202-
}
203-
204198
/// Handle focus change
205199
// used for accessibility
206200
fn handle_focus_change(&self, _old: Option<ItemRc>, _new: Option<ItemRc>) {}

tools/docsnapper/headless.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ pub struct HeadlessWindow {
102102
}
103103

104104
impl WindowAdapterInternal for HeadlessWindow {
105-
fn as_any(&self) -> &dyn std::any::Any {
106-
self
107-
}
108-
109105
fn input_method_request(&self, request: i_slint_core::window::InputMethodRequest) {
110106
self.ime_requests.borrow_mut().push(request)
111107
}

0 commit comments

Comments
 (0)