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

Conversation

cecton
Copy link
Member

@cecton cecton commented Nov 20, 2023

Description

This change will allow the user to pass primitive values that are normally
converted easily with .into() in Rust.

Example:

pub struct Props {
    value: u16,
}

fn foo() -> u8 { 42 }

html! {
    <MyComponent value={foo()} />
}

This is currently not allowed in Yew and should be implemented that way:

html! {
    <MyComponent value={foo() as u16} />
}

The use of as in this case is not really recommended because it is error
prone. Ideally, someone should use:

html! {
    <MyComponent value={foo().into()} />
}

But this doesn't work either in Yew.

Checklist

  • I have reviewed my own code
  • I have added tests

github-actions[bot]
github-actions bot previously approved these changes Nov 20, 2023
Copy link

github-actions bot commented Nov 20, 2023

Visit the preview URL for this PR (updated for commit 0cc3926):

https://yew-rs-api--pr3533-automatically-cast-p-8kwidweh.web.app

(expires Mon, 27 Nov 2023 08:31:36 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Copy link

github-actions bot commented Nov 20, 2023

Benchmark - core

Yew Master

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.282 ns      │ 5.308 ns      │ 2.471 ns      │ 2.51 ns       │ 100     │ 1000000000

Pull Request

vnode           fastest       │ slowest       │ median        │ mean          │ samples │ iters
╰─ vnode_clone  2.318 ns      │ 3.417 ns      │ 2.489 ns      │ 2.474 ns      │ 100     │ 1000000000

github-actions[bot]
github-actions bot previously approved these changes Nov 20, 2023
@@ -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

Copy link

github-actions bot commented Nov 20, 2023

Size Comparison

examples master (KB) pull request (KB) diff (KB) diff (%)
async_clock 100.240 100.240 0 0.000%
boids 173.734 173.734 0 0.000%
communication_child_to_parent 92.742 92.742 0 0.000%
communication_grandchild_with_grandparent 105.764 105.764 0 0.000%
communication_grandparent_to_grandchild 101.015 101.015 0 0.000%
communication_parent_to_child 89.083 89.083 0 0.000%
contexts 105.988 105.988 0 0.000%
counter 86.119 86.119 0 0.000%
counter_functional 86.508 86.508 0 0.000%
dyn_create_destroy_apps 88.927 88.927 0 0.000%
file_upload 99.991 99.991 0 0.000%
function_memory_game 172.329 172.329 0 0.000%
function_router 348.917 348.917 0 0.000%
function_todomvc 161.189 161.189 0 0.000%
futures 229.060 229.060 0 0.000%
game_of_life 110.118 110.118 0 0.000%
immutable 185.419 185.419 0 0.000%
inner_html 79.894 79.894 0 0.000%
js_callback 109.502 109.502 0 0.000%
keyed_list 199.569 199.569 0 0.000%
mount_point 82.784 82.784 0 0.000%
nested_list 113.881 113.881 0 0.000%
node_refs 90.319 90.319 0 0.000%
password_strength 1750.333 1750.333 0 0.000%
portals 93.456 93.456 0 0.000%
router 317.808 317.808 0 0.000%
simple_ssr 140.297 140.297 0 0.000%
ssr_router 386.184 386.184 0 0.000%
suspense 115.709 115.709 0 0.000%
timer 88.554 88.554 0 0.000%
timer_functional 97.905 97.905 0 0.000%
todomvc 141.335 141.335 0 0.000%
two_apps 85.823 85.823 0 0.000%
web_worker_fib 134.757 134.757 0 0.000%
web_worker_prime 184.962 184.962 0 0.000%
webgl 82.449 82.449 0 0.000%

✅ None of the examples has changed their size significantly.

Copy link

github-actions bot commented Nov 20, 2023

Benchmark - SSR

Yew Master

Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 289.445 296.181 290.357 2.056
Hello World 10 481.306 500.626 484.380 5.788
Function Router 10 1607.365 1637.106 1623.718 9.209
Concurrent Task 10 1004.782 1006.841 1006.166 0.619
Many Providers 10 1105.098 1168.838 1141.913 20.417

Pull Request

Benchmark Round Min (ms) Max (ms) Mean (ms) Standard Deviation
Baseline 10 289.321 290.355 289.612 0.321
Hello World 10 478.073 494.773 481.945 4.868
Function Router 10 1596.355 1608.716 1603.163 4.929
Concurrent Task 10 1005.603 1006.772 1006.227 0.428
Many Providers 10 1099.907 1133.165 1115.964 13.684

@cecton cecton marked this pull request as draft November 30, 2023 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant