@@ -5,7 +5,9 @@ use nix::sys::aio::*;
5
5
use nix:: sys:: signal:: * ;
6
6
use nix:: sys:: time:: { TimeSpec , TimeValLike } ;
7
7
use std:: io:: { Write , Read , Seek , SeekFrom } ;
8
+ use std:: ops:: Deref ;
8
9
use std:: os:: unix:: io:: AsRawFd ;
10
+ use std:: rc:: Rc ;
9
11
use std:: sync:: Mutex ;
10
12
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
11
13
use std:: { thread, time} ;
@@ -25,12 +27,12 @@ fn poll_aio(mut aiocb: &mut AioCb) -> Result<()> {
25
27
// AioCancelStat value.
26
28
#[ test]
27
29
fn test_cancel ( ) {
28
- let mut wbuf = "CDEF" . to_string ( ) . into_bytes ( ) ;
30
+ let wbuf: & ' static [ u8 ] = b "CDEF";
29
31
30
32
let f = tempfile ( ) . unwrap ( ) ;
31
- let mut aiocb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
33
+ let mut aiocb = AioCb :: from_slice ( f. as_raw_fd ( ) ,
32
34
0 , //offset
33
- & mut wbuf,
35
+ & wbuf,
34
36
0 , //priority
35
37
SigevNotify :: SigevNone ,
36
38
LioOpcode :: LIO_NOP ) ;
@@ -49,12 +51,12 @@ fn test_cancel() {
49
51
// Tests using aio_cancel_all for all outstanding IOs.
50
52
#[ test]
51
53
fn test_aio_cancel_all ( ) {
52
- let mut wbuf = "CDEF" . to_string ( ) . into_bytes ( ) ;
54
+ let wbuf: & ' static [ u8 ] = b "CDEF";
53
55
54
56
let f = tempfile ( ) . unwrap ( ) ;
55
- let mut aiocb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
57
+ let mut aiocb = AioCb :: from_slice ( f. as_raw_fd ( ) ,
56
58
0 , //offset
57
- & mut wbuf,
59
+ & wbuf,
58
60
0 , //priority
59
61
SigevNotify :: SigevNone ,
60
62
LioOpcode :: LIO_NOP ) ;
@@ -90,7 +92,7 @@ fn test_aio_suspend() {
90
92
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
91
93
const WBUF : & ' static [ u8 ] = b"CDEF" ;
92
94
let timeout = TimeSpec :: seconds ( 10 ) ;
93
- let mut rbuf = vec ! [ 0 ; 4 ] ;
95
+ let rbuf = Rc :: new ( vec ! [ 0 ; 4 ] . into_boxed_slice ( ) ) ;
94
96
let mut f = tempfile ( ) . unwrap ( ) ;
95
97
f. write ( INITIAL ) . unwrap ( ) ;
96
98
@@ -101,9 +103,9 @@ fn test_aio_suspend() {
101
103
SigevNotify :: SigevNone ,
102
104
LioOpcode :: LIO_WRITE ) ;
103
105
104
- let mut rcb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
106
+ let mut rcb = AioCb :: from_boxed_slice ( f. as_raw_fd ( ) ,
105
107
8 , //offset
106
- & mut rbuf,
108
+ rbuf. clone ( ) ,
107
109
0 , //priority
108
110
SigevNotify :: SigevNone ,
109
111
LioOpcode :: LIO_READ ) ;
@@ -128,6 +130,31 @@ fn test_aio_suspend() {
128
130
// for completion
129
131
#[ test]
130
132
fn test_read ( ) {
133
+ const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
134
+ let rbuf = Rc :: new ( vec ! [ 0 ; 4 ] . into_boxed_slice ( ) ) ;
135
+ const EXPECT : & ' static [ u8 ] = b"cdef" ;
136
+ let mut f = tempfile ( ) . unwrap ( ) ;
137
+ f. write ( INITIAL ) . unwrap ( ) ;
138
+ {
139
+ let mut aiocb = AioCb :: from_boxed_slice ( f. as_raw_fd ( ) ,
140
+ 2 , //offset
141
+ rbuf. clone ( ) ,
142
+ 0 , //priority
143
+ SigevNotify :: SigevNone ,
144
+ LioOpcode :: LIO_NOP ) ;
145
+ aiocb. read ( ) . unwrap ( ) ;
146
+
147
+ let err = poll_aio ( & mut aiocb) ;
148
+ assert ! ( err == Ok ( ( ) ) ) ;
149
+ assert ! ( aiocb. aio_return( ) . unwrap( ) as usize == EXPECT . len( ) ) ;
150
+ }
151
+
152
+ assert ! ( EXPECT == rbuf. deref( ) . deref( ) ) ;
153
+ }
154
+
155
+ // Tests from_mut_slice
156
+ #[ test]
157
+ fn test_read_into_mut_slice ( ) {
131
158
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
132
159
let mut rbuf = vec ! [ 0 ; 4 ] ;
133
160
const EXPECT : & ' static [ u8 ] = b"cdef" ;
@@ -154,7 +181,7 @@ fn test_read() {
154
181
#[ test]
155
182
#[ should_panic( expected = "Can't read into an immutable buffer" ) ]
156
183
fn test_read_immutable_buffer ( ) {
157
- let rbuf = vec ! [ 0 ; 4 ] ;
184
+ let rbuf: & ' static [ u8 ] = b"CDEF" ;
158
185
let f = tempfile ( ) . unwrap ( ) ;
159
186
let mut aiocb = AioCb :: from_slice ( f. as_raw_fd ( ) ,
160
187
2 , //offset
@@ -165,28 +192,29 @@ fn test_read_immutable_buffer() {
165
192
aiocb. read ( ) . unwrap ( ) ;
166
193
}
167
194
195
+
168
196
// Test a simple aio operation with no completion notification. We must poll
169
197
// for completion. Unlike test_aio_read, this test uses AioCb::from_slice
170
198
#[ test]
171
199
fn test_write ( ) {
172
200
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
173
- const WBUF : & ' static [ u8 ] = b"CDEF" ; // "CDEF".to_string().into_bytes();
201
+ let wbuf = "CDEF" . to_string ( ) . into_bytes ( ) ;
174
202
let mut rbuf = Vec :: new ( ) ;
175
203
const EXPECT : & ' static [ u8 ] = b"abCDEF123456" ;
176
204
177
205
let mut f = tempfile ( ) . unwrap ( ) ;
178
206
f. write ( INITIAL ) . unwrap ( ) ;
179
207
let mut aiocb = AioCb :: from_slice ( f. as_raw_fd ( ) ,
180
208
2 , //offset
181
- & WBUF ,
209
+ & wbuf ,
182
210
0 , //priority
183
211
SigevNotify :: SigevNone ,
184
212
LioOpcode :: LIO_NOP ) ;
185
213
aiocb. write ( ) . unwrap ( ) ;
186
214
187
215
let err = poll_aio ( & mut aiocb) ;
188
216
assert ! ( err == Ok ( ( ) ) ) ;
189
- assert ! ( aiocb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
217
+ assert ! ( aiocb. aio_return( ) . unwrap( ) as usize == wbuf . len( ) ) ;
190
218
191
219
f. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
192
220
let len = f. read_to_end ( & mut rbuf) . unwrap ( ) ;
@@ -249,7 +277,7 @@ fn test_write_sigev_signal() {
249
277
fn test_lio_listio_wait ( ) {
250
278
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
251
279
const WBUF : & ' static [ u8 ] = b"CDEF" ;
252
- let mut rbuf = vec ! [ 0 ; 4 ] ;
280
+ let rbuf = Rc :: new ( vec ! [ 0 ; 4 ] . into_boxed_slice ( ) ) ;
253
281
let mut rbuf2 = Vec :: new ( ) ;
254
282
const EXPECT : & ' static [ u8 ] = b"abCDEF123456" ;
255
283
let mut f = tempfile ( ) . unwrap ( ) ;
@@ -264,9 +292,9 @@ fn test_lio_listio_wait() {
264
292
SigevNotify :: SigevNone ,
265
293
LioOpcode :: LIO_WRITE ) ;
266
294
267
- let mut rcb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
295
+ let mut rcb = AioCb :: from_boxed_slice ( f. as_raw_fd ( ) ,
268
296
8 , //offset
269
- & mut rbuf,
297
+ rbuf. clone ( ) ,
270
298
0 , //priority
271
299
SigevNotify :: SigevNone ,
272
300
LioOpcode :: LIO_READ ) ;
@@ -276,7 +304,7 @@ fn test_lio_listio_wait() {
276
304
assert ! ( wcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
277
305
assert ! ( rcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
278
306
}
279
- assert ! ( rbuf == b"3456" ) ;
307
+ assert ! ( rbuf. deref ( ) . deref ( ) == b"3456" ) ;
280
308
281
309
f. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
282
310
let len = f. read_to_end ( & mut rbuf2) . unwrap ( ) ;
@@ -291,7 +319,7 @@ fn test_lio_listio_wait() {
291
319
fn test_lio_listio_nowait ( ) {
292
320
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
293
321
const WBUF : & ' static [ u8 ] = b"CDEF" ;
294
- let mut rbuf = vec ! [ 0 ; 4 ] ;
322
+ let rbuf = Rc :: new ( vec ! [ 0 ; 4 ] . into_boxed_slice ( ) ) ;
295
323
let mut rbuf2 = Vec :: new ( ) ;
296
324
const EXPECT : & ' static [ u8 ] = b"abCDEF123456" ;
297
325
let mut f = tempfile ( ) . unwrap ( ) ;
@@ -306,9 +334,9 @@ fn test_lio_listio_nowait() {
306
334
SigevNotify :: SigevNone ,
307
335
LioOpcode :: LIO_WRITE ) ;
308
336
309
- let mut rcb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
337
+ let mut rcb = AioCb :: from_boxed_slice ( f. as_raw_fd ( ) ,
310
338
8 , //offset
311
- & mut rbuf,
339
+ rbuf. clone ( ) ,
312
340
0 , //priority
313
341
SigevNotify :: SigevNone ,
314
342
LioOpcode :: LIO_READ ) ;
@@ -320,7 +348,7 @@ fn test_lio_listio_nowait() {
320
348
assert ! ( wcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
321
349
assert ! ( rcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
322
350
}
323
- assert ! ( rbuf == b"3456" ) ;
351
+ assert ! ( rbuf. deref ( ) . deref ( ) == b"3456" ) ;
324
352
325
353
f. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
326
354
let len = f. read_to_end ( & mut rbuf2) . unwrap ( ) ;
@@ -336,7 +364,7 @@ fn test_lio_listio_signal() {
336
364
let _ = SIGUSR2_MTX . lock ( ) . expect ( "Mutex got poisoned by another test" ) ;
337
365
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
338
366
const WBUF : & ' static [ u8 ] = b"CDEF" ;
339
- let mut rbuf = vec ! [ 0 ; 4 ] ;
367
+ let rbuf = Rc :: new ( vec ! [ 0 ; 4 ] . into_boxed_slice ( ) ) ;
340
368
let mut rbuf2 = Vec :: new ( ) ;
341
369
const EXPECT : & ' static [ u8 ] = b"abCDEF123456" ;
342
370
let mut f = tempfile ( ) . unwrap ( ) ;
@@ -356,9 +384,9 @@ fn test_lio_listio_signal() {
356
384
SigevNotify :: SigevNone ,
357
385
LioOpcode :: LIO_WRITE ) ;
358
386
359
- let mut rcb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
387
+ let mut rcb = AioCb :: from_boxed_slice ( f. as_raw_fd ( ) ,
360
388
8 , //offset
361
- & mut rbuf,
389
+ rbuf. clone ( ) ,
362
390
0 , //priority
363
391
SigevNotify :: SigevNone ,
364
392
LioOpcode :: LIO_READ ) ;
@@ -373,7 +401,7 @@ fn test_lio_listio_signal() {
373
401
assert ! ( wcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
374
402
assert ! ( rcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
375
403
}
376
- assert ! ( rbuf == b"3456" ) ;
404
+ assert ! ( rbuf. deref ( ) . deref ( ) == b"3456" ) ;
377
405
378
406
f. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
379
407
let len = f. read_to_end ( & mut rbuf2) . unwrap ( ) ;
@@ -386,7 +414,7 @@ fn test_lio_listio_signal() {
386
414
#[ cfg( not( any( target_os = "ios" , target_os = "macos" ) ) ) ]
387
415
#[ should_panic( expected = "Can't read into an immutable buffer" ) ]
388
416
fn test_lio_listio_read_immutable ( ) {
389
- let rbuf = vec ! [ 0 ; 4 ] ;
417
+ let rbuf: & ' static [ u8 ] = b"abcd" ;
390
418
let f = tempfile ( ) . unwrap ( ) ;
391
419
392
420
0 commit comments