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.
This commit is contained in:
Matthias Krüger 2023-02-20 14:32:54 +01:00 committed by GitHub
commit fde38f1174
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -457,6 +457,10 @@ const fn const_impl(bytes: &[u8]) -> &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
@ -470,6 +474,7 @@ const fn const_impl(bytes: &[u8]) -> &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