motor: Use UTF-8 guarantee for OS strings #147797
Open
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.
Motor OS (a new target added in #147000) guarantees that strings from the OS are valid UTF-8, yet the
OsStrExt
/OsStringExt
traits, which aim to expose the inner UTF-8 representations, use checked UTF-8 conversions.The only way for a user to construct an
OsStr
/OsString
from arbitrary bytes is viafrom_encoded_bytes_unchecked
, which requires:Thus, callers are required to supply bytes which originated from
OsStr::as_encoded_bytes
(i.e., guaranteed to be UTF-8) and/or are valid UTF-8, so it is library UB for anOsStr
/OsString
to contain invalid UTF-8 on Motor OS.Since the standard library can make these guarantees, I think it is appropriate for these extension traits to perform unchecked conversions. As they stand, they offer no benefit over existing methods.
Also: Replace
OsStringExt::as_str
withOsStringExt::into_string
(just useOsStr::as_str
for the other) and mirror the Unix comments for these functions.cc @lasiotus