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.

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

+1
−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.

History
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 »