Skip to content

Commit

Permalink
event: Unwrap user event in map_nonuser_event Err()
Browse files Browse the repository at this point in the history
Currently separating an Event<T> into a user event T and Event<()> is
not ergonomic: when map_nonuser_event returns Err() the caller "knows"
this can only contain UserEvent(T), yet has to write additional
unpacking logic to extract the event, with an unnecessary
unreachable!().

Solve this by returning T directly in the Err case, instead of
Event<'a, T>.
  • Loading branch information
MarijnS95 committed Jul 8, 2021
1 parent 5a65347 commit 769e8b6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- **Breaking:** On Web, remove the `stdweb` backend.
- Added `Window::focus_window`to bring the window to the front and set input focus.
- On Wayland and X11, implement `is_maximized` method on `Window`.
- **Breaking:** `map_nonuser_event` now returns the unwrapped user event `T` in `Err()`.

# 0.25.0 (2021-05-15)

Expand Down
4 changes: 2 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ impl<T: Clone> Clone for Event<'static, T> {
}

impl<'a, T> Event<'a, T> {
pub fn map_nonuser_event<U>(self) -> Result<Event<'a, U>, Event<'a, T>> {
pub fn map_nonuser_event<U>(self) -> Result<Event<'a, U>, T> {
use self::Event::*;
match self {
UserEvent(_) => Err(self),
UserEvent(user_event) => Err(user_event),
WindowEvent { window_id, event } => Ok(WindowEvent { window_id, event }),
DeviceEvent { device_id, event } => Ok(DeviceEvent { device_id, event }),
NewEvents(cause) => Ok(NewEvents(cause)),
Expand Down

0 comments on commit 769e8b6

Please sign in to comment.