Skip to content

Instantly share code, notes, and snippets.

@heeen heeen/main.rs

Created Jan 17, 2020
Embed
What would you like to do?
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow},
};
use std::cell::RefCell;
pub struct ExampleBase {
pub window: winit::window::Window,
pub event_loop: RefCell<winit::event_loop::EventLoop<()>>,
pub test: i32,
}
impl ExampleBase {
pub fn render_loop<F: Fn()>(&self, f: F) {
// FIXME 1: cannot move out of dereference of `std::cell::RefMut<'_, winit::event_loop::EventLoop<()>>`
self.event_loop.borrow_mut().run(move |event, _, control_flow| {
//FIXME 2: the parameter type `F` may not live long enough
f();
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { event, .. } => {
match event {
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
_ => (),
}
}
_ => (),
}
});
}
pub fn new() -> Self {
let event_loop = winit::event_loop::EventLoop::new();
let window = winit::window::WindowBuilder::new()
.with_title("Example")
.with_inner_size(winit::dpi::LogicalSize::new( f64::from(800), f64::from(600),))
.build(&event_loop)
.unwrap();
let test = 42;
ExampleBase {
event_loop: RefCell::new(event_loop),
window,
test
}
}
}
fn main() {
let base = ExampleBase::new();
base.render_loop(|| {
println!("test: {:?}", base.test);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.