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

Variant of Event::to_static that takes self by reference #1968

Closed
birktj opened this issue Jun 21, 2021 · 4 comments · Fixed by #2981
Closed

Variant of Event::to_static that takes self by reference #1968

birktj opened this issue Jun 21, 2021 · 4 comments · Fixed by #2981
Labels
C - needs discussion Direction must be ironed out S - enhancement Wouldn't this be the coolest?

Comments

@birktj
Copy link

birktj commented Jun 21, 2021

Currently Event::to_static and WindowEvent::to_static takes self by value. This is inconvenient because static variants of these enums are required to clone them. And so if one needs to clone these enums for any reason one also ends up needing to throw away the WindowEvent::ScaleFactorChanged event.

I propose instead to either change to_static to take a &self or alternatively add an additional method to_static_cloned.

@maroider
Copy link
Member

maroider commented Jun 21, 2021

And so if one needs to clone these enums for any reason one also ends up needing to throw away the WindowEvent::ScaleFactorChanged event.

This is done by necessity, as you can't clone a mutable reference or any type which contains one. Nor can you turn any old mutable reference into one with the 'static lifetime willy-nilly. At best, you'd have to do a Box::leak every time WindowEvent::to_static is called on WindowEvent::ScaleFactorChanged.

Previous efforts and discussions aimed at making things more ergonomic can be found in #1387 and #1456.

@birktj
Copy link
Author

birktj commented Jun 22, 2021

If my understanding is correct Event::to_static will return None if the event contains a WindowEvent::ScaleFactorChanged event. This is of course perfectly understandable, however I see no reason for why this method could not take a &self instead of a self. The advantage being that one can still keep the original event with a reference around and at the same time create a copy with a 'static lifetime (which will of course be None if the event is WindowEvent::ScaleFactorChanged).

@maroider maroider added C - needs discussion Direction must be ironed out S - enhancement Wouldn't this be the coolest? labels Jul 12, 2021
@madsmtm madsmtm mentioned this issue Aug 24, 2021
8 tasks
@philpax
Copy link
Contributor

philpax commented Jul 6, 2022

Just confirming that this is an issue for me as well - I'm working with an existing codebase that converts the event to static and propagates that around, which is fine, except in the case where it can't be converted to static. Because to_static consumes the event, I have no way of handling the event afterward. That is to say:

        if let Some(event) = event.to_static() {
            app.handle_event(&event, control_flow);
        } else {
            // the event is dead here, there's nothing I can meaningfully do
        }

May I suggest renaming the current function to into_static (to be consistent with https://rust-lang.github.io/rust-clippy/master/#wrong_self_convention) and adding a try_into_static(self) -> Result<Event<'static, T>, Self> that returns the event unchanged if it's not convertible?

@daxpedda
Copy link
Member

The current plan is to remove the lifetime entirely after #2900 is merged: #2973 (comment).

@madsmtm madsmtm linked a pull request Jul 28, 2023 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C - needs discussion Direction must be ironed out S - enhancement Wouldn't this be the coolest?
4 participants