Skip to content

Commit

Permalink
Add unsafe blocks around conversions that need it, clean up sups.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfbiedert committed Dec 28, 2023
1 parent 0e21e97 commit 26a7784
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5787,15 +5787,15 @@ If you **want** a string of type …
| --- | --- |
|`String`|`CString::new(x)?`|
|`CString`|`x`|
|`OsString` <sup>2</sup>|`CString::new(x.to_str()?)?`|
|`OsString` |`CString::new(x.to_str()?)?`|
|`PathBuf`|`CString::new(x.to_str()?)?`|
|`Vec<u8>` <sup>1</sup> |`CString::new(x)?`|
|`&str`|`CString::new(x)?`|
|`&CStr`|`x.to_owned()` <sup>`i`</sup> |
|`&OsStr` <sup>2</sup> |`CString::new(x.to_os_string().into_string()?)?`|
|`&OsStr` |`CString::new(x.to_os_string().into_string()?)?`|
|`&Path`|`CString::new(x.to_str()?)?`|
|`&[u8]` <sup>1</sup> |`CString::new(Vec::from(x))?`|
|`*mut c_char` <sup>3</sup> |`unsafe { CString::from_raw(x) }`|
|`*mut c_char` <sup>2</sup> |`unsafe { CString::from_raw(x) }`|

</div></panel></tab>

Expand All @@ -5811,12 +5811,12 @@ If you **want** a string of type &hellip;
|`CString`|`OsString::from(x.to_str()?)`|
|`OsString`|`x`|
|`PathBuf`|`x.into_os_string()`|
|`Vec<u8>` <sup>1</sup> |`OsString::from_encoded_bytes_unchecked(x)` <sup>2</sup>|
|`Vec<u8>` <sup>1,5</sup> |`unsafe { OsString::from_encoded_bytes_unchecked(x) }`|
|`&str`|`OsString::from(x)` <sup>`i`</sup>|
|`&CStr`|`OsString::from(x.to_str()?)`|
|`&OsStr`|`OsString::from(x)` <sup>`i`</sup>|
|`&Path`|`x.as_os_str().to_owned()`|
|`&[u8]` <sup>1</sup> |`OsString::from_encoded_bytes_unchecked(x.to_vec())` <sup>2</sup>|
|`&[u8]` <sup>1,5</sup> |`unsafe { OsString::from_encoded_bytes_unchecked(x.to_vec()) }`|

</div></panel></tab>

Expand All @@ -5832,12 +5832,12 @@ If you **want** a string of type &hellip;
|`CString`|`PathBuf::from(x.to_str()?)`|
|`OsString`|`PathBuf::from(x)` <sup>`i`</sup>|
|`PathBuf`|`x`|
|`Vec<u8>` <sup>1</sup> |`PathBuf::from(OsString::from_encoded_bytes_unchecked(x))` <sup>2</sup>|
|`Vec<u8>` <sup>1,5</sup> |`unsafe { PathBuf::from(OsString::from_encoded_bytes_unchecked(x)) }` |
|`&str`|`PathBuf::from(x)` <sup>`i`</sup>|
|`&CStr`|`PathBuf::from(x.to_str()?)`|
|`&OsStr`|`PathBuf::from(x)` <sup>`i`</sup>|
|`&Path`|`PathBuf::from(x)` <sup>`i`</sup>|
|`&[u8]` <sup>1</sup> |`PathBuf::from(OsString::from_encoded_bytes_unchecked(x.to_vec()))` <sup>2</sup>|
|`&[u8]` <sup>1,5</sup> |`unsafe { PathBuf::from(OsString::from_encoded_bytes_unchecked(x.to_vec())) }` |

</div></panel></tab>

Expand Down Expand Up @@ -5893,14 +5893,14 @@ If you **want** a string of type &hellip;
| --- | --- |
|`String`|`CString::new(x)?.as_c_str()`|
|`CString`|`x.as_c_str()`|
|`OsString` <sup>2</sup>|`x.to_str()?`|
|`PathBuf`| {{ todo() }}<sup>,4</sup> |
|`Vec<u8>` <sup>1</sup><sup>,5</sup> |`CStr::from_bytes_with_nul(&x)?`|
|`&str`| {{ todo() }}<sup>,4</sup> |
|`OsString`|`x.to_str()?`|
|`PathBuf`| {{ todo() }}<sup>,3</sup> |
|`Vec<u8>` <sup>1</sup><sup>,4</sup> |`CStr::from_bytes_with_nul(&x)?`|
|`&str`| {{ todo() }}<sup>,3</sup> |
|`&CStr`|`x`|
|`&OsStr` <sup>2</sup>| {{ todo() }} |
|`&OsStr` | {{ todo() }} |
|`&Path`| {{ todo() }} |
|`&[u8]` <sup>1</sup><sup>,5</sup> |`CStr::from_bytes_with_nul(x)?`|
|`&[u8]` <sup>1</sup><sup>,4</sup> |`CStr::from_bytes_with_nul(x)?`|
|`*const c_char` <sup>1</sup> |`unsafe { CStr::from_ptr(x) }`|

