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

Add importing strings as JsString #4055

Merged
merged 1 commit into from
Aug 6, 2024
Merged

Conversation

daxpedda
Copy link
Collaborator

@daxpedda daxpedda commented Aug 6, 2024

This PR adds the ability to import strings directly as JsString. This can be useful when trying to avoid TextDecoder/Encoder, e.g. when dealing with an audio worklet.

#[wasm_bindgen]
// Currently Rustfmt simply removes the value on this static.
// See <https://github.com/rust-lang/rustfmt/issues/6267>.
#[rustfmt::skip]
extern "C" {
    #[wasm_bindgen(static_string)]
    static STRING: JsString = "foo";
}

I added the static_string attribute item requirement because we might want to support other types in the future and we can't do type checking on the proc-macro level without knowing what the type should be. The alternative would be to do the type checking via wasm-bindgen by using descriptors, which isn't ideal for users.

Fixes #4031.

@daxpedda daxpedda merged commit 0b1cce6 into rustwasm:main Aug 6, 2024
27 checks passed
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.

Defining String Constants
1 participant