-
-
Notifications
You must be signed in to change notification settings - Fork 932
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
SuperFrom for more optional prop types including custom types #3476
Comments
You can do this today with a custom SuperFrom impl for a specific type. It just doesn't work with the use dioxus::prelude::*;
fn main() {
launch(|| rsx! { Custom {
state: "hello world"
} });
}
#[component]
fn Custom(
#[props(into)]
state: Option<MyCustomType>
) -> Element {
VNode::empty()
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct MyCustomType;
impl From<&str> for MyCustomType {
fn from(_: &str) -> Self {
Self
}
} But this version does: use dioxus::prelude::*;
fn main() {
launch(|| rsx! { Custom {
state: "hello world"
} });
}
#[component]
fn Custom(
#[props(into)]
state: Option<MyCustomType>
) -> Element {
VNode::empty()
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct MyCustomType;
impl SuperFrom<&str, MyCustomType> for Option<MyCustomType> {
fn super_from(_: &str) -> Option<MyCustomType> {
Some(MyCustomType)
}
}
I don't think that implementation is possible for the std version of Option because it already implements The implementation for specific types (like |
I just want to clarify, because I still think it would work without overlaps. Here is an example that I came up with that is similar to trait MyInto<O, M = ()> {
fn my_into(self) -> O;
}
impl<T, O, M> MyInto<O, M> for T
where
O: MyFrom<T, M>
{
fn my_into(self) -> O {
O::my_from(self)
}
}
trait MyFrom<T, M = ()> {
fn my_from(_: T) -> Self;
}
struct ValueToOptionalMyFrom;
impl<T, U> MyFrom<T, ValueToOptionalMyFrom> for Option<U>
where
T: Into<U>
{
fn my_from(value: T) -> Self {
Some(Into::<U>::into(value))
}
}
struct OptionalToOptionalMyFrom;
impl<T, U> MyFrom<Option<T>, OptionalToOptionalMyFrom> for Option<U>
where
T: Into<U>
{
fn my_from(value: Option<T>) -> Self {
value.map(Into::into)
}
}
impl<T, O> MyFrom<T, ()> for O
where
O: From<T>
{
fn my_from(value: T) -> Self {
Self::from(value)
}
}
fn test<P, M>(value: P)
where
P: MyInto<Option<String>, M>
{
let value = value.my_into();
println!("{value:?}")
} |
That implementation fails to infer in this case: fn test() {
let value: Option<i32> = 0i32.my_into();
println!("{value:?}")
} |
Feature Request
So right now, as of v0.6.1, optional component props are only supported for specific types when also using
#[props(into)]
. It would be helpful if it was also usable withOption<MyCustomType>
given the custom type meets the prop requirements.I currently wrote a custom
Option
that implementsSuperFrom
for any inner type that implementsInto
for the target type. This would mean something like this would work.Currently, there is an error since
SuperFrom
is not implemented forOption<MyCustomType>
Implement Suggestion
Something like the snippet below may work. This would also mean that it would be implemented for anything that implements
From
orInto
between the types. If this isn't desired, then maybe a new Trait that can be derived to convert between the types and would automatically allow it to be used as a prop inOption<MyCustomType>
The text was updated successfully, but these errors were encountered: