Skip to content

Commit f212ba6

Browse files
committed
use c literals in library
1 parent a17561f commit f212ba6

File tree

10 files changed

+23
-26
lines changed

10 files changed

+23
-26
lines changed

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
#![feature(allocator_internals)]
241241
#![feature(allow_internal_unsafe)]
242242
#![feature(allow_internal_unstable)]
243+
#![feature(c_str_literals)]
243244
#![feature(c_unwind)]
244245
#![feature(cfg_target_thread_local)]
245246
#![feature(concat_idents)]

library/std/src/sys/unix/args.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,13 @@ mod imp {
242242
let mut res = Vec::new();
243243

244244
unsafe {
245-
let process_info_sel = sel_registerName("processInfo\0".as_ptr());
246-
let arguments_sel = sel_registerName("arguments\0".as_ptr());
247-
let utf8_sel = sel_registerName("UTF8String\0".as_ptr());
248-
let count_sel = sel_registerName("count\0".as_ptr());
249-
let object_at_sel = sel_registerName("objectAtIndex:\0".as_ptr());
245+
let process_info_sel = sel_registerName(c"processInfo".as_ptr());
246+
let arguments_sel = sel_registerName(c"arguments".as_ptr());
247+
let utf8_sel = sel_registerName(c"UTF8String".as_ptr());
248+
let count_sel = sel_registerName(c"count".as_ptr());
249+
let object_at_sel = sel_registerName(c"objectAtIndex:".as_ptr());
250250

251-
let klass = objc_getClass("NSProcessInfo\0".as_ptr());
251+
let klass = objc_getClass(c"NSProcessInfo".as_ptr());
252252
let info = objc_msgSend(klass, process_info_sel);
253253
let args = objc_msgSend(info, arguments_sel);
254254

library/std/src/sys/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ impl File {
10631063
cfg_has_statx! {
10641064
if let Some(ret) = unsafe { try_statx(
10651065
fd,
1066-
b"\0" as *const _ as *const c_char,
1066+
c"".as_ptr() as *const c_char,
10671067
libc::AT_EMPTY_PATH | libc::AT_STATX_SYNC_AS_STAT,
10681068
libc::STATX_ALL,
10691069
) } {

library/std/src/sys/unix/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![allow(missing_docs, nonstandard_style)]
22

3-
use crate::ffi::CStr;
43
use crate::io::ErrorKind;
54

65
pub use self::rand::hashmap_random_keys;
@@ -75,7 +74,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
7574
// thread-id for the main thread and so renaming the main thread will rename the
7675
// process and we only want to enable this on platforms we've tested.
7776
if cfg!(target_os = "macos") {
78-
thread::Thread::set_name(&CStr::from_bytes_with_nul_unchecked(b"main\0"));
77+
thread::Thread::set_name(&c"main");
7978
}
8079

8180
unsafe fn sanitize_standard_fds() {
@@ -121,7 +120,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
121120
if pfd.revents & libc::POLLNVAL == 0 {
122121
continue;
123122
}
124-
if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
123+
if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
125124
// If the stream is closed but we failed to reopen it, abort the
126125
// process. Otherwise we wouldn't preserve the safety of
127126
// operations on the corresponding Rust object Stdin, Stdout, or
@@ -151,7 +150,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
151150
use libc::open64;
152151
for fd in 0..3 {
153152
if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
154-
if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
153+
if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
155154
// If the stream is closed but we failed to reopen it, abort the
156155
// process. Otherwise we wouldn't preserve the safety of
157156
// operations on the corresponding Rust object Stdin, Stdout, or

library/std/src/sys/unix/process/process_common.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ cfg_if::cfg_if! {
2424
if #[cfg(target_os = "fuchsia")] {
2525
// fuchsia doesn't have /dev/null
2626
} else if #[cfg(target_os = "redox")] {
27-
const DEV_NULL: &str = "null:\0";
27+
const DEV_NULL: &CStr = c"null:";
2828
} else if #[cfg(target_os = "vxworks")] {
29-
const DEV_NULL: &str = "/null\0";
29+
const DEV_NULL: &CStr = c"/null";
3030
} else {
31-
const DEV_NULL: &str = "/dev/null\0";
31+
const DEV_NULL: &CStr = c"/dev/null";
3232
}
3333
}
3434

@@ -474,8 +474,7 @@ impl Stdio {
474474
let mut opts = OpenOptions::new();
475475
opts.read(readable);
476476
opts.write(!readable);
477-
let path = unsafe { CStr::from_ptr(DEV_NULL.as_ptr() as *const _) };
478-
let fd = File::open_c(&path, &opts)?;
477+
let fd = File::open_c(DEV_NULL, &opts)?;
479478
Ok((ChildStdio::Owned(fd.into_inner()), None))
480479
}
481480

library/std/src/sys/unix/thread.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,9 @@ impl Thread {
163163
#[cfg(target_os = "netbsd")]
164164
pub fn set_name(name: &CStr) {
165165
unsafe {
166-
let cname = CStr::from_bytes_with_nul_unchecked(b"%s\0".as_slice());
167166
let res = libc::pthread_setname_np(
168167
libc::pthread_self(),
169-
cname.as_ptr(),
168+
c"%s".as_ptr(),
170169
name.as_ptr() as *mut libc::c_void,
171170
);
172171
debug_assert_eq!(res, 0);

library/std/src/sys/windows/c.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ pub unsafe fn NtWriteFile(
317317
// Functions that aren't available on every version of Windows that we support,
318318
// but we still use them and just provide some form of a fallback implementation.
319319
compat_fn_with_fallback! {
320-
pub static KERNEL32: &CStr = ansi_str!("kernel32");
320+
pub static KERNEL32: &CStr = c"kernel32";
321321

322322
// >= Win10 1607
323323
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
@@ -350,7 +350,7 @@ compat_fn_optional! {
350350
}
351351

352352
compat_fn_with_fallback! {
353-
pub static NTDLL: &CStr = ansi_str!("ntdll");
353+
pub static NTDLL: &CStr = c"ntdll";
354354

355355
pub fn NtCreateKeyedEvent(
356356
KeyedEventHandle: LPHANDLE,

library/std/src/sys/windows/compat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ macro_rules! compat_fn_optional {
228228
/// Load all needed functions from "api-ms-win-core-synch-l1-2-0".
229229
pub(super) fn load_synch_functions() {
230230
fn try_load() -> Option<()> {
231-
const MODULE_NAME: &CStr = ansi_str!("api-ms-win-core-synch-l1-2-0");
232-
const WAIT_ON_ADDRESS: &CStr = ansi_str!("WaitOnAddress");
233-
const WAKE_BY_ADDRESS_SINGLE: &CStr = ansi_str!("WakeByAddressSingle");
231+
const MODULE_NAME: &CStr = c"api-ms-win-core-synch-l1-2-0";
232+
const WAIT_ON_ADDRESS: &CStr = c"WaitOnAddress";
233+
const WAKE_BY_ADDRESS_SINGLE: &CStr = c"WakeByAddressSingle";
234234

235235
// Try loading the library and all the required functions.
236236
// If any step fails, then they all fail.

library/std/src/sys/windows/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(missing_docs, nonstandard_style)]
22

3-
use crate::ffi::{CStr, OsStr, OsString};
3+
use crate::ffi::{OsStr, OsString};
44
use crate::io::ErrorKind;
55
use crate::mem::MaybeUninit;
66
use crate::os::windows::ffi::{OsStrExt, OsStringExt};
@@ -51,7 +51,7 @@ pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {
5151

5252
// Normally, `thread::spawn` will call `Thread::set_name` but since this thread already
5353
// exists, we have to call it ourselves.
54-
thread::Thread::set_name(&CStr::from_bytes_with_nul_unchecked(b"main\0"));
54+
thread::Thread::set_name(&c"main");
5555
}
5656

5757
// SAFETY: must be called only once during runtime cleanup.

src/tools/tidy/src/deps.rs

-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
132132
"crossbeam-epoch",
133133
"crossbeam-utils",
134134
"crypto-common",
135-
"cstr",
136135
"datafrog",
137136
"derive_more",
138137
"digest",

0 commit comments

Comments
 (0)