Skip to content

Commit 7653d39

Browse files
committed
Reduce monomorphisation bloat in small_c_string
1 parent 150523e commit 7653d39

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

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),

0 commit comments

Comments
 (0)