Skip to content

Commit

Permalink
Rollup merge of rust-lang#108124 - kornelski:cstr_c_char, r=thomcc
Browse files Browse the repository at this point in the history
Document that CStr::as_ptr returns a type alias

Rustdoc resolves type aliases too eagerly rust-lang#15823 which makes the [std re-export](https://doc.rust-lang.org/stable/std/ffi/struct.CStr.html#method.as_ptr) of `CStr::as_ptr` show `i8` instead of `c_char`. To work around this I've added info about `c_char` in the method's description.

BTW, I've also added a comment to what-not-to-do example in case someone copypasted it without reading the surrounding text.
  • Loading branch information
matthiaskrgr authored Feb 20, 2023
2 parents 7e253a7 + fd89470 commit df5d330
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ impl CStr {
/// to a contiguous region of memory terminated with a 0 byte to represent
/// the end of the string.
///
/// The type of the returned pointer is
/// [`*const c_char`][crate::ffi::c_char], and whether it's
/// an alias for `*const i8` or `*const u8` is platform-specific.
///
/// **WARNING**
///
/// The returned pointer is read-only; writing to it (including passing it
Expand All @@ -470,6 +474,7 @@ impl CStr {
/// # #![allow(unused_must_use)] #![allow(temporary_cstring_as_ptr)]
/// use std::ffi::CString;
///
/// // Do not do this:
/// let ptr = CString::new("Hello").expect("CString::new failed").as_ptr();
/// unsafe {
/// // `ptr` is dangling
Expand Down

0 comments on commit df5d330

Please sign in to comment.