Skip to content

Commit 8daf137

Browse files
committed
Reduce monomorphisation bloat in small_c_string
1 parent 8b21296 commit 8daf137

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

library/std/src/sys/pal/common/small_c_string.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,27 @@ const NUL_ERR: io::Error =
1717
#[inline]
1818
pub fn run_path_with_cstr<T, F>(path: &Path, f: F) -> io::Result<T>
1919
where
20-
F: FnOnce(&CStr) -> io::Result<T>,
20+
F: FnMut(&CStr) -> io::Result<T>,
2121
{
2222
run_with_cstr(path.as_os_str().as_encoded_bytes(), f)
2323
}
2424

2525
#[inline]
26-
pub fn run_with_cstr<T, F>(bytes: &[u8], f: F) -> io::Result<T>
26+
pub fn run_with_cstr<T, F>(bytes: &[u8], mut f: F) -> io::Result<T>
2727
where
28-
F: FnOnce(&CStr) -> io::Result<T>,
28+
F: FnMut(&CStr) -> io::Result<T>,
2929
{
3030
if bytes.len() >= MAX_STACK_ALLOCATION {
31-
return run_with_cstr_allocating(bytes, f);
31+
run_with_cstr_allocating(bytes, &mut f)
32+
} else {
33+
unsafe { run_with_cstr_stack(bytes, &mut f) }
3234
}
35+
}
3336

37+
unsafe fn run_with_cstr_stack<T>(
38+
bytes: &[u8],
39+
f: &mut dyn FnMut(&CStr) -> io::Result<T>,
40+
) -> io::Result<T> {
3441
let mut buf = MaybeUninit::<[u8; MAX_STACK_ALLOCATION]>::uninit();
3542
let buf_ptr = buf.as_mut_ptr() as *mut u8;
3643

@@ -47,10 +54,10 @@ where
4754

4855
#[cold]
4956
#[inline(never)]
50-
fn run_with_cstr_allocating<T, F>(bytes: &[u8], f: F) -> io::Result<T>
51-
where
52-
F: FnOnce(&CStr) -> io::Result<T>,
53-
{
57+
fn run_with_cstr_allocating<T>(
58+
bytes: &[u8],
59+
f: &mut dyn FnMut(&CStr) -> io::Result<T>,
60+
) -> io::Result<T> {
5461
match CString::new(bytes) {
5562
Ok(s) => f(&s),
5663
Err(_) => Err(NUL_ERR),

src/tools/miri/tests/fail/shims/fs/isolated_file.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LL | let fd = cvt_r(|| unsafe { open64(path.as_ptr(), flags, opts.mode a
1111
= note: inside `std::sys::pal::PLATFORM::cvt_r::<i32, {closure@std::sys::pal::PLATFORM::fs::File::open_c::{closure#0}}>` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC
1212
= note: inside `std::sys::pal::PLATFORM::fs::File::open_c` at RUSTLIB/std/src/sys/pal/PLATFORM/fs.rs:LL:CC
1313
= note: inside closure at RUSTLIB/std/src/sys/pal/PLATFORM/fs.rs:LL:CC
14+
= note: inside `std::sys::pal::PLATFORM::small_c_string::run_with_cstr_stack::<std::sys::pal::PLATFORM::fs::File>` at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC
1415
= note: inside `std::sys::pal::PLATFORM::small_c_string::run_with_cstr::<std::sys::pal::PLATFORM::fs::File, {closure@std::sys::pal::PLATFORM::fs::File::open::{closure#0}}>` at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC
1516
= note: inside `std::sys::pal::PLATFORM::small_c_string::run_path_with_cstr::<std::sys::pal::PLATFORM::fs::File, {closure@std::sys::pal::PLATFORM::fs::File::open::{closure#0}}>` at RUSTLIB/std/src/sys/pal/PLATFORM/small_c_string.rs:LL:CC
1617
= note: inside `std::sys::pal::PLATFORM::fs::File::open` at RUSTLIB/std/src/sys/pal/PLATFORM/fs.rs:LL:CC

0 commit comments

Comments
 (0)