Skip to content

Extend call_with_explicit_closure to block that returns a closure #4026

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

Open
clouds56 opened this issue Apr 22, 2025 · 1 comment · May be fixed by #4031
Open

Extend call_with_explicit_closure to block that returns a closure #4026

clouds56 opened this issue Apr 22, 2025 · 1 comment · May be fixed by #4031
Labels
enhancement New feature or request rsx Related to rsx or the dioxus-rsx crate

Comments

@clouds56
Copy link
Contributor

Feature Request

Now this would compile

rsx! {
  input {
    oninput: move |e| value.set(e.value()) // it is using `oninput::call_with_explicit_closure(..)`
  }
}

while this would not

rsx! {
  input {
    oninput: {
      let prefix = prefix.clone();
      move |e| value.set(format!("{}{}", prefix, e.value()) // using `oninput(..)`
    }
  }
}

Implement Suggestion

Shall we extend call_with_explicit_closure to work with a block which last expr is an explicit closure?
Since there's no "clone" closure in rust, it seems to be very common that we'd like to clone something before move (like Rc, Arc, SmolStr, etc.). not everything here is signal that Copy.
Now I have to

let prefix2 = prefix.clone()
rsx! {
  input {
    oninput: 
      move |e| value.set(format!("{}{}", prefix, e.value())
  }
}

See also
https://users.rust-lang.org/t/clone-when-moving-into-a-closure/82680
https://internals.rust-lang.org/t/feature-request-add-clone-closure/15484/2

@clouds56 clouds56 added the enhancement New feature or request label Apr 22, 2025
@ealmloff ealmloff added the rsx Related to rsx or the dioxus-rsx crate label Apr 22, 2025
@ealmloff
Copy link
Member

This is a very common case and fairly easy to implement so I think this is worth adding. Once the expression gets complex enough, you will still need type annotations on the function argument for event handlers until rust's type inference improves:

fn main() {
    dioxus::launch(|| {
        let mut value = use_signal(String::new);
        let prefix = String::from("Hello, ");
        rsx! {
          input {
            oninput: 'a: {
              let prefix = prefix.clone();
              break 'a move |e: FormEvent| value.set(format!("{}{}", prefix, e.value()));

              todo!()
            }
          }
        }
    });
}

@clouds56 clouds56 linked a pull request Apr 23, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rsx Related to rsx or the dioxus-rsx crate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants