@@ -17,20 +17,27 @@ const NUL_ERR: io::Error =
17
17
#[ inline]
18
18
pub fn run_path_with_cstr < T , F > ( path : & Path , f : F ) -> io:: Result < T >
19
19
where
20
- F : FnOnce ( & CStr ) -> io:: Result < T > ,
20
+ F : FnMut ( & CStr ) -> io:: Result < T > ,
21
21
{
22
22
run_with_cstr ( path. as_os_str ( ) . as_encoded_bytes ( ) , f)
23
23
}
24
24
25
25
#[ 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 >
27
27
where
28
- F : FnOnce ( & CStr ) -> io:: Result < T > ,
28
+ F : FnMut ( & CStr ) -> io:: Result < T > ,
29
29
{
30
30
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) }
32
34
}
35
+ }
33
36
37
+ unsafe fn run_with_cstr_stack < T > (
38
+ bytes : & [ u8 ] ,
39
+ f : & mut dyn FnMut ( & CStr ) -> io:: Result < T > ,
40
+ ) -> io:: Result < T > {
34
41
let mut buf = MaybeUninit :: < [ u8 ; MAX_STACK_ALLOCATION ] > :: uninit ( ) ;
35
42
let buf_ptr = buf. as_mut_ptr ( ) as * mut u8 ;
36
43
@@ -47,10 +54,10 @@ where
47
54
48
55
#[ cold]
49
56
#[ 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 > {
54
61
match CString :: new ( bytes) {
55
62
Ok ( s) => f ( & s) ,
56
63
Err ( _) => Err ( NUL_ERR ) ,
0 commit comments