</div></panel></tab>
Expand All @@ -5917,12 +5917,13 @@ If you **want** a string of type &hellip;
|`CString`| {{ todo() }} |
|`OsString`|`x.as_os_str()`|
|`PathBuf`|`x.as_os_str()`|
|`Vec<u8>` <sup>1</sup> |`OsStr::from_encoded_bytes_unchecked(&x)` <sup>2</sup>|
|`Vec<u8>` <sup>1,5</sup> |`unsafe { OsStr::from_encoded_bytes_unchecked(&x) }`|
|`&str`|`OsStr::new(x)`|
|`&CStr`| {{ todo() }} |
|`&OsStr`|`x`|
|`&Path`|`x.as_os_str()`|
|`&[u8]` <sup>1</sup> |`OsStr::from_encoded_bytes_unchecked(x)` <sup>2</sup>|
|`&[u8]` <sup>1,5</sup> | `unsafe { OsStr::from_encoded_bytes_unchecked(x) }` |


</div></panel></tab>

Expand All @@ -5938,12 +5939,12 @@ If you **want** a string of type &hellip;
|`CString`|`Path::new(x.to_str()?)` |
|`OsString`|`Path::new(x.to_str()?)` <sup>`r`</sup>|
|`PathBuf`|`Path::new(x.to_str()?)` <sup>`r`</sup>|
|`Vec<u8>` <sup>1</sup> |`Path::new(OsStr::from_encoded_bytes_unchecked(&x))` <sup>2</sup>|
|`Vec<u8>` <sup>1,5</sup> |`unsafe { Path::new(OsStr::from_encoded_bytes_unchecked(&x)) }`|
|`&str`|`Path::new(x)` <sup>`r`</sup>|
|`&CStr`|`Path::new(x.to_str()?)` |
|`&OsStr`|`Path::new(x)` <sup>`r`</sup>|
|`&Path`|`x`|
|`&[u8]` <sup>1</sup> |`Path::new(OsStr::from_encoded_bytes_unchecked(x))` <sup>2</sup>|
|`&[u8]` <sup>1,5</sup> |`unsafe { Path::new(OsStr::from_encoded_bytes_unchecked(x)) }` |

</div></panel></tab>

Expand Down Expand Up @@ -5991,17 +5992,12 @@ If you **want** a string of type &hellip;
<footnotes>

<sup>i</sup> Short form `x.into()` possible if type can be inferred. <br>
<sup>r</sup> Short form `x.as_ref()` possible if type can be inferred.

<sup>1</sup> You must ensure raw data comes with a valid representation for the string type (e.g., UTF-8 data for a `String`).

<sup>2</sup> This operation is **unsafe** and you need to additional legality checks based on your platform.

<sup>3</sup> The `c_char` **must** have come from a previous `CString`. If it comes from FFI see `&CStr` instead.

<sup>4</sup> No known shorthand as `x` will lack terminating `0x0`. Best way to probably go via `CString`.

<sup>5</sup> Must ensure vector actually ends with `0x0`.
<sup>r</sup> Short form `x.as_ref()` possible if type can be inferred.<br>
<sup>1</sup> You must ensure raw data comes with a valid representation for the string type (e.g., UTF-8 data for a `String`).<br>
<sup>2</sup> The `c_char` **must** have come from a previous `CString`. If it comes from FFI see `&CStr` instead.<br>
<sup>3</sup> No known shorthand as `x` will lack terminating `0x0`. Best way to probably go via `CString`.<br>
<sup>4</sup> Must ensure `x` actually ends with `0x0`.<br>
<sup>5</sup> Must ensure `x` actually valid operating-system native string representation.<br>

</footnotes>

Expand Down

0 comments on commit 26a7784

Please sign in to comment.