Skip to content

Consider adding JsValue::as_char #1363

Closed
@RReverser

Description

@RReverser

Motivation

Currently it's not too easy to convert a single char, which is represented in JS as a string, and involves either getting a heap-allocated String and then extracting a char, or doing manual conversions that use char_code_at / code_point_at which currently don't have convenient types (see #1362), and also checking the length to make sure it's indeed a char and not an arbitrary string.

Proposed Solution

Add as_char on JsValue that would make such conversions easier by performing length check and .codePointAt on JS side, passing Option<u32> back to Rust and converting back to char using char::from_u32 internally.

Alternatives

Wait for fixes to #1362, and then ask users to use js-sys and perform conversion manually, but the length check is still non-trivial to implement correctly (because JS length can be 1 or 2 for a single-codepoint string) and feels like belongs to the core of wasm-bindgen.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions