Skip to content

Tracking Issue for explicit-endian String::from_utf16 #116258

Description

@CAD97

View all comments

Feature gate: #![feature(str_from_utf16_endian)]

This is a tracking issue for versions of String::from_utf16 which take &[u8] and use a specific endianness.

Public API

impl String {
    fn from_utf16le(v: &[u8]) -> Result<String, FromUtf16Error>;
    fn from_utf16le_lossy(v: &[u8]) -> String;
    fn from_utf16be(v: &[u8]) -> Result<String, FromUtf16Error>;
    fn from_utf16be_lossy(v: &[u8]) -> String;
}

Steps / History

Unresolved Questions

  • Ideal naming; options include from_utf16le, from_utf16_le, from_le_utf16, from_le_utf16_bytes, and other such combinations.
  • Should these methods get the with_capacity+push implementation used for from_utf16 while collect doesn't reserve capacity? (Collecting into a Result<Vec<_>> doesn't reserve the capacity in advance #48994)
  • Tweaks to the error type: FromUtf16Error currently displays as "invalid utf-16: lone surrogate found" which isn't correct for an error due to odd byte length.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩

Activity

  1. added
    T-libs-api[DEPRECATED; DO NOT USE]
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Sep 29, 2023
  2. changed the title [-]Tracking Issue for endian specific String::from_utf16[/-] [+]Tracking Issue for explicit-endian String::from_utf16[/+] on Sep 29, 2023
  3. zachs18 commented on Jan 19, 2024

    @zachs18
    Contributor

    Perhaps as an unresolved question: with these added, FromUtf16Error's Display impl is no longer always accurate; it says "invalid utf-16: lone surrogate found", but these functions introduce a new failure case: the &[u8] was of odd length. Making FromUtf16Error hold information about which kind of error occurred would require making it not a ZST anymore, which could degrade performance since currently Result<String, FromUtf16Error> is (non-guaranteed-ly) null-pointer-optimized to be the same size as String. (see below)

    Alternately, they could return some new FromUtf16BytesError type which can represent both errors, so that String::from_utf16 can still return the null-pointer-optimized Result<String, FromUtf16Error>.

    (Alternately, FromUtf16Error's Display impl could be updated to say something like "invalid utf-16: lone surrogate found, or odd length byte string passed".)

  4. CAD97 commented on Jan 21, 2024

    @CAD97
    ContributorAuthor

    To note, Result<String, enum { L, R }> is still niched. The data pointer is null and the other 2×usize are available to carry the Err payload. The only performance hit would be constructing or inspecting the error payload.

    But that said, I also think just rendering the error as invalid utf-16 would be sufficient. Adding a new variant to the existing enum is also fine, but I don't think making a new error type is particularly helpful.

    An alternative would be to panic if given an odd-length slice, since that's trivial to precheck. But not a particularly good alternative.

  5. eirnym commented on Mar 20, 2025

    @eirnym

    Any news on this tracking issue? I'd love to use it

  6. Havunen commented on Apr 7, 2025

    @Havunen

    Yeah, I have been using these for months without issues. This API is my last one that requires nightly version of Rust. I would love to move back to stable :)

    Is there anything we can help to get this stabilized?

  7. Oakchris1955 commented on May 25, 2025

    @Oakchris1955

    I agree with the people above, I think that we should try to get this stabilized ASAP

  8. Havunen commented on Jul 26, 2025

    @Havunen

    Hi rust lang libs team, I'm asking for help how to proceed stabilizing str_from_utf16_endian feature. I believe it is ready for "Final comment period" FCP.

    @rustbot ping libs-team

    @rust-lang/libs-api

  9. rustbot commented on Jul 26, 2025

    @rustbot
    Collaborator

    Error: Parsing ping command in comment failed: ...' libs-team' | error: expected end of command at >| ' I'm ask'...

    Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

  10. rustbot commented on Jul 26, 2025

    @rustbot
    Collaborator

    Error: Only Rust team members can ping teams.

    Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

  11. joshtriplett commented on Aug 5, 2025

    @joshtriplett
    Member

    We discussed this in today's @rust-lang/libs-api meeting.

    We'd like to see these stabilized. We're happy with the current proposed names.

    @rfcbot merge

    We do want to see the error improved, either by making the single error message more generic or by adding internal variants for multiple different errors.

    @rfcbot concern fix-error-message

  12. rfcbot commented on Aug 5, 2025

    @rfcbot

    Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

    Concerns:

    Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

    See this document for info about what commands tagged team members can give me.

  13. 29 remaining items

  14. added
    to-announceAnnounce this issue on triage meeting
    and removed
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    on Jun 12, 2026
  15. rust-rfcbot commented on Jun 12, 2026

    @rust-rfcbot
    Collaborator

    The final comment period, with a disposition to merge, as per the review above, is now complete.

    As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

  16. added a commit that references this issue on Jun 18, 2026
    6705db6
  17. added a commit that references this issue on Jun 18, 2026
    26c003f
  18. tgross35 commented on Aug 19, 2026

    @tgross35
    Member

    Hello, as of Rust 1.98.0-nightly FromUtf16Error does not implement Clone similar to FromUtf8Error.

    FromUtf16ErrorKind does implement Clone and FromUtf8Error also does with #[cfg_attr(not(no_global_oom_handling), derive(Clone))]

    @cooolbros those are small enough that if you are interested, you could submit a PR and nominate for T-libs: I expect it to be unobjectionable enough to FCP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCS-waiting-on-fcpStatus: PR is in FCP and is awaiting for FCP to complete.T-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions