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

LaunchBuilder::custom is not usable due to implicit Sized bound #2909

Closed
1 of 3 tasks
enbyted opened this issue Aug 31, 2024 · 0 comments · Fixed by #2920
Closed
1 of 3 tasks

LaunchBuilder::custom is not usable due to implicit Sized bound #2909

enbyted opened this issue Aug 31, 2024 · 0 comments · Fixed by #2920
Labels
bug Something isn't working

Comments

@enbyted
Copy link

enbyted commented Aug 31, 2024

Problem

When trying to use LaunchBuilder::custom the compiler complains with the following diagnostic:

error[E0277]: the size for values of type `dyn std::ops::Fn() -> Box<(dyn std::any::Any + 'static)> + Send + Sync` cannot be known at compilation time
  --> src/main.rs:49:27
   |
49 |     LaunchBuilder::custom(launch_server).with_cfg(config).launch(App);
   |     --------------------- ^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |     |
   |     required by a bound introduced by this call
   |
   = help: the trait `Sized` is not implemented for `dyn std::ops::Fn() -> Box<(dyn std::any::Any + 'static)> + Send + Sync`
note: required by an implicit `Sized` bound in `dioxus::prelude::LaunchBuilder::custom`
  --> /home/enbyted/.cargo/registry/src/index.crates.io-6f17d22bba15001f/dioxus-0.5.6/src/launch.rs:87:24
   |
87 |     pub fn custom<Cfg, List>(launch_fn: LaunchFn<Cfg, List>) -> LaunchBuilder<Cfg, List> {
   |                        ^^^^ required by the implicit `Sized` requirement on this type parameter in `LaunchBuilder::custom`

Steps To Reproduce

Try compiling the following code:

use dioxus::prelude::*;

#[cfg(feature = "server")]
fn launch_server(
    root: fn() -> Element,
    contexts: Vec<Box<dyn Fn() -> Box<dyn std::any::Any> + Send + Sync>>,
    platform_config: dioxus::fullstack::Config,
) {
  unimplemented!();
}

#[cfg(feature = "server")]
fn main_server() {
    use dioxus::fullstack::prelude::*;

    LaunchBuilder::custom(launch_server).launch(App);
}

fn App() -> Element {
    rsx! {
    }
}

Expected behavior

The code compiles correctly.

Environment:

  • Dioxus version: 0.5.6 (but issue is also present on master)
  • Rust version: 1.80.1
  • OS info: Ubuntu 22.04
  • App platform: fullstack

Analysis

The cause is simple, given definitions of custom():

    pub fn custom<Cfg, List>(launch_fn: LaunchFn<Cfg, List>) -> LaunchBuilder<Cfg, List> {
        LaunchBuilder {
            launch_fn,
            contexts: vec![],
            platform_config: None,
        }
    }

and the definition of LaunchFn<Cfg, List>:

pub type LaunchFn<Cfg, Context> = fn(fn() -> Element, Vec<Box<Context>>, Cfg);

It is clear that List type parameter has type dyn Fn /* ... */ which is not Sized. Likely the issue occured during modification of the LanuchFn type alias or misunderstanding of its type params during authoring of the custom function.

The fix is simple, add ?Sized bounds to custom() and rename the generic:

    pub fn custom<Cfg, Context: ?Sized>(launch_fn: LaunchFn<Cfg, Context>) -> LaunchBuilder<Cfg, Context> {
      /* ... */
    }

Questionnaire

  • I'm interested in fixing this myself but don't know where to start
  • I would like to fix and I have a solution
  • I don't have time to fix this right now, but maybe later
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants