Add SecretBox::from_utf8 and related methods - #1364
Open
mrdomino wants to merge 11 commits into
Open
Conversation
These methods allow converting a `SecretBox<[u8]>` into a `SecretString` without copies. They are both based on a new `TryFrom<Vec<u8>>` for `SecretString`.
mrdomino
force-pushed
the
add-utf8-methods
branch
from
March 14, 2026 20:52
b681716 to
341331c
Compare
I, Steven Dee, hereby agree to license all contributions I make to this project under the terms of the Apache License, Version 2.0.
Author
|
Just read |
I decided the `impl TryFrom<Vec<u8>>` was too cheesy and instead just called that method `SecretString::from_utf8`, with different variants for `from_utf8_box` or `from_utf8_box_len`.
Specify the actual path from `Box<[u8]>` to `SecretString` with a copy.
mrdomino
force-pushed
the
add-utf8-methods
branch
from
March 17, 2026 23:55
87e0179 to
f8707fe
Compare
Puts the new methods next to the other `SecretString` trait impls. Derives `Debug`, `Display`, and `Error` on `FromUtf8Error`.
mrdomino
force-pushed
the
add-utf8-methods
branch
from
March 18, 2026 00:30
b840404 to
cdd32d7
Compare
Drops Clone (which is only derived on `no_global_oom_handling`) and adds `PartialEq` + `Eq`.
Author
|
Coming back to this: is there any interest in anything in the general area of this? Very open to discussing changes to the proposed API. Also fine with “no, we don’t want anything like this” as an answer; extra copies are not the worst thing in the world. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
These methods allow converting a
SecretBox<[u8]>into aSecretStringwithout creating a new allocation. Also adds a newFromUtf8Errortype, similar to (and in fact wrapping) the one returned byString::from_utf8, except that this one zeroizes its buffer on return.I made the choice here to provide this functionality via functions declared directly on
impl SecretString(i.e.,impl SecretBox<str>) rather than via, say,impl TryFrom<Vec<u8>> for SecretString. Either approach would probably work; two arguments in favor of the one taken:StringAPI.from_utf8_box_len, except maybe via a contrivedTryFrom<(SecretBox<[u8]>, usize)>.