Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Trying to make a nannou app from the template but with a customizable model() function. The template has this main(): fn main() { nannou::app(model).update(update).run(); } The model t...
#2: Post edited
Nannou model requires a function poniter: How to return a function pointer
- Nannou model requires a function pointer: How to return a function pointer
Trying to make a [nannou app](https://nannou.cc/) from the [template](https://github.com/nannou-org/nannou/blob/master/examples/templates/template_app.rs) but with a customizable `model()` function.- The template has this `main()`:
- fn main() {
- nannou::app(model).update(update).run();
- }
- The `model` that goes intot the `app()` is a function that returns a `Model`. My `Model` struct is very simple:
- struct Model {
- color: Hsl,
- max_time: u32,
- counter: u32,
- }
- The `model` function simply returns a `Model` struct. But instead of hard coding the parameters in a `model()` function I want a `model_function_creator()` that takes `max_time`and a `color_tuple`, *captures* them and returns a `model` function that is parameterized.
- I tried something like this:
- fn model_function_creator(max_time: u32, color_tuple: (f32, f32, f32)) -> fn(&App) -> Model {
- Box::new(move |_app: &App| Model {
- color: hsl(color.0, color.1, color.2),
- max_time: max_time,
- counter: 0,
- })
- }
- So that in `main()`, I can call the `model_function_creator` with custom parameters like this:
- fn main() {
- let max_time = 100;
- let color = (0.3, 0.5, 0.5);
- nannou::app(model_function_creator(max_time, color))
- .update(update)
- .simple_window(view)
- .run();
- }
- But I get the following error messages:
- expected `for<'r> fn(&'r nannou::App) -> Model` because of return type
- and
- expected fn pointer, found struct `Box`
- How do I return a `fn pointer` from a function and still have it *capture* the two parameter I pass to it.
- []()Trying to make a [nannou app](https://nannou.cc/) from the [template](https://github.com/nannou-org/nannou/blob/master/examples/templates/template_app.rs) but with a customizable `model()` function.
- The template has this `main()`:
- fn main() {
- nannou::app(model).update(update).run();
- }
- The `model` that goes intot the `app()` is a function that returns a `Model`. My `Model` struct is very simple:
- struct Model {
- color: Hsl,
- max_time: u32,
- counter: u32,
- }
- The `model` function simply returns a `Model` struct. But instead of hard coding the parameters in a `model()` function I want a `model_function_creator()` that takes `max_time`and a `color_tuple`, *captures* them and returns a `model` function that is parameterized.
- I tried something like this:
- fn model_function_creator(max_time: u32, color_tuple: (f32, f32, f32)) -> fn(&App) -> Model {
- Box::new(move |_app: &App| Model {
- color: hsl(color.0, color.1, color.2),
- max_time: max_time,
- counter: 0,
- })
- }
- So that in `main()`, I can call the `model_function_creator` with custom parameters like this:
- fn main() {
- let max_time = 100;
- let color = (0.3, 0.5, 0.5);
- nannou::app(model_function_creator(max_time, color))
- .update(update)
- .simple_window(view)
- .run();
- }
- But I get the following error messages:
- expected `for<'r> fn(&'r nannou::App) -> Model` because of return type
- and
- expected fn pointer, found struct `Box`
- How do I return a `fn pointer` from a function and still have it *capture* the two parameter I pass to it.
#1: Initial revision
Nannou model requires a function poniter: How to return a function pointer
Trying to make a [nannou app](https://nannou.cc/) from the [template](https://github.com/nannou-org/nannou/blob/master/examples/templates/template_app.rs) but with a customizable `model()` function. The template has this `main()`: fn main() { nannou::app(model).update(update).run(); } The `model` that goes intot the `app()` is a function that returns a `Model`. My `Model` struct is very simple: struct Model { color: Hsl, max_time: u32, counter: u32, } The `model` function simply returns a `Model` struct. But instead of hard coding the parameters in a `model()` function I want a `model_function_creator()` that takes `max_time`and a `color_tuple`, *captures* them and returns a `model` function that is parameterized. I tried something like this: fn model_function_creator(max_time: u32, color_tuple: (f32, f32, f32)) -> fn(&App) -> Model { Box::new(move |_app: &App| Model { color: hsl(color.0, color.1, color.2), max_time: max_time, counter: 0, }) } So that in `main()`, I can call the `model_function_creator` with custom parameters like this: fn main() { let max_time = 100; let color = (0.3, 0.5, 0.5); nannou::app(model_function_creator(max_time, color)) .update(update) .simple_window(view) .run(); } But I get the following error messages: expected `for<'r> fn(&'r nannou::App) -> Model` because of return type and expected fn pointer, found struct `Box` How do I return a `fn pointer` from a function and still have it *capture* the two parameter I pass to it.