Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

50%
+1 −1
Q&A Nannou model requires a function pointer: How to return a function pointer

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...

0 answers  ·  posted 2y ago by telefza‭  ·  edited 2y ago by Alexei‭

#2: Post edited by user avatar Alexei‭ · 2021-08-27T06:47:39Z (over 2 years ago)
added relevant tag + minor typo fix
  • 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 by user avatar telefza‭ · 2021-08-19T15:00:52Z (over 2 years ago)
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.