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

Rationale for "while let" design #132

Closed
bvssvni opened this issue Apr 19, 2016 · 0 comments

Comments

@bvssvni
Copy link
Member

commented Apr 19, 2016

In the previous design, the emitted event was a PistonWindow object. This allowed for e in window { ... }. There were some drawbacks with the old design:

  • People were confused about the PistonWindow emitting events of its own type. They could not easily figure out how to use it and the idea behind the design was conceptually hard to grasp.
  • Because Rc<RefCell<T>> was used, one had to do .borrow() and .borrow_mut() in many places. This was inconvenient and sometimes lead to runtime errors that were hard to debug.

The new design uses "while let" instead of "for". This lets you borrow a mutable reference to the window only for the scope that pulls the event.

    let mut window: PistonWindow =
        WindowSettings::new("Hello World!", [512; 2])
            .build().unwrap();
    while let Some(e) = window.next() {
        // Pass the event to `.draw_2d` and `.draw_3d`.
        window.draw_2d(&e, |c, g| {
            ...
        }
    }

The event loop state is stored within the PistonWindow object, such that you can nest one event loop within another. This makes it easy to change scenes that has different event logic. For example, when going from a map to a level and back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.