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

set_attribute: Taking ToString instead of AsRef<str> #13

Closed
d4h0 opened this issue Sep 16, 2021 · 3 comments
Closed

set_attribute: Taking ToString instead of AsRef<str> #13

d4h0 opened this issue Sep 16, 2021 · 3 comments

Comments

@d4h0
Copy link

d4h0 commented Sep 16, 2021

Hi,

Thanks for creating SilkenWeb – so far I really enjoy the nice macro-less API!

(It's smooth as silk... πŸ˜…)

While looking at the source code, I noticed the following function:

fn set_attribute(dom_element: &dom::Element, name: impl AsRef<str>, value: impl AsRef<str>) {
    clone!(dom_element);
    let name = name.as_ref().to_string();
    let value = value.as_ref().to_string();

    queue_update(move || dom_element.set_attribute(&name, &value).unwrap());
}

Basically, you:

  1. request something that can be turned into &str
  2. convert it (potentially a String) into a &str
  3. and then you turn that &str into a String.

That doesn't seem to make sense to me – is there a reason for that?

I believe, ToString would be the right trait to request:

fn foo<T: ToString>(s: T) {
    println!("{}", s.to_string())
}

fn main() {
    foo("bar");
    foo("bar".to_string())
}
@simon-bourne
Copy link
Contributor

Hi, glad you like it!

I agree, impl AsRef<str> doesn't look right, and there are probably other places in the code like this. I think Into<String> might be the right trait to require here, but I'll take a look. ToString is implemented in terms of Display and specialized for some std types.

@d4h0
Copy link
Author

d4h0 commented Sep 17, 2021

Oh, right, Into<String> might be better.

@simon-bourne
Copy link
Contributor

set_attribute now takes string arguments as Into<String>

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

No branches or pull requests

2 participants