Skip to content

Conversation

@kchibisov
Copy link
Member

@kchibisov kchibisov commented May 11, 2025

This helps with portability and defines some top-level structure around the event loop, so in the future, backends can get an idea of what API to use.

This also changes the API to be object safe by using dyn throughout.

--

I think that should help with core split.

@kchibisov kchibisov requested a review from madsmtm May 11, 2025 11:11
@kchibisov kchibisov force-pushed the kchibisov/core-event-loop branch 2 times, most recently from 8368762 to 09211f8 Compare May 11, 2025 11:15
@kchibisov kchibisov requested a review from daxpedda as a code owner May 11, 2025 11:15
@kchibisov kchibisov force-pushed the kchibisov/core-event-loop branch from 09211f8 to 0d0fd39 Compare May 11, 2025 12:11
Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is what I thought of in #4208 (comment).

@madsmtm madsmtm mentioned this pull request May 12, 2025
@madsmtm madsmtm added the S - api Design and usability label May 12, 2025
@madsmtm madsmtm mentioned this pull request May 12, 2025
5 tasks
This helps with portability and defines some top-level structure around
the event loop, so in the future, backends can get an idea of what API
to use.

This also changes the API to be object safe by using `dyn` throughout.
@kchibisov kchibisov requested a review from madsmtm May 13, 2025 00:03
@kchibisov kchibisov force-pushed the kchibisov/core-event-loop branch from 0d0fd39 to 8d39dbf Compare May 13, 2025 00:03
Comment on lines +198 to +201
- `run_app_on_demand`/`pump_app_events` now accept `&mut dyn ApplicationHandler` instead of generic.
- Moved common `EventLoop` methods like `run_app` into `EventLoopProvider` trait.
- Moved `event_loop::EventLoop` into `platform::event_loop::EventLoop` keeping the old re-export in place.
- `EventLoopProvider::run_app` now takes `Box<dyn ApplicationHandler` instead of owned generic.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Outdated

/// [`set_control_flow()`]: ActiveEventLoop::set_control_flow()
/// [`run_app()`]: Self::run_app()
#[inline]
#[cfg(not(all(web_platform, target_feature = "exception-handling")))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not that clean, but until we land #4165, I think you have to keep this cfg, otherwise it won't build?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, It'll fail to compile on a different level, thus I can not do much about it...

Maybe the run APIs on their own should be separated into their own Traits, so the impl will be present only when it's actually possible?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the run APIs on their own should be separated into their own Traits, so the impl will be present only when it's actually possible?

Possibly? I'm not sure at the moment.

Copy link
Member

@madsmtm madsmtm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I spent some more time thinking this over, and I no longer think a trait is warranted here.

Places where it could be useful:

  • If Winit wanted to do Box<dyn EventLoopProvider> - but we do not, the top-level crate is always going to know the set of backends that it supports (i.e. that's an enum).
  • If some downstream library wanted to do fn my_fn(event_loop: impl EventLoopProvider). The only functions that you can call here though are:
    • run_app, in which case my_fn should instead return a impl ApplicationHandler
    • create_proxy, in which case my_fn should just take EventLoopProxy.
    • listen_device_events, in which case you'd probably want impl ActiveEventLoop instead.
    • set_control_flow, in which case you'd probably want impl ActiveEventLoop instead.

So I think we should close this PR, and move forwards with #4208 without.

Apologies for making you submit this PR, and then ending up changing my mind!

@kchibisov
Copy link
Member Author

If Winit wanted to do Box - but we do not, the top-level crate is always going to know the set of backends that it supports (i.e. that's an enum).

While that is true, there's another side of the issue. Given that those core traits work both ways, the user of the API doesn't really need that in a form of trait, since as you've said, they can manage it themselves, however, backend authors may want to have some kind of API that describes how the backend is expected to operate from the user point of view.

Also, the user of the API wants to store EventLoop in some kind of struct and uses thirdparty event loops under certain conditions, then they'd need a trait of some sort. This mostly an issue with a non consuming API, such as pump_events. Though, consuming is also an issue if you create the event loop and pass further up.

@madsmtm
Copy link
Member

madsmtm commented May 16, 2025

backend authors may want to have some kind of API that describes how the backend is expected to operate from the user point of view.

Yeah, I can see that point, though that is kinda also solvable with better docs.

Also, the user of the API wants to store EventLoop in some kind of struct and uses thirdparty event loops under certain conditions, then they'd need a trait of some sort. This mostly an issue with a non consuming API, such as pump_events. Though, consuming is also an issue if you create the event loop and pass further up.

Which is why I think it's fine for us to sidestep the issue and wait with introducing a trait here.

@madsmtm madsmtm mentioned this pull request May 16, 2025
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S - api Design and usability

Development

Successfully merging this pull request may close these issues.

3 participants