yewstack / yew

Rust / Wasm framework for creating reliable and efficient web applications
https://yew.rs
Apache License 2.0
30.5k stars 1.42k forks source link

When passing `Classes` in `html!`, component props don't need `.clone()`, but element props do #3589

Open kawadakk opened 7 months ago

kawadakk commented 7 months ago

Problem You can write prop_name={&value} when passing a prop of type Classes to a component (because Classes implements ImplicitClone), but you can't when passing it to an element, requiring you to write prop_name={value.clone()} instead, which seems inconsistent.

Steps To Reproduce

#[derive(Properties, PartialEq)]
struct Props {
    class: Classes,
}

#[function_component]
fn Foo1(props: &Props) -> Html {
    html! {
        <a class={&props.class} />
    }
}

#[function_component]
fn Foo2(props: &Props) -> Html {
    html! {
        <Foo1 class={&props.class} />
    }
}

Expected behavior Successful compilation

Screenshots

error[E0277]: the trait bound `yew::Classes: std::convert::From<&yew::Classes>` is not satisfied
  --> .../foo.rs:69:19
   |
68 | /     html! {
69 | |         <a class={&props.class} />
   | |                   ^^^^^^^^^^^^ the trait `std::convert::From<&yew::Classes>` is not implemented for `yew::Classes`
70 | |     }
   | |_____- required by a bound introduced by this call
   |
   = note: required for `&yew::Classes` to implement `std::convert::Into<yew::Classes>`
help: consider dereferencing here
   |
69 |         <a class={*&props.class} />
   |                   +

Environment:

Questionnaire

kawadakk commented 7 months ago

This could be fixed by adding an impl From<&Classes> for Classes, but I'm curious why html! is using From instead of IntoPropValue (as in component props) in the first place.