Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lifetime from the Event #2981

Merged
merged 1 commit into from
Jul 30, 2023

Conversation

kchibisov
Copy link
Member

Lifetimes don't work nicely when dealing with multithreaded environments in the current design of the existing winit's event handling model, so remove it in favor of Weak fences passed to client, so they could try to update the size.

Fixes #1387.

  • Tested on all platforms changed
  • Added an entry to CHANGELOG.md if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality
  • Updated feature matrix, if new features were added or implemented

--

The changes are uncontroversial and basically removed one concept with another. If backend maintainers think that they can do better now due to event being without a lifetime they should do that in the follow up PR.

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.

I wouldn't say it's an "uncontroversial" change, see e.g. #939 where alternatives to a lifetime was discussed.

In any case, while I agree that the lifetime is not ideal from a user's perspective, it does act as sort of "speedbump" for handling events outside of the event loop, which, even though most events don't have a lifetime, is generally not a good idea.

I.e. I think we should consider this change in that light as well, e.g. it's just not just about "what can users now do more easily", but also "what pitfalls will they now fall into more easily".

Approved the iOS and macOS impls.

CHANGELOG.md Outdated Show resolved Hide resolved
src/event.rs Outdated Show resolved Hide resolved
@@ -46,7 +46,7 @@ fn main() -> Result<(), impl std::error::Error> {

println!("parent window: {parent_window:?})");

event_loop.run(move |event: Event<'_, ()>, event_loop, control_flow| {
event_loop.run(move |event: Event<()>, event_loop, control_flow| {
Copy link
Member

Choose a reason for hiding this comment

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

Do we have no examples that use ScaleFactorChanged?

That seems like an oversight, and makes it hard for us to judge how the API will be used by users.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess the main case is to write when changing to some size the user wants so you don't have a window jumping in sizes, but not sure that we really want to demonstrate it.

src/event.rs Outdated Show resolved Hide resolved
src/event.rs Outdated Show resolved Hide resolved
@madsmtm madsmtm mentioned this pull request Jul 28, 2023
17 tasks
@madsmtm madsmtm linked an issue Jul 28, 2023 that may be closed by this pull request
@kchibisov
Copy link
Member Author

I.e. I think we should consider this change in that light as well, e.g. it's just not just about "what can users now do more easily", but also "what pitfalls will they now fall into more easily".

In the current state of things this design is bad, because we tie event to the types it shouldn't be tied to. Yes this is sort of bad, but the lifetime is still there and we provided all the things to do event handling outside the event loop before with Event::to_static.

What I think we should change is to split the event callback handler into small ones, so we can have immediate actions encoded in a different way, like with the return type on the callback or some other handler structs. Events are just data in the end, they shouldn't be used to perform actions.

So the current design is bad, and we already have workaround for it, so it's just justifies its removal.

@madsmtm
Copy link
Member

madsmtm commented Jul 28, 2023

What I think we should change is to split the event callback handler into small ones

I fully agree, but we're not there right now, and it may take us a while to get there. So I fear that in the meantime, more people will be incentivized to handle events outside the callback, which will make it even harder for them to transition to a more fully callback-based model.

Anyhow, I am somewhat just playing the devil's advocate, I think the trade-off by accepting this PR is in general okay, I just want to highlight that it is indeed a trade-off.

@kchibisov
Copy link
Member Author

So I fear that in the meantime, more people will be incentivized to handle events outside the callback, which will make it even harder for them to transition to a more fully callback-based model.

They already do that with Event::to_static.

@kchibisov kchibisov requested a review from jackpot51 as a code owner July 28, 2023 22:01
@kchibisov kchibisov force-pushed the no-lifetime branch 4 times, most recently from 58107fd to 2e6e2da Compare July 28, 2023 22:31
src/error.rs Show resolved Hide resolved
src/event.rs Show resolved Hide resolved
Lifetimes don't work nicely when dealing with multithreaded environments
in the current design of the existing winit's event handling model, so
remove it in favor of `InnerSizeWriter` fences passed to client, so they
could try to update the size.

Fixes rust-windowing#1387.
@madsmtm
Copy link
Member

madsmtm commented Jul 30, 2023

I'll note down @Osspial's positive attitude towards the previous PR, that basically resolves my remaining concerns for why we shouldn't do this.

@kchibisov kchibisov merged commit 9ac3259 into rust-windowing:master Jul 30, 2023
@kchibisov kchibisov deleted the no-lifetime branch July 30, 2023 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Variant of Event::to_static that takes self by reference Event now takes a lifetime
2 participants