Last active
August 10, 2017 12:24
-
-
Save jsimmons/53cd7de23ba3746ce94f781df53e6884 to your computer and use it in GitHub Desktop.
The winit event loop API has weird consequences with borrow checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use winit; | |
struct Game { | |
window: winit::Window, | |
ev: winit::EventLoop | |
} | |
impl Game { | |
fn new() -> Game { | |
let ev = winit::EventsLoop::new(); | |
let window = winit::Window::new() | |
.build(&ev) | |
.unwrap(); | |
Game { | |
ev, | |
window | |
} | |
} | |
fn run<F: Fn()>(&mut self, f: F) { | |
self.ev.run_forever(|event| { | |
match event { | |
_ => {} | |
} | |
f(); | |
winit::ControlFlow::Continue | |
}); | |
} | |
} | |
fn main() { | |
let mut game = Game::new(); | |
// CAN'T USE MUT GAME HERE BECAUSE IT'S IMMUTABLY BORROWED IN THE ANON FN | |
// v | |
game.run(|| { | |
// BORROWS GAME IMMUTABLY | |
// use game.window.width or whatever | |
}); | |
// It's really weird because you can't just create the event loop | |
// object when you want to either, it has to be created _before_ you | |
// can create a window. | |
// lambdas, mutability and the borrow checker are spooky to untangle. | |
// p.s. trying to fix somebody else's code, this wouldn't be | |
// my first choice of design. (They changed the winit api, so updating some | |
// example code) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment