Setters for optional parameters now accept a generic Maybe<T> implementation instead of just T.
Added generic impl<T> Maybe<Option<T>> for T and impl<T> Maybe<T> for Option<T>.
This makes optional parameter handling really nice without any bloat to the API surface.
Taking an Option as a parameter:
#[component(
// Make `name` an optional parameter, defaults to `"Kobold"`
name?: "Kobold",
// Make `age` an optional parameter, use the `Default` value
age?,
)]
fn Greeter<'a>(name: &'a str, age: Option<u32>) -> impl View + 'a {
let age = age.map(|age| view!(", you are "{ age }" years old"));
view! {
<p> "Hello "{ name }{ age }
}
}
view! {
// Hello Kobold
<Greeter />
// Hello Alice
<Greeter name="Alice" />
// Hello Bob, you are 42 years old
<Greeter name="Bob" age={42} />
}
Using Option to set an optional parameter:
#[component(code?: 200)]
fn StatusCode(code: u32) -> impl View {
view! {
<p> "Status code was "{ code }
}
}
view! {
// Status code was 200
<StatusCode />
// Status code was 404
<StatusCode code={404} />
// Status code was 200
<StatusCode code={None} />
// Status code was 500
<StatusCode code={Some(500)} />
}
Maybe
trait into its own moduleMaybe<T>
implementation instead of justT
.impl<T> Maybe<Option<T>> for T
andimpl<T> Maybe<T> for Option<T>
.This makes optional parameter handling really nice without any bloat to the API surface.
Taking an
Option
as a parameter:Using
Option
to set an optional parameter: