Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct safety docs #5281

Merged
merged 1 commit into from Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions clap_lex/src/ext.rs
Expand Up @@ -2,6 +2,9 @@ use std::ffi::OsStr;

pub trait OsStrExt: private::Sealed {
/// Converts to a string slice.
///
/// The Utf8Error is guaranteed to have a valid UTF8 boundary
/// in its `valid_up_to()`
fn try_str(&self) -> Result<&str, std::str::Utf8Error>;
/// Returns `true` if the given pattern matches a sub-slice of
/// this string slice.
Expand Down
3 changes: 2 additions & 1 deletion clap_lex/src/lib.rs
Expand Up @@ -463,7 +463,8 @@ fn split_nonutf8_once(b: &OsStr) -> (&str, Option<&OsStr>) {
match b.try_str() {
Ok(s) => (s, None),
Err(err) => {
// SAFETY: `char_indices` ensures `index` is at a valid UTF-8 boundary
// SAFETY: `err.valid_up_to()`, which came from str::from_utf8(), is guaranteed
// to be a valid UTF8 boundary
let (valid, after_valid) = unsafe { ext::split_at(b, err.valid_up_to()) };
let valid = valid.try_str().unwrap();
(valid, Some(after_valid))
Expand Down