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

Remove Clone requirement for slots in Vec #1564

Merged
merged 1 commit into from
Aug 23, 2023

Conversation

Senzaki
Copy link
Contributor

@Senzaki Senzaki commented Aug 21, 2023

This PR removes the requirement for S: Clone for when rendering a list of slots S.

It is currently required because the view! macro is building an array of S, then calling to_vec() (which obviously requires S: Clone). This can be a problem when the slot contains ChildrenFn (i.e. Box<dyn Fn(Scope) -> Fragment), as this type does not implement Clone.

This PR removes this requirement by using the vec! macro directly instead.

Take this code for example:

use leptos::{component, mount_to_body, slot, view, ChildrenFn, CollectView, IntoView, Scope};

#[slot]
pub struct MenuItems {
    pub menu_item: Vec<MenuItem>,
}

#[slot]
pub struct MenuItem {
    pub children: ChildrenFn,
}

#[component]
pub fn Menu(cx: Scope, menu_items: MenuItems) -> impl IntoView {
    view! {
        cx,
        <ul>
            {
                menu_items
                    .menu_item
                    .into_iter()
                    .map(|item| view! {
                        cx,
                        <li>{ (item.children)(cx) }</li>
                    })
                    .collect_view(cx)
            }
        </ul>
    }
}

fn main() {
    mount_to_body(|cx| {
        view! {
            cx,
            <Menu>
                <MenuItems slot>
                    <MenuItem slot>"hello"</MenuItem>
                    <MenuItem slot>"world"</MenuItem>
                </MenuItems>
            </Menu>
        }
    });
}

With the current version of leptos, it fails to compile with the following message:

error[E0277]: the trait bound `MenuItem: Clone` is not satisfied
note: required by a bound in `std::slice::<impl [T]>::to_vec`

With this PR, it build properly.

@Senzaki
Copy link
Contributor Author

Senzaki commented Aug 21, 2023

The CI fail does not seem to be a regression because of this commit, it might be an ICE because of a new version of rust nightly, as it also occurs on the main branch.

Running the following commands fails on both main and on my branch slot-vec-without-clone:

cd leptos_macro/example
cargo +nightly doc

It fails in the bumpalo dependency, which has not published a new version since May (so it mustn't be this crate's fault either?)

@Senzaki
Copy link
Contributor Author

Senzaki commented Aug 21, 2023

I have also confirmed it works with a previous version of rust nightly (cargo +nightly-2023-08-15 doc), so it is indeed a rust nightly ICE. Must be this issue.

@gbj
Copy link
Collaborator

gbj commented Aug 21, 2023

Thanks for the PR! I'll try to review it this week. Agreed that this is an ICE on most recent nightly, affecting several branches here. I can run CI locally if need be.

@gbj
Copy link
Collaborator

gbj commented Aug 23, 2023

This looks good to me. Thanks very much.

@gbj gbj merged commit 195b843 into leptos-rs:main Aug 23, 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.

2 participants