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

Dashboard
Notifications
Mark all as read
Q&A

Nannou model requires a function pointer: How to return a function pointer

+0
−1

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

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

0 answers

Sign up to answer this question »