fix: explicit lifetimes for view closures

This commit is contained in:
Ashley Wulber 2025-02-21 14:29:12 -05:00
parent 343496841e
commit ab438b87f9
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
6 changed files with 127 additions and 93 deletions

View file

@ -86,8 +86,10 @@ pub enum Message {
pub struct Cosmic<App: Application> {
pub app: App,
#[cfg(feature = "wayland")]
pub surface_views:
HashMap<window::Id, Box<dyn Fn(&App) -> Element<'static, super::Message<App::Message>>>>,
pub surface_views: HashMap<
window::Id,
Box<dyn for<'a> Fn(&'a App) -> Element<'a, super::Message<App::Message>>>,
>,
}
impl<T: Application> Cosmic<T>
@ -138,7 +140,10 @@ where
if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn Fn(&T) -> Element<'static, super::Message<T::Message>>
dyn for<'a> Fn(
&'a T,
)
-> Element<'a, super::Message<T::Message>>
+ Send
+ Sync,
>>(
@ -170,7 +175,10 @@ where
if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn Fn(&T) -> Element<'static, super::Message<T::Message>>
dyn for<'a> Fn(
&'a T,
)
-> Element<'a, super::Message<T::Message>>
+ Send
+ Sync,
>>(
@ -840,7 +848,9 @@ impl<App: Application> Cosmic<App> {
pub fn get_subsurface(
&mut self,
settings: iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings,
view: Box<dyn Fn(&App) -> Element<'static, super::Message<App::Message>> + Send + Sync>,
view: Box<
dyn for<'a> Fn(&'a App) -> Element<'a, super::Message<App::Message>> + Send + Sync,
>,
) -> Task<super::Message<App::Message>> {
use iced_winit::commands::subsurface::get_subsurface;
@ -853,7 +863,9 @@ impl<App: Application> Cosmic<App> {
pub fn get_popup(
&mut self,
settings: iced_runtime::platform_specific::wayland::popup::SctkPopupSettings,
view: Box<dyn Fn(&App) -> Element<'static, super::Message<App::Message>> + Send + Sync>,
view: Box<
dyn for<'a> Fn(&'a App) -> Element<'a, super::Message<App::Message>> + Send + Sync,
>,
) -> Task<super::Message<App::Message>> {
use iced_winit::commands::popup::get_popup;

View file

@ -81,10 +81,12 @@ pub mod message {
+ Sync
+ 'static,
view: Option<
impl Fn(&App) -> crate::Element<'static, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
Box<
dyn for<'a> Fn(&'a App) -> crate::Element<'a, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
>,
>,
) -> App::Message
where
@ -104,13 +106,7 @@ pub mod message {
App::Message::from(SurfaceMessage::AppPopup(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
dyn Fn(&App) -> crate::Element<'static, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
> = Box::new(view);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
Arc::new(boxed)
}),
))
@ -118,7 +114,7 @@ pub mod message {
/// Used to create a popup message from within a widget.
#[cfg(feature = "wayland")]
pub fn simple_popup<Message>(
pub fn simple_popup<Message, V>(
settings: impl Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
@ -129,6 +125,7 @@ pub mod message {
) -> Message
where
Message: SurfaceMessageHandler + From<SurfaceMessage> + 'static,
V:,
{
use std::{any::Any, sync::Arc};
@ -173,19 +170,21 @@ pub mod message {
#[cfg(feature = "wayland")]
pub fn subsurface<App: super::Application>(
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static,
// XXX Boxed trait object is required for less cumbersome type inference, but we box it anyways.
view: Option<
impl Fn(&App) -> crate::Element<'static, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
Box<
dyn for<'a> Fn(&'a App) -> crate::Element<'a, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
>,
>,
) -> App::Message
where
App::Message: SurfaceMessageHandler + From<SurfaceMessage>,
{
use crate::surface_message::SurfaceMessage;
use std::{any::Any, sync::Arc};
use crate::surface_message::{SurfaceMessage, SurfaceMessageHandler};
let boxed: Box<
dyn Fn(
&mut App,
@ -200,13 +199,7 @@ pub mod message {
App::Message::from(SurfaceMessage::Subsurface(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
dyn Fn(&App) -> crate::Element<'static, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
> = Box::new(view);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
Arc::new(boxed)
}),
))