In rust I can put trait bounds on a template argument to guarantee it conforms to the functionality I want:
fn print<T:Debug>(t: T) {
println!("{:?}", t);
}
Can I do something similar with fields?
fn print_name<T:HasNameField>(t: T) {
println!("{:?}", t.name);
}
I'm not trying access a specific field (for which I could make some accessor trait). I am trying to promise a 3P function that I have certain fields, similar to how we promise functionality in a template w/Trait bounds.
My use case is that in yew I would like to create a form. Instead of just using <input type="text".../>
I would like users to be able to create their own input fields and be able to build CustomForm. And then I could have:
#[function_component(CustomForm)]
fn custom_form<T: yew::Component>() -> Html {
<form>
<T name="field name"/>
</form>
}
Currently this fails with the following message:
error[E0609]: no field `name` on type `<T as yew::Component>::Properties`