Skip to content

Commit fde38f1

Browse files
authored
Rollup merge of #108124 - kornelski:cstr_c_char, r=thomcc
Document that CStr::as_ptr returns a type alias Rustdoc resolves type aliases too eagerly #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.
2 parents e7eaed2 + fd89470 commit fde38f1

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

library/core/src/ffi/c_str.rs

+5
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ impl CStr {
457457
/// to a contiguous region of memory terminated with a 0 byte to represent
458458
/// the end of the string.
459459
///
460+
/// The type of the returned pointer is
461+
/// [`*const c_char`][crate::ffi::c_char], and whether it's
462+
/// an alias for `*const i8` or `*const u8` is platform-specific.
463+
///
460464
/// **WARNING**
461465
///
462466
/// The returned pointer is read-only; writing to it (including passing it
@@ -470,6 +474,7 @@ impl CStr {
470474
/// # #![allow(unused_must_use)] #![allow(temporary_cstring_as_ptr)]
471475
/// use std::ffi::CString;
472476
///
477+
/// // Do not do this:
473478
/// let ptr = CString::new("Hello").expect("CString::new failed").as_ptr();
474479
/// unsafe {
475480
/// // `ptr` is dangling

0 commit comments

Comments
 (0)