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

impl From<&str> for MaybeSignal<String> #472

Merged
merged 1 commit into from
Feb 4, 2023

Conversation

g-re-g
Copy link
Contributor

@g-re-g g-re-g commented Feb 4, 2023

Once again I'm not sure if there are any unforeseen consequences of doing this, just something I ran into while writing some code.
Enables not having to call .to_string() on a &str passed as a MaybeSignal like this:

<A class="navbar-brand" href="/">"link text"</A>

instead of this:

<A class="navbar-brand".to_string() href="/">"link text"</A>

@gbj
Copy link
Collaborator

gbj commented Feb 4, 2023

Hm... I'd like to have a more generic version of this, for sure. Unfortunately Rust won't allow

impl<T, U> From<U> for MaybeSignal<T>
where
    U: Into<T>,
{
    fn from(value: U) -> Self {
        Self::Static(value.into())
    }
}

Because it conflicts with the blanket From<T> for T, which is very annoying.

It won't even allow

impl<T> From<T> for MaybeSignal<String>
where
    T: std::fmt::Display,
{
    fn from(value: U) -> Self {
        Self::Static(value.to_string())
    }
}

Because, of course, String implements Display.

It feels weird to have a special case like this but it's so common in HTML that it probably makes sense.

@gbj gbj merged commit 7f77910 into leptos-rs:main Feb 4, 2023
@g-re-g
Copy link
Contributor Author

g-re-g commented Feb 4, 2023

I hear that, I was trying to do the same thing and couldn't find a work around!

gbj pushed a commit that referenced this pull request Feb 12, 2023
gbj pushed a commit that referenced this pull request Mar 21, 2023
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.

None yet

2 participants