Rollup merge of #133701 - kornelski:c-str, r=workingjubilee

Use c"lit" for CStrings without unwrap

I've reviewed uses of `CString::new("lit")`.

Some could be changed to `c"lit"`. Some could be changed to `c"lit".to_owned()`, avoiding an `unwrap()`.

Many `CString` documentation examples could be simplified. I deliberately haven't changed all the examples to use the exact same expression, so that they can demonstrate many ways of creating `CString`s.

I've left UI tests mostly unchanged, because `c""` requires edition 2021, but most UI tests use 2015, and I didn't want to accidentally change what the tests are testing.
This commit is contained in:
Guillaume Gomez 2024-12-02 23:08:55 +01:00 committed by GitHub
commit d9c0f46090
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -62,15 +62,13 @@ fn memusage_linux() -> MemoryUsage {
// mallinfo2 is very recent, so its presence needs to be detected at runtime.
// Both are abysmally slow.
use std::ffi::CStr;
use std::sync::atomic::{AtomicUsize, Ordering};
static MALLINFO2: AtomicUsize = AtomicUsize::new(1);
let mut mallinfo2 = MALLINFO2.load(Ordering::Relaxed);
if mallinfo2 == 1 {
let cstr = CStr::from_bytes_with_nul(b"mallinfo2\0").unwrap();
mallinfo2 = unsafe { libc::dlsym(libc::RTLD_DEFAULT, cstr.as_ptr()) } as usize;
mallinfo2 = unsafe { libc::dlsym(libc::RTLD_DEFAULT, c"mallinfo2".as_ptr()) } as usize;
// NB: races don't matter here, since they'll always store the same value
MALLINFO2.store(mallinfo2, Ordering::Relaxed);
}