Skip to content

Implement automatic conversion for primitives #3533

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/yew-macro/tests/derive_props/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ mod value_into_some_value_in_props {
#[::yew::function_component]
fn Main() -> ::yew::html::Html {
::yew::html! {<>
<Inner required=3 optional=5/>
<Inner required={::std::option::Option::Some(6)}/>
<Inner required=3_usize optional=5_usize />
<Inner required={::std::option::Option::Some(6_usize)} />
</>}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn const_generics<const N: ::std::primitive::i32>() -> ::yew::Html {
}

fn compile_pass() {
::yew::html! { <Comp<Props> a=10 /> };
::yew::html! { <Comp<Props> a=10_usize /> };
::yew::html! { <Comp1<::std::primitive::usize, ::std::primitive::usize> /> };

::yew::html! { <ConstGenerics<10> /> };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ fn Comp(props: &Props) -> ::yew::prelude::Html {

fn main() {
let _ = ::yew::prelude::html! {
<Comp a={0} />
<Comp a={0_usize} />
};
}
48 changes: 24 additions & 24 deletions packages/yew-macro/tests/html_macro/component-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,14 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
| required by a bound introduced by this call
|
= help: the following other types implement trait `IntoPropValue<T>`:
f32
f64
i128
i16
i32
i64
i8
isize
<f32 as IntoPropValue<VNode>>
<f64 as IntoPropValue<VNode>>
<i128 as IntoPropValue<VNode>>
<i16 as IntoPropValue<Option<f32>>>
<i16 as IntoPropValue<Option<f64>>>
<i16 as IntoPropValue<Option<i128>>>
<i16 as IntoPropValue<Option<i32>>>
<i16 as IntoPropValue<Option<i64>>>
and $N others
note: required by a bound in `ChildPropertiesBuilder::string`
--> tests/html_macro/component-fail.rs:4:17
Expand All @@ -511,14 +511,14 @@ error[E0277]: the trait bound `{integer}: IntoPropValue<String>` is not satisfie
| required by a bound introduced by this call
|
= help: the following other types implement trait `IntoPropValue<T>`:
f32
f64
i128
i16
i32
i64
i8
isize
<f32 as IntoPropValue<VNode>>
<f64 as IntoPropValue<VNode>>
<i128 as IntoPropValue<VNode>>
<i16 as IntoPropValue<Option<f32>>>
<i16 as IntoPropValue<Option<f64>>>
<i16 as IntoPropValue<Option<i128>>>
<i16 as IntoPropValue<Option<i32>>>
<i16 as IntoPropValue<Option<i64>>>
and $N others
note: required by a bound in `ChildPropertiesBuilder::string`
--> tests/html_macro/component-fail.rs:4:17
Expand Down Expand Up @@ -556,14 +556,14 @@ error[E0277]: the trait bound `u32: IntoPropValue<i32>` is not satisfied
| required by a bound introduced by this call
|
= help: the following other types implement trait `IntoPropValue<T>`:
f32
f64
i128
i16
i32
i64
i8
isize
<f32 as IntoPropValue<VNode>>
<f64 as IntoPropValue<VNode>>
<i128 as IntoPropValue<VNode>>
<i16 as IntoPropValue<Option<f32>>>
<i16 as IntoPropValue<Option<f64>>>
<i16 as IntoPropValue<Option<i128>>>
<i16 as IntoPropValue<Option<i32>>>
<i16 as IntoPropValue<Option<i64>>>
and $N others
note: required by a bound in `ChildPropertiesBuilder::int`
--> tests/html_macro/component-fail.rs:4:17
Expand Down
3 changes: 3 additions & 0 deletions packages/yew-macro/tests/html_macro/element-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ error[E0277]: the trait bound `Option<NotToString>: IntoPropValue<Option<implici
<Option<String> as IntoPropValue<Option<implicit_clone::unsync::IString>>>
<Option<VChild<T>> as IntoPropValue<Option<ChildrenRenderer<C>>>>
<Option<VNode> as IntoPropValue<VNode>>
and $N others

error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<implicit_clone::unsync::IString>>` is not satisfied
--> tests/html_macro/element-fail.rs:48:22
Expand All @@ -468,6 +469,7 @@ error[E0277]: the trait bound `Option<{integer}>: IntoPropValue<Option<implicit_
<Option<String> as IntoPropValue<Option<implicit_clone::unsync::IString>>>
<Option<VChild<T>> as IntoPropValue<Option<ChildrenRenderer<C>>>>
<Option<VNode> as IntoPropValue<VNode>>
and $N others

error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `{integer}`
--> tests/html_macro/element-fail.rs:51:28
Expand Down Expand Up @@ -574,6 +576,7 @@ error[E0277]: the trait bound `Option<yew::NodeRef>: IntoPropValue<yew::NodeRef>
<Option<String> as IntoPropValue<Option<implicit_clone::unsync::IString>>>
<Option<VChild<T>> as IntoPropValue<Option<ChildrenRenderer<C>>>>
<Option<VNode> as IntoPropValue<VNode>>
and $N others
= note: this error originates in the macro `html` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: expected a `Fn<(MouseEvent,)>` closure, found `yew::Callback<String>`
Expand Down
10 changes: 5 additions & 5 deletions packages/yew-macro/tests/props_macro/props-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ struct Props {
}

fn pass_simple_props() {
::yew::props!(Props { a: 5 });
let (a, b) = (3, 5);
::yew::props!(Props { a: 5_usize });
let (a, b) = (3_usize, 5_usize);
::yew::props!(Props { a, b });
}

Expand All @@ -57,8 +57,8 @@ pub struct RawIdentProps {
}

fn pass_raw_idents() {
::yew::props!(RawIdentProps { r#true: 5 });
let (r#true, r#pointless_raw_name) = (3, 5);
::yew::props!(RawIdentProps { r#true: 5_usize });
let (r#true, r#pointless_raw_name) = (3_usize, 5_usize);
::yew::props!(RawIdentProps { r#true, r#pointless_raw_name });
}

Expand All @@ -68,7 +68,7 @@ struct BuildProp {
}

fn pass_build_prop() {
::yew::props!(BuildProp { build: 5 });
::yew::props!(BuildProp { build: 5_usize });
}

#[derive(::yew::Properties, ::std::cmp::PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion packages/yew-macro/tests/props_macro_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn props_order() {
last: usize,
}

let mut g = 1..=3;
let mut g = 1_usize..=3_usize;
let props = yew::props!(Props {
first: g.next().unwrap(),
second: g.next().unwrap(),
Expand Down
86 changes: 86 additions & 0 deletions packages/yew/src/html/conversion/into_prop_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,44 @@ impl_into_prop!(|value: String| -> AttrValue { AttrValue::Rc(Rc::from(value)) })
impl_into_prop!(|value: Rc<str>| -> AttrValue { AttrValue::Rc(value) });
impl_into_prop!(|value: Cow<'static, str>| -> AttrValue { AttrValue::from(value) });

// implemented for casting primitive values automatically
impl_into_prop!(|value: u8| -> u16 { value.into() });
impl_into_prop!(|value: u8| -> u32 { value.into() });
impl_into_prop!(|value: u8| -> u64 { value.into() });
impl_into_prop!(|value: u8| -> u128 { value.into() });
impl_into_prop!(|value: u8| -> usize { value.into() });
impl_into_prop!(|value: u16| -> u32 { value.into() });
impl_into_prop!(|value: u16| -> u64 { value.into() });
impl_into_prop!(|value: u16| -> u128 { value.into() });
impl_into_prop!(|value: u16| -> usize { value.into() });
impl_into_prop!(|value: u32| -> u64 { value.into() });
impl_into_prop!(|value: u32| -> u128 { value.into() });
impl_into_prop!(|value: u64| -> u128 { value.into() });

impl_into_prop!(|value: i8| -> i16 { value.into() });
impl_into_prop!(|value: i8| -> i32 { value.into() });
impl_into_prop!(|value: i8| -> i64 { value.into() });
impl_into_prop!(|value: i8| -> i128 { value.into() });
impl_into_prop!(|value: i8| -> isize { value.into() });
impl_into_prop!(|value: i16| -> i32 { value.into() });
impl_into_prop!(|value: i16| -> i64 { value.into() });
impl_into_prop!(|value: i16| -> i128 { value.into() });
impl_into_prop!(|value: i16| -> isize { value.into() });
impl_into_prop!(|value: i32| -> i64 { value.into() });
impl_into_prop!(|value: i32| -> i128 { value.into() });
impl_into_prop!(|value: i64| -> i128 { value.into() });

impl_into_prop!(|value: i8| -> f32 { value.into() });
impl_into_prop!(|value: u8| -> f32 { value.into() });
impl_into_prop!(|value: i16| -> f32 { value.into() });
impl_into_prop!(|value: u16| -> f32 { value.into() });
impl_into_prop!(|value: i8| -> f64 { value.into() });
impl_into_prop!(|value: u8| -> f64 { value.into() });
impl_into_prop!(|value: i16| -> f64 { value.into() });
impl_into_prop!(|value: u16| -> f64 { value.into() });
impl_into_prop!(|value: i32| -> f64 { value.into() });
impl_into_prop!(|value: u32| -> f64 { value.into() });

impl<T: ImplicitClone + 'static> IntoPropValue<IArray<T>> for &'static [T] {
fn into_prop_value(self) -> IArray<T> {
IArray::from(self)
Expand Down Expand Up @@ -343,6 +381,54 @@ mod test {
let _: Option<AttrValue> = Cow::Borrowed("foo").into_prop_value();
}

#[test]
fn primitives() {
macro_rules! assert_into_prop_value {
($src_ty:ty => $dst_ty:ty) => {{
let _: $dst_ty = <$src_ty as Default>::default().into_prop_value();
let _: Option<$dst_ty> = <$src_ty as Default>::default().into_prop_value();
let _: Option<$dst_ty> = Some(<$src_ty as Default>::default()).into_prop_value();
}};
}

assert_into_prop_value!(u8 => u16);
assert_into_prop_value!(u8 => u32);
assert_into_prop_value!(u8 => u64);
assert_into_prop_value!(u8 => u128);
assert_into_prop_value!(u8 => usize);
assert_into_prop_value!(u16 => u32);
assert_into_prop_value!(u16 => u64);
assert_into_prop_value!(u16 => u128);
assert_into_prop_value!(u16 => usize);
assert_into_prop_value!(u32 => u64);
assert_into_prop_value!(u32 => u128);
assert_into_prop_value!(u64 => u128);

assert_into_prop_value!(i8 => i16);
assert_into_prop_value!(i8 => i32);
assert_into_prop_value!(i8 => i64);
assert_into_prop_value!(i8 => i128);
assert_into_prop_value!(i8 => isize);
assert_into_prop_value!(i16 => i32);
assert_into_prop_value!(i16 => i64);
assert_into_prop_value!(i16 => i128);
assert_into_prop_value!(i16 => isize);
assert_into_prop_value!(i32 => i64);
assert_into_prop_value!(i32 => i128);
assert_into_prop_value!(i64 => i128);

assert_into_prop_value!(i8 => f32);
assert_into_prop_value!(u8 => f32);
assert_into_prop_value!(i16 => f32);
assert_into_prop_value!(u16 => f32);
assert_into_prop_value!(i8 => f64);
assert_into_prop_value!(u8 => f64);
assert_into_prop_value!(i16 => f64);
assert_into_prop_value!(u16 => f64);
assert_into_prop_value!(i32 => f64);
assert_into_prop_value!(u32 => f64);
}

#[test]
fn test_callback() {
let _: Callback<String> = (|_: String| ()).into_prop_value();
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ pub use yew_macro::html_nested;
/// # assert_eq!(props.name, "Minka");
/// // ... or build the associated properties of a component
/// let props = yew::props!(MyComponent::Properties {
/// id: 2,
/// id: 2_usize,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue is REALLY unfortunate. The compiler used to guess that type properly before but with this change it seems it's impossible to make it work again. I don't know why. But I think the pros outweigh the cons.

error[E0277]: the trait bound `i32: IntoPropValue<usize>` is not satisfied
  --> packages/yew/src/lib.rs:255:9
   |
32 |     id: 2,
   |     --  ^ the trait `IntoPropValue<usize>` is not implemented for `i32`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the following other types implement trait `IntoPropValue<T>`:
             <i32 as IntoPropValue<i64>>
             <i32 as IntoPropValue<i128>>
             <i32 as IntoPropValue<f64>>
             <i32 as IntoPropValue<VNode>>
             <i32 as IntoPropValue<Option<i64>>>
             <i32 as IntoPropValue<Option<i128>>>
             <i32 as IntoPropValue<Option<f64>>>
note: required by a bound in `PropsBuilder::id`
  --> packages/yew/src/lib.rs:231:17
   |
8  | #[derive(Clone, Properties, PartialEq)]
   |                 ^^^^^^^^^^ required by this bound in `PropsBuilder::id`
...
11 |     id: usize,
   |     -- required by a bound in this associated function
   = note: this error originates in the derive macro `Properties` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
Couldn't compile the test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow there are a lot of occurrences of that issue in the tests, it's terrible 😩

I'm not 100% if the pros outweigh the cons in the end...

Pros:

  • basic conversion between primitives
  • particularly useful if you call functions and return a value in some kind of type then need to convert

Cons:

  • requires to be explicit on the type of the integers pass in props
  • particularly annoying when using the values directly in props (instead of using a constant or a function)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to see if I can do something smart with the macro but I think it shouldn't go to the next release. We have enough breaking changes like that that we want to fix

/// name: Cow::from("Lemmy")
/// });
/// # assert_eq!(props.id, 2);
Expand Down