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

witx: Add trait implementations to Id #287

Merged

Conversation

cratelyn
Copy link
Contributor

@cratelyn cratelyn commented Jun 4, 2020

While working on bytecodealliance/wasmtime#1793, I noticed that Id cannot be compared against strings, or referenced as a string slice. While as_str can be used, this often means hanging onto a temporary value, i.e. let id_str: &str = id.as_str().

Because Id is a newtype wrapper around a single String, it felt like these would be both useful & safe to expose a few more trait implementations, delegating to the underlying String value.

After applying these changes, this code will compile:

use witx::Id;

fn main() {
    let id = Id::new("hello");

    // Compare to string slices, as the lhs.
    let _ = id == "hello";

    // Compare to string slices, as the rhs.
    let _ = "hello" == id;

    // Call `Into::into` and `From::from` on string slices.
    let _: Id = "call into on a string".into();
    let _ = Id::from("call from, on a string");
}

@pchickey pchickey merged commit 7d1f396 into WebAssembly:master Jun 4, 2020
@cratelyn cratelyn deleted the ktm/add-witx-id-as-ref-string-slice-impl branch June 4, 2020 16:07
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