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 2 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
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