6
6
use crate :: bitmap:: BitmapSlice ;
7
7
use crate :: volatile_memory:: copy_slice_impl:: { copy_from_volatile_slice, copy_to_volatile_slice} ;
8
8
use crate :: { VolatileMemoryError , VolatileSlice } ;
9
- use std:: io:: { Cursor , ErrorKind , Stdout } ;
9
+ use std:: io:: { Cursor , ErrorKind } ;
10
+
11
+ #[ cfg( feature = "rawfd" ) ]
12
+ use std:: io:: Stdout ;
13
+
14
+ #[ cfg( feature = "rawfd" ) ]
10
15
use std:: os:: fd:: AsRawFd ;
11
16
12
17
/// A version of the standard library's [`Read`](std::io::Read) trait that operates on volatile
@@ -114,6 +119,7 @@ pub trait WriteVolatile {
114
119
115
120
macro_rules! impl_read_write_volatile_for_raw_fd {
116
121
( $raw_fd_ty: ty) => {
122
+ #[ cfg( feature = "rawfd" ) ]
117
123
impl ReadVolatile for $raw_fd_ty {
118
124
fn read_volatile<B : BitmapSlice >(
119
125
& mut self ,
@@ -123,6 +129,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
123
129
}
124
130
}
125
131
132
+ #[ cfg( feature = "rawfd" ) ]
126
133
impl WriteVolatile for $raw_fd_ty {
127
134
fn write_volatile<B : BitmapSlice >(
128
135
& mut self ,
@@ -134,6 +141,7 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
134
141
} ;
135
142
}
136
143
144
+ #[ cfg( feature = "rawfd" ) ]
137
145
impl WriteVolatile for Stdout {
138
146
fn write_volatile < B : BitmapSlice > (
139
147
& mut self ,
@@ -153,6 +161,7 @@ impl_read_write_volatile_for_raw_fd!(std::os::fd::BorrowedFd<'_>);
153
161
/// the given [`VolatileSlice`].
154
162
///
155
163
/// Returns the numbers of bytes read.
164
+ #[ cfg( feature = "rawfd" ) ]
156
165
fn read_volatile_raw_fd < Fd : AsRawFd > (
157
166
raw_fd : & mut Fd ,
158
167
buf : & mut VolatileSlice < impl BitmapSlice > ,
@@ -183,6 +192,7 @@ fn read_volatile_raw_fd<Fd: AsRawFd>(
183
192
/// data stored in the given [`VolatileSlice`].
184
193
///
185
194
/// Returns the numbers of bytes written.
195
+ #[ cfg( feature = "rawfd" ) ]
186
196
fn write_volatile_raw_fd < Fd : AsRawFd > (
187
197
raw_fd : & mut Fd ,
188
198
buf : & VolatileSlice < impl BitmapSlice > ,
@@ -361,7 +371,10 @@ impl WriteVolatile for Cursor<&mut [u8]> {
361
371
mod tests {
362
372
use crate :: io:: { ReadVolatile , WriteVolatile } ;
363
373
use crate :: { VolatileMemoryError , VolatileSlice } ;
364
- use std:: io:: { Cursor , ErrorKind , Read , Seek , Write } ;
374
+ use std:: io:: { Cursor , ErrorKind } ;
375
+ #[ cfg( feature = "rawfd" ) ]
376
+ use std:: io:: { Read , Seek , Write } ;
377
+ #[ cfg( feature = "rawfd" ) ]
365
378
use vmm_sys_util:: tempfile:: TempFile ;
366
379
367
380
// ---- Test ReadVolatile for &[u8] ----
@@ -398,6 +411,7 @@ mod tests {
398
411
}
399
412
400
413
// ---- Test ReadVolatile for File ----
414
+ #[ cfg( feature = "rawfd" ) ]
401
415
fn read_4_bytes_from_file ( source : Vec < u8 > , expected_output : [ u8 ; 5 ] ) {
402
416
let mut temp_file = TempFile :: new ( ) . unwrap ( ) . into_file ( ) ;
403
417
temp_file. write_all ( source. as_ref ( ) ) . unwrap ( ) ;
@@ -441,6 +455,7 @@ mod tests {
441
455
442
456
for ( input, output) in test_cases {
443
457
read_4_bytes_to_5_byte_memory ( input. clone ( ) , output) ;
458
+ #[ cfg( feature = "rawfd" ) ]
444
459
read_4_bytes_from_file ( input, output) ;
445
460
}
446
461
}
@@ -481,6 +496,7 @@ mod tests {
481
496
}
482
497
483
498
// ---- Test ẂriteVolatile for File works ----
499
+ #[ cfg( feature = "rawfd" ) ]
484
500
fn write_5_bytes_to_file ( mut source : Vec < u8 > ) {
485
501
// Test write_volatile for File works
486
502
let mut temp_file = TempFile :: new ( ) . unwrap ( ) . into_file ( ) ;
@@ -524,6 +540,7 @@ mod tests {
524
540
525
541
for ( input, output) in test_cases {
526
542
write_4_bytes_to_5_byte_vec ( input. clone ( ) , output) ;
543
+ #[ cfg( feature = "rawfd" ) ]
527
544
write_5_bytes_to_file ( input) ;
528
545
}
529
546
}
0 commit comments