Skip to content

Conversation

@johann-cm
Copy link
Contributor

I'm proposing changing the error type of String::from_utf8 form core::str::Utf8Error to heapless::string::FromUtf8Error. This would mirror the error type of std::String::from_utf8 (which is std::string::FromUtf8Error). This is a breaking change.

FromUtf8Error has the advantage that you can regain ownership over the data that you want to convert to UTF-8 in case of an error, which you currently lose in that case.
The change would have made it possible for me in #639 to implement into_string without unsafe code.

The FromUtf8Error allows you to regain ownership over the byte vector
that you have moved into the from_utf8 method. This mirrors std.
@sgued
Copy link
Contributor

sgued commented Jan 10, 2026

Thank you for the PR!

I'm not convinced it is that useful

The ergonomics of having the Error type have a const-generic and not being Copy don't seem justified. Unlike in std, we never allocate, so cloning the Vec is somewhat cheap and can be done before calling the from_utf8 function without a significant performance cost. I suspect LLVM is very capable of avoiding the copy when doing this. If a benchmark shows this is justified, I would merge this, but otherwise I think keeping it the way it currently is is best from a downstream ergonomics standpoint.

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.

2 participants