From 0e21e974756d1307eb0f60b9a3dc14f77cc53871 Mon Sep 17 00:00:00 2001 From: yhx-12243 Date: Thu, 28 Dec 2023 17:07:45 +0800 Subject: [PATCH] Update feature `os_str_bytes` in Rust 1.74.0 --- content/_index.md | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/content/_index.md b/content/_index.md index 5efefbc..5d59c94 100644 --- a/content/_index.md +++ b/content/_index.md @@ -5811,12 +5811,12 @@ If you **want** a string of type … |`CString`|`OsString::from(x.to_str()?)`| |`OsString`|`x`| |`PathBuf`|`x.into_os_string()`| -|`Vec` 1 | {{ todo() }} | +|`Vec` 1 |`OsString::from_encoded_bytes_unchecked(x)` 2| |`&str`|`OsString::from(x)` `i`| |`&CStr`|`OsString::from(x.to_str()?)`| |`&OsStr`|`OsString::from(x)` `i`| |`&Path`|`x.as_os_str().to_owned()`| -|`&[u8]` 1 | {{ todo() }} | +|`&[u8]` 1 |`OsString::from_encoded_bytes_unchecked(x.to_vec())` 2| @@ -5832,12 +5832,12 @@ If you **want** a string of type … |`CString`|`PathBuf::from(x.to_str()?)`| |`OsString`|`PathBuf::from(x)` `i`| |`PathBuf`|`x`| -|`Vec` 1 | {{ todo() }} | +|`Vec` 1 |`PathBuf::from(OsString::from_encoded_bytes_unchecked(x))` 2| |`&str`|`PathBuf::from(x)` `i`| |`&CStr`|`PathBuf::from(x.to_str()?)`| |`&OsStr`|`PathBuf::from(x)` `i`| |`&Path`|`PathBuf::from(x)` `i`| -|`&[u8]` 1 | {{ todo() }} | +|`&[u8]` 1 |`PathBuf::from(OsString::from_encoded_bytes_unchecked(x.to_vec()))` 2| @@ -5851,13 +5851,13 @@ If you **want** a string of type … | --- | --- | |`String`|`x.into_bytes()`| |`CString`|`x.into_bytes()`| -|`OsString`| {{ todo() }} | -|`PathBuf`| {{ todo() }} | +|`OsString`|`x.into_encoded_bytes()`| +|`PathBuf`|`x.into_os_string().into_encoded_bytes()`| |`Vec` 1 |`x`| |`&str`|`Vec::from(x.as_bytes())`| |`&CStr`|`Vec::from(x.to_bytes_with_nul())`| -|`&OsStr`| {{ todo() }} | -|`&Path`| {{ todo() }} | +|`&OsStr`|`Vec::from(x.as_encoded_bytes())`| +|`&Path`|`Vec::from(x.as_os_str().as_encoded_bytes())`| |`&[u8]` 1 |`x.to_vec()`| @@ -5917,12 +5917,12 @@ If you **want** a string of type … |`CString`| {{ todo() }} | |`OsString`|`x.as_os_str()`| |`PathBuf`|`x.as_os_str()`| -|`Vec` 1 | {{ todo() }} | +|`Vec` 1 |`OsStr::from_encoded_bytes_unchecked(&x)` 2| |`&str`|`OsStr::new(x)`| |`&CStr`| {{ todo() }} | |`&OsStr`|`x`| |`&Path`|`x.as_os_str()`| -|`&[u8]` 1 | {{ todo() }} | +|`&[u8]` 1 |`OsStr::from_encoded_bytes_unchecked(x)` 2| @@ -5938,12 +5938,12 @@ If you **want** a string of type … |`CString`|`Path::new(x.to_str()?)` | |`OsString`|`Path::new(x.to_str()?)` `r`| |`PathBuf`|`Path::new(x.to_str()?)` `r`| -|`Vec` 1 | {{ todo() }} | +|`Vec` 1 |`Path::new(OsStr::from_encoded_bytes_unchecked(&x))` 2| |`&str`|`Path::new(x)` `r`| |`&CStr`|`Path::new(x.to_str()?)` | |`&OsStr`|`Path::new(x)` `r`| |`&Path`|`x`| -|`&[u8]` 1 | {{ todo() }} | +|`&[u8]` 1 |`Path::new(OsStr::from_encoded_bytes_unchecked(x))` 2| @@ -5957,13 +5957,13 @@ If you **want** a string of type … | --- | --- | |`String`|`x.as_bytes()`| |`CString`|`x.as_bytes()`| -|`OsString`| {{ todo() }} | -|`PathBuf`| {{ todo() }} | +|`OsString`|`x.as_encoded_bytes()`| +|`PathBuf`|`x.as_os_str().as_encoded_bytes()`| |`Vec` 1 |`&x`| |`&str`|`x.as_bytes()`| |`&CStr`|`x.to_bytes_with_nul()`| -|`&OsStr`| `x.as_bytes()` 2 | -|`&Path`| {{ todo() }} | +|`&OsStr`|`x.as_encoded_bytes()`| +|`&Path`|`x.as_os_str().as_encoded_bytes()`| |`&[u8]` 1 |`x`| @@ -5995,14 +5995,7 @@ If you **want** a string of type … 1 You must ensure raw data comes with a valid representation for the string type (e.g., UTF-8 data for a `String`). - -2 Only on some platforms `std::os::::ffi::OsStrExt` exists with helper methods to get a raw `&[u8]` representation of the underlying `OsStr`. Use the rest of the table to go from there, e.g.: - -``` -use std::os::unix::ffi::OsStrExt; -let bytes: &[u8] = my_os_str.as_bytes(); -CString::new(bytes)? -``` +2 This operation is **unsafe** and you need to additional legality checks based on your platform. 3 The `c_char` **must** have come from a previous `CString`. If it comes from FFI see `&CStr` instead.