diff options
| author | David Thomas <david2005thomas@gmail.com> | 2024-02-17 13:36:00 +0000 |
|---|---|---|
| committer | GnomedDev <david2005thomas@gmail.com> | 2024-02-18 17:58:52 +0000 |
| commit | dbb15fb45d45615b443f3fbf009af047719005ff (patch) | |
| tree | 97756ecf35c8e82c412d3e7e6de9d63f1a4d5da6 /library/std/src/sys/pal/common | |
| parent | 04334394337e19e632eba32ae1dfd554f5831b42 (diff) | |
Dyn erase at call site
Diffstat (limited to 'library/std/src/sys/pal/common')
| -rw-r--r-- | library/std/src/sys/pal/common/small_c_string.rs | 21 | ||||
| -rw-r--r-- | library/std/src/sys/pal/common/tests.rs | 12 |
2 files changed, 12 insertions, 21 deletions
diff --git a/library/std/src/sys/pal/common/small_c_string.rs b/library/std/src/sys/pal/common/small_c_string.rs index 8e5ecdea9a8..37812fc0659 100644 --- a/library/std/src/sys/pal/common/small_c_string.rs +++ b/library/std/src/sys/pal/common/small_c_string.rs @@ -15,24 +15,18 @@ const NUL_ERR: io::Error = io::const_io_error!(io::ErrorKind::InvalidInput, "file name contained an unexpected NUL byte"); #[inline] -pub fn run_path_with_cstr<T, F>(path: &Path, f: F) -> io::Result<T> -where - F: FnMut(&CStr) -> io::Result<T>, -{ +pub fn run_path_with_cstr<T>(path: &Path, f: &dyn Fn(&CStr) -> io::Result<T>) -> io::Result<T> { run_with_cstr(path.as_os_str().as_encoded_bytes(), f) } #[inline] -pub fn run_with_cstr<T, F>(bytes: &[u8], mut f: F) -> io::Result<T> -where - F: FnMut(&CStr) -> io::Result<T>, -{ +pub fn run_with_cstr<T>(bytes: &[u8], f: &dyn Fn(&CStr) -> io::Result<T>) -> io::Result<T> { // Dispatch and dyn erase the closure type to prevent mono bloat. // See https://github.com/rust-lang/rust/pull/121101. if bytes.len() >= MAX_STACK_ALLOCATION { - run_with_cstr_allocating(bytes, &mut f) + run_with_cstr_allocating(bytes, f) } else { - unsafe { run_with_cstr_stack(bytes, &mut f) } + unsafe { run_with_cstr_stack(bytes, f) } } } @@ -41,7 +35,7 @@ where /// `bytes` must have a length less than `MAX_STACK_ALLOCATION`. unsafe fn run_with_cstr_stack<T>( bytes: &[u8], - f: &mut dyn FnMut(&CStr) -> io::Result<T>, + f: &dyn Fn(&CStr) -> io::Result<T>, ) -> io::Result<T> { let mut buf = MaybeUninit::<[u8; MAX_STACK_ALLOCATION]>::uninit(); let buf_ptr = buf.as_mut_ptr() as *mut u8; @@ -59,10 +53,7 @@ unsafe fn run_with_cstr_stack<T>( #[cold] #[inline(never)] -fn run_with_cstr_allocating<T>( - bytes: &[u8], - f: &mut dyn FnMut(&CStr) -> io::Result<T>, -) -> io::Result<T> { +fn run_with_cstr_allocating<T>(bytes: &[u8], f: &dyn Fn(&CStr) -> io::Result<T>) -> io::Result<T> { match CString::new(bytes) { Ok(s) => f(&s), Err(_) => Err(NUL_ERR), diff --git a/library/std/src/sys/pal/common/tests.rs b/library/std/src/sys/pal/common/tests.rs index 32dc18ee1cf..e72d02203da 100644 --- a/library/std/src/sys/pal/common/tests.rs +++ b/library/std/src/sys/pal/common/tests.rs @@ -7,7 +7,7 @@ use core::iter::repeat; #[test] fn stack_allocation_works() { let path = Path::new("abc"); - let result = run_path_with_cstr(path, |p| { + let result = run_path_with_cstr(path, &|p| { assert_eq!(p, &*CString::new(path.as_os_str().as_encoded_bytes()).unwrap()); Ok(42) }); @@ -17,14 +17,14 @@ fn stack_allocation_works() { #[test] fn stack_allocation_fails() { let path = Path::new("ab\0"); - assert!(run_path_with_cstr::<(), _>(path, |_| unreachable!()).is_err()); + assert!(run_path_with_cstr::<()>(path, &|_| unreachable!()).is_err()); } #[test] fn heap_allocation_works() { let path = repeat("a").take(384).collect::<String>(); let path = Path::new(&path); - let result = run_path_with_cstr(path, |p| { + let result = run_path_with_cstr(path, &|p| { assert_eq!(p, &*CString::new(path.as_os_str().as_encoded_bytes()).unwrap()); Ok(42) }); @@ -36,7 +36,7 @@ fn heap_allocation_fails() { let mut path = repeat("a").take(384).collect::<String>(); path.push('\0'); let path = Path::new(&path); - assert!(run_path_with_cstr::<(), _>(path, |_| unreachable!()).is_err()); + assert!(run_path_with_cstr::<()>(path, &|_| unreachable!()).is_err()); } #[bench] @@ -44,7 +44,7 @@ fn bench_stack_path_alloc(b: &mut test::Bencher) { let path = repeat("a").take(383).collect::<String>(); let p = Path::new(&path); b.iter(|| { - run_path_with_cstr(p, |cstr| { + run_path_with_cstr(p, &|cstr| { black_box(cstr); Ok(()) }) @@ -57,7 +57,7 @@ fn bench_heap_path_alloc(b: &mut test::Bencher) { let path = repeat("a").take(384).collect::<String>(); let p = Path::new(&path); b.iter(|| { - run_path_with_cstr(p, |cstr| { + run_path_with_cstr(p, &|cstr| { black_box(cstr); Ok(()) }) |
