Skip to content
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

Unable to assign to Option<String> and Option<Cow<'static, str>> #1888

Closed
1 of 3 tasks
ctron opened this issue May 31, 2021 · 6 comments · Fixed by #1895
Closed
1 of 3 tasks

Unable to assign to Option<String> and Option<Cow<'static, str>> #1888

ctron opened this issue May 31, 2021 · 6 comments · Fixed by #1895
Labels

Comments

@ctron
Copy link
Contributor

ctron commented May 31, 2021

Problem

In Yew 0.17 the following code worked:

#[cfg(test)]
mod test {
    use super::*;
    use crate::{html, ComponentLink, Html};
    use crate::html::Properties;

    #[derive(Properties, Clone)]
    pub struct MockProps {
        #[prop_or_default]
        string: String,
        string_opt: Option<String>,
    }

    pub struct MockComponent;

    impl Component for MockComponent {
        type Message = ();
        type Properties = MockProps;

        fn create(_props: Self::Properties, _link: ComponentLink<Self>) -> Self {
            unimplemented!()
        }

        fn update(&mut self, _msg: Self::Message) -> bool {
            unimplemented!()
        }

        fn change(&mut self, _props: Self::Properties) -> bool {
            unimplemented!()
        }

        fn view(&self) -> Html {
            unimplemented!()
        }
    }

    #[test]
    fn test_str() {
        html!{
            <MockComponent
                string="foo"
                string_opt="foo"
                />
        };
    }
}

With Yew 0.18, this fails with:

error[E0277]: the trait bound `&str: conversion::IntoPropValue<std::option::Option<std::string::String>>` is not satisfied
   --> packages/yew/src/html/conversion.rs:138:28
    |
138 |                 string_opt="foo"
    |                            ^^^^^ the trait `conversion::IntoPropValue<std::option::Option<std::string::String>>` is not implemented for `&str`
    |
    = help: the following implementations were found:
              <&'static str as conversion::IntoPropValue<std::borrow::Cow<'static, str>>>
              <&'static str as conversion::IntoPropValue<std::string::String>>

Steps To Reproduce
Steps to reproduce the behavior:

  1. Compile the code above

Expected behavior

I would expect to have a simple way to assign T to Option using the macro.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment:

  • Yew version: 0.18, master
  • Rust version: 0.51.0

Questionnaire

  • I'm interested in fixing this myself but don't know where to start
  • I would like to fix and I have a solution
  • I don't have time to fix this right now, but maybe later
@ctron ctron added the bug label May 31, 2021
@ctron ctron changed the title Unable to assign to Option<String> Unable to assign to Option<String> and Option<Cow<'static, str>> May 31, 2021
@ctron
Copy link
Contributor Author

ctron commented May 31, 2021

I have the same issue when switching from String to Cow<'static, str>.

@gzeronet
Copy link

gzeronet commented Jun 2, 2021

@ctron, same issue when assign value to properties like (input) 'max', I just use 'value.to_string()', and it works now. Hope that can help.

@ctron
Copy link
Contributor Author

ctron commented Jun 2, 2021

Yea, that works. But also clones. And doesn't work with Cow.

I played around a little bit with the Yew code, and it looks to me the problem is the concept of IntoOptPropValue<T>. That doesn't work properly.

Removing this in favor of IntoPropValue<Option<T>> seems to bring the same functionality, but removes the problem. So you can properly assign &str to Option<Cow<static, str>>and&strtoOption` too.

ctron added a commit to ctron/yew that referenced this issue Jun 2, 2021
ctron added a commit to ctron/yew that referenced this issue Jun 2, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes yewstack#1888
ctron added a commit to ctron/yew that referenced this issue Jun 2, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes yewstack#1888
ctron added a commit to ctron/yew that referenced this issue Jun 2, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes yewstack#1888
@ctron
Copy link
Contributor Author

ctron commented Jun 2, 2021

I created a PR (#1895) which works fine for me, and doesn't seem to have any negative effects.

ctron added a commit to ctron/yew that referenced this issue Jun 7, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes yewstack#1888
ctron added a commit to ctron/yew that referenced this issue Jun 7, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes yewstack#1888
ctron added a commit to ctron/yew that referenced this issue Jun 7, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes yewstack#1888
ctron added a commit to ctron/yew that referenced this issue Jun 7, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes yewstack#1888
ctron added a commit to ctron/yew that referenced this issue Jun 7, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes yewstack#1888
ctron added a commit to ctron/yew that referenced this issue Jun 7, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes yewstack#1888
ctron added a commit to ctron/yew that referenced this issue Jun 7, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes yewstack#1888
siku2 pushed a commit that referenced this issue Jun 7, 2021
This drops the "optional" conversion concept in favor of directly
converting to `Option<T>`.

fixes #1888
@christianfosli
Copy link

I'm also experiencing this issue updating from yew 0.17 to 0.18.
Would be amazing if the fix from #1895 could be ported to 0.18 and released on crates.io 👼

christianfosli pushed a commit to christianfosli/taskboard-rs that referenced this issue Jul 24, 2021
@ctron
Copy link
Contributor Author

ctron commented Jul 26, 2021

Yea, I was hoping for a release of this too. Either 0.18 or 0.19.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants