@@ -6,6 +6,7 @@ use std::sync::Mutex;
6
6
use std:: task:: { Context , Poll } ;
7
7
8
8
use futures:: prelude:: * ;
9
+ #[ cfg( feature = "mmap" ) ]
9
10
use memmap2:: MmapMut ;
10
11
use ssri:: { Algorithm , Integrity , IntegrityOpts } ;
11
12
use tempfile:: NamedTempFile ;
@@ -14,8 +15,23 @@ use crate::async_lib::{AsyncWrite, JoinHandle};
14
15
use crate :: content:: path;
15
16
use crate :: errors:: { IoErrorExt , Result } ;
16
17
18
+ #[ cfg( feature = "mmap" ) ]
17
19
pub const MAX_MMAP_SIZE : usize = 1024 * 1024 ;
18
20
21
+ #[ cfg( not( feature = "mmap" ) ) ]
22
+ struct MmapMut ;
23
+
24
+ #[ cfg( not( feature = "mmap" ) ) ]
25
+ impl MmapMut {
26
+ fn flush_async ( & self ) -> std:: io:: Result < ( ) > {
27
+ panic ! ( )
28
+ }
29
+
30
+ fn copy_from_slice ( & self , _: & [ u8 ] ) {
31
+ panic ! ( )
32
+ }
33
+ }
34
+
19
35
pub struct Writer {
20
36
cache : PathBuf ,
21
37
builder : IntegrityOpts ,
@@ -44,24 +60,7 @@ impl Writer {
44
60
tmp_path_clone. display( )
45
61
)
46
62
} ) ?;
47
- let mmap = if let Some ( size) = size {
48
- if size <= MAX_MMAP_SIZE {
49
- tmpfile
50
- . as_file_mut ( )
51
- . set_len ( size as u64 )
52
- . with_context ( || {
53
- format ! (
54
- "Failed to configure file length for temp file at {}" ,
55
- tmpfile. path( ) . display( )
56
- )
57
- } ) ?;
58
- unsafe { MmapMut :: map_mut ( tmpfile. as_file ( ) ) . ok ( ) }
59
- } else {
60
- None
61
- }
62
- } else {
63
- None
64
- } ;
63
+ let mmap = make_mmap ( & mut tmpfile, size) ?;
65
64
Ok ( Writer {
66
65
cache : cache_path,
67
66
builder : IntegrityOpts :: new ( ) . algorithm ( algo) ,
@@ -162,24 +161,7 @@ impl AsyncWriter {
162
161
)
163
162
} ) ?;
164
163
let mut tmpfile = crate :: async_lib:: create_named_tempfile ( tmp_path) . await ?;
165
- let mmap = if let Some ( size) = size {
166
- if size <= MAX_MMAP_SIZE {
167
- tmpfile
168
- . as_file_mut ( )
169
- . set_len ( size as u64 )
170
- . with_context ( || {
171
- format ! (
172
- "Failed to configure file length for temp file at {}" ,
173
- tmpfile. path( ) . display( )
174
- )
175
- } ) ?;
176
- unsafe { MmapMut :: map_mut ( tmpfile. as_file ( ) ) . ok ( ) }
177
- } else {
178
- None
179
- }
180
- } else {
181
- None
182
- } ;
164
+ let mmap = make_mmap ( & mut tmpfile, size) ?;
183
165
Ok ( AsyncWriter ( Mutex :: new ( State :: Idle ( Some ( Inner {
184
166
cache : cache_path,
185
167
builder : IntegrityOpts :: new ( ) . algorithm ( algo) ,
@@ -428,6 +410,29 @@ impl AsyncWriter {
428
410
}
429
411
}
430
412
413
+ #[ cfg( feature = "mmap" ) ]
414
+ fn make_mmap ( tmpfile : & mut NamedTempFile , size : Option < usize > ) -> Result < Option < MmapMut > > {
415
+ if let Some ( size @ 0 ..=MAX_MMAP_SIZE ) = size {
416
+ tmpfile
417
+ . as_file_mut ( )
418
+ . set_len ( size as u64 )
419
+ . with_context ( || {
420
+ format ! (
421
+ "Failed to configure file length for temp file at {}" ,
422
+ tmpfile. path( ) . display( )
423
+ )
424
+ } ) ?;
425
+ Ok ( unsafe { MmapMut :: map_mut ( tmpfile. as_file ( ) ) . ok ( ) } )
426
+ } else {
427
+ Ok ( None )
428
+ }
429
+ }
430
+
431
+ #[ cfg( not( feature = "mmap" ) ) ]
432
+ fn make_mmap ( _: & mut NamedTempFile , _: Option < usize > ) -> Result < Option < MmapMut > > {
433
+ Ok ( None )
434
+ }
435
+
431
436
#[ cfg( test) ]
432
437
mod tests {
433
438
use super :: * ;
0 commit comments