Variant of `Event::to_static` that takes self by reference
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.
And so if one needs to clone these enums for any reason one also ends up needing to throw away the
WindowEvent::ScaleFactorChangedevent.
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.
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).
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?
The current plan is to remove the lifetime entirely after #2900 is merged: https://github.com/rust-windowing/winit/pull/2973#issuecomment-1646776344.