Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making default functions to closures #899

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thinkrapido
Copy link

I like to suggest a pull request to make the default functions like ModelFn, UpdateFn, etc... usable as a closure.

Related: #793

It is a very easy fix and we gain much more flexibility in developing complex programs.

A call would be via an macro

fn main() {
    nannou::sketch(view!(|app: &App, frame: Frame| {
        //...
    })).run()
}

and to respective functions.

Also read, how it is meant: https://medium.com/@romeo-disca/smart-pointers-in-rust-158046006f15

@thinkrapido thinkrapido force-pushed the feature/tr/functions-as-closures branch from bf3f0ac to 3d7f49e Compare November 28, 2022 13:22
@thinkrapido thinkrapido force-pushed the feature/tr/functions-as-closures branch from 3d7f49e to bc457e0 Compare November 28, 2022 13:25
@thinkrapido
Copy link
Author

all the examples need to be improved due to this pull request

@Woyten
Copy link
Contributor

Woyten commented Nov 30, 2022

Using closures instead of fns would make things easier, indeed. In my personal project, for example, I am using thread_local! to pass a pre-computed value to the fn. However, there are certain things in your PR I do not understand:

  • Why are the types Arc<dyn Fn(...)>? Shared Fn closures are a pretty rare thing in Rust. You probably want to use Box<dyn FnMut(...)>.
  • What is the reason to use macros? You could just use regular generics instead e.g. fn foo(foo_fn: impl FnMut()) { let boxed = Box::new(foo_fn) }. If you want some kind of name aliasing you could do the following:
trait FooFn {
    fn foo(&mut self);
}

impl<F: FnMut()> FooFn for F {
    fn foo(&mut self) {
        self()
    }
}

fn consumes_foo(foo_fn: impl FooFn) {
    let boxed: Box<dyn FooFn> = Box::new(foo_fn);
}

fn calls_foo() {
    consumes_foo(|| {})
}

@abey79
Copy link

abey79 commented Feb 21, 2023

@thinkrapido Did you make any progresses with this PR? I just ran into the issue of passing state from outside nannou::app(), which it would solve.

@thinkrapido
Copy link
Author

@abey79 , no I didn't. For me the pull request solved to problem.

@thinkrapido thinkrapido changed the title making default functions a closures making default functions to closures Feb 28, 2023
mokeko added a commit to mokeko/nannou that referenced this pull request Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants