Skip to content

Commit bf3f0ac

Browse files
committed
making default functions a closure
1 parent 8744412 commit bf3f0ac

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

nannou/src/app.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,59 @@ use winit;
3131
use winit::event_loop::ControlFlow;
3232

3333
/// The user function type for initialising their model.
34-
pub type ModelFn<Model> = fn(&App) -> Model;
34+
pub type ModelFn<Model> = Arc<dyn Fn(&App) -> Model>;
35+
#[macro_export]
36+
macro_rules! model {
37+
($e:expr) => {
38+
{ Arc::new($e) }
39+
}
40+
}
41+
3542

3643
/// The user function type for updating their model in accordance with some event.
37-
pub type EventFn<Model, Event> = fn(&App, &mut Model, Event);
44+
pub type EventFn<Model, Event> = Arc<dyn Fn(&App, &mut Model, Event)>;
45+
#[macro_export]
46+
macro_rules! event {
47+
($e:expr) => {
48+
{ Arc::new($e) }
49+
}
50+
}
3851

3952
/// The user function type for updating the user model within the application loop.
40-
pub type UpdateFn<Model> = fn(&App, &mut Model, Update);
53+
pub type UpdateFn<Model> = Arc<dyn Fn(&App, &mut Model, Update)>;
54+
#[macro_export]
55+
macro_rules! update {
56+
($e:expr) => {
57+
{ Arc::new($e) }
58+
}
59+
}
4160

4261
/// The user function type for drawing their model to the surface of a single window.
43-
pub type ViewFn<Model> = fn(&App, &Model, Frame);
62+
pub type ViewFn<Model> = Arc<dyn Fn(&App, &Model, Frame)>;
63+
#[macro_export]
64+
macro_rules! view {
65+
($e:expr) => {
66+
{ Arc::new($e) }
67+
}
68+
}
4469

4570
/// A shorthand version of `ViewFn` for sketches where the user does not need a model.
46-
pub type SketchViewFn = fn(&App, Frame);
71+
pub type SketchViewFn = Arc<dyn Fn(&App, Frame)>;
72+
#[macro_export]
73+
macro_rules! sketch_view {
74+
($e:expr) => {
75+
{ Arc::new($e) }
76+
}
77+
}
4778

4879
/// The user function type allowing them to consume the `model` when the application exits.
49-
pub type ExitFn<Model> = fn(&App, Model);
80+
pub type ExitFn<Model> = Arc<dyn Fn(&App, Model)>;
81+
#[macro_export]
82+
macro_rules! exit {
83+
($e:expr) => {
84+
{ Arc::new($e) }
85+
}
86+
}
5087

5188
/// The **App**'s view function.
5289
enum View<Model = ()> {
@@ -548,7 +585,7 @@ impl Builder<(), Event> {
548585
/// This is useful for late night hack sessions where you just don't care about all that other
549586
/// stuff, you just want to play around with some ideas or make something pretty.
550587
pub fn sketch(view: SketchViewFn) -> SketchBuilder<Event> {
551-
let mut builder = Builder::new(default_model);
588+
let mut builder = Builder::new(model!(default_model));
552589
builder.default_view = Some(View::Sketch(view));
553590
builder.create_default_window = true;
554591
SketchBuilder { builder }
@@ -1028,7 +1065,7 @@ impl EventLoopWindowTarget {
10281065
// This method is solely used during `window::Builder::build` to allow for
10291066
pub(crate) fn as_ref(&self) -> &winit::event_loop::EventLoopWindowTarget<()> {
10301067
match *self {
1031-
EventLoopWindowTarget::Owned(ref event_loop) => (&**event_loop),
1068+
EventLoopWindowTarget::Owned(ref event_loop) => &**event_loop,
10321069
EventLoopWindowTarget::Pointer(ptr) => {
10331070
// This cast is safe, assuming that the `App`'s `EventLoopWindowTarget` will only
10341071
// ever be in the `Pointer` state while the pointer is valid - that is, during the
@@ -1100,9 +1137,6 @@ fn run_loop<M, E>(
11001137
if let Some(model) = model.as_mut() {
11011138
let loop_mode = app.loop_mode();
11021139
let now = Instant::now();
1103-
let mut do_update = |loop_state: &mut LoopState| {
1104-
apply_update(&mut app, model, event_fn, update_fn, loop_state, now);
1105-
};
11061140
match loop_mode {
11071141
LoopMode::NTimes { number_of_updates }
11081142
if loop_state.total_updates >= number_of_updates as u64 => {}
@@ -1114,7 +1148,7 @@ fn run_loop<M, E>(
11141148
// LoopMode::Wait { updates_before_waiting } =>
11151149
// if loop_state.updates_since_event > updates_before_waiting as u64 => {}
11161150
_ => {
1117-
do_update(&mut loop_state);
1151+
apply_update(&mut app, model, event_fn.clone(), update_fn.clone(), &mut loop_state, now);
11181152
},
11191153
}
11201154
}
@@ -1231,13 +1265,13 @@ fn run_loop<M, E>(
12311265
(*raw_view)(&app, &model, raw_frame);
12321266
}
12331267
None => match default_view {
1234-
Some(View::Sketch(view)) => {
1268+
Some(View::Sketch(ref view)) => {
12351269
let data = frame_data.as_ref().expect("missing `frame_data`");
12361270
let frame =
12371271
Frame::new_empty(raw_frame, &data.render, &data.capture);
12381272
view(&app, frame);
12391273
}
1240-
Some(View::WithModel(view)) => {
1274+
Some(View::WithModel(ref view)) => {
12411275
let data = frame_data.as_ref().expect("missing `frame_data`");
12421276
let frame =
12431277
Frame::new_empty(raw_frame, &data.render, &data.capture);
@@ -1330,7 +1364,7 @@ fn run_loop<M, E>(
13301364

13311365
// Process the event with the user's functions and see if we need to exit.
13321366
if let Some(model) = model.as_mut() {
1333-
exit |= process_and_emit_winit_event::<M, E>(&mut app, model, event_fn, &event);
1367+
exit |= process_and_emit_winit_event::<M, E>(&mut app, model, event_fn.clone(), &event);
13341368
}
13351369

13361370
// Set the control flow based on the loop mode.
@@ -1348,7 +1382,7 @@ fn run_loop<M, E>(
13481382
// If we need to exit, call the user's function and update control flow.
13491383
if exit {
13501384
if let Some(model) = model.take() {
1351-
if let Some(exit_fn) = exit_fn {
1385+
if let Some(exit_fn) = exit_fn.clone() {
13521386
exit_fn(&app, model);
13531387
}
13541388
}

nannou_egui_demo_app/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use nannou::prelude::*;
2+
use nannou::{model, update};
3+
use std::sync::Arc;
24
use nannou_egui::{egui_wgpu_backend::epi::App as EguiApp, Egui};
35

46
fn main() {
5-
nannou::app(model).update(update).run();
7+
nannou::app(model!(model)).update(update!(update)).run();
68
}
79

810
struct Model {

0 commit comments

Comments
 (0)