@@ -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:: { thread, time} ;
10
12
use tempfile:: tempfile;
11
13
@@ -23,12 +25,12 @@ fn poll_aio(mut aiocb: &mut AioCb) -> Result<()> {
23
25
// AioCancelStat value.
24
26
#[ test]
25
27
fn test_cancel ( ) {
26
- let mut wbuf = "CDEF" . to_string ( ) . into_bytes ( ) ;
28
+ let wbuf: & ' static [ u8 ] = b "CDEF";
27
29
28
30
let f = tempfile ( ) . unwrap ( ) ;
29
- let mut aiocb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
31
+ let mut aiocb = AioCb :: from_slice ( f. as_raw_fd ( ) ,
30
32
0 , //offset
31
- & mut wbuf,
33
+ & wbuf,
32
34
0 , //priority
33
35
SigevNotify :: SigevNone ,
34
36
LioOpcode :: LIO_NOP ) ;
@@ -47,12 +49,12 @@ fn test_cancel() {
47
49
// Tests using aio_cancel_all for all outstanding IOs.
48
50
#[ test]
49
51
fn test_aio_cancel_all ( ) {
50
- let mut wbuf = "CDEF" . to_string ( ) . into_bytes ( ) ;
52
+ let wbuf: & ' static [ u8 ] = b "CDEF";
51
53
52
54
let f = tempfile ( ) . unwrap ( ) ;
53
- let mut aiocb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
55
+ let mut aiocb = AioCb :: from_slice ( f. as_raw_fd ( ) ,
54
56
0 , //offset
55
- & mut wbuf,
57
+ & wbuf,
56
58
0 , //priority
57
59
SigevNotify :: SigevNone ,
58
60
LioOpcode :: LIO_NOP ) ;
@@ -88,7 +90,7 @@ fn test_aio_suspend() {
88
90
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
89
91
const WBUF : & ' static [ u8 ] = b"CDEF" ;
90
92
let timeout = TimeSpec :: seconds ( 10 ) ;
91
- let mut rbuf = vec ! [ 0 ; 4 ] ;
93
+ let rbuf = Rc :: new ( vec ! [ 0 ; 4 ] . into_boxed_slice ( ) ) ;
92
94
let mut f = tempfile ( ) . unwrap ( ) ;
93
95
f. write ( INITIAL ) . unwrap ( ) ;
94
96
@@ -99,9 +101,9 @@ fn test_aio_suspend() {
99
101
SigevNotify :: SigevNone ,
100
102
LioOpcode :: LIO_WRITE ) ;
101
103
102
- let mut rcb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
104
+ let mut rcb = AioCb :: from_boxed_slice ( f. as_raw_fd ( ) ,
103
105
8 , //offset
104
- & mut rbuf,
106
+ rbuf. clone ( ) ,
105
107
0 , //priority
106
108
SigevNotify :: SigevNone ,
107
109
LioOpcode :: LIO_READ ) ;
@@ -126,6 +128,31 @@ fn test_aio_suspend() {
126
128
// for completion
127
129
#[ test]
128
130
fn test_read ( ) {
131
+ const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
132
+ let rbuf = Rc :: new ( vec ! [ 0 ; 4 ] . into_boxed_slice ( ) ) ;
133
+ const EXPECT : & ' static [ u8 ] = b"cdef" ;
134
+ let mut f = tempfile ( ) . unwrap ( ) ;
135
+ f. write ( INITIAL ) . unwrap ( ) ;
136
+ {
137
+ let mut aiocb = AioCb :: from_boxed_slice ( f. as_raw_fd ( ) ,
138
+ 2 , //offset
139
+ rbuf. clone ( ) ,
140
+ 0 , //priority
141
+ SigevNotify :: SigevNone ,
142
+ LioOpcode :: LIO_NOP ) ;
143
+ aiocb. read ( ) . unwrap ( ) ;
144
+
145
+ let err = poll_aio ( & mut aiocb) ;
146
+ assert ! ( err == Ok ( ( ) ) ) ;
147
+ assert ! ( aiocb. aio_return( ) . unwrap( ) as usize == EXPECT . len( ) ) ;
148
+ }
149
+
150
+ assert ! ( EXPECT == rbuf. deref( ) . deref( ) ) ;
151
+ }
152
+
153
+ // Tests from_mut_slice
154
+ #[ test]
155
+ fn test_read_into_mut_slice ( ) {
129
156
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
130
157
let mut rbuf = vec ! [ 0 ; 4 ] ;
131
158
const EXPECT : & ' static [ u8 ] = b"cdef" ;
@@ -152,7 +179,7 @@ fn test_read() {
152
179
#[ test]
153
180
#[ should_panic( expected = "Can't read into an immutable buffer" ) ]
154
181
fn test_read_immutable_buffer ( ) {
155
- let rbuf = vec ! [ 0 ; 4 ] ;
182
+ let rbuf: & ' static [ u8 ] = b"CDEF" ;
156
183
let f = tempfile ( ) . unwrap ( ) ;
157
184
let mut aiocb = AioCb :: from_slice ( f. as_raw_fd ( ) ,
158
185
2 , //offset
@@ -163,28 +190,29 @@ fn test_read_immutable_buffer() {
163
190
aiocb. read ( ) . unwrap ( ) ;
164
191
}
165
192
193
+
166
194
// Test a simple aio operation with no completion notification. We must poll
167
195
// for completion. Unlike test_aio_read, this test uses AioCb::from_slice
168
196
#[ test]
169
197
fn test_write ( ) {
170
198
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
171
- const WBUF : & ' static [ u8 ] = b"CDEF" ; // "CDEF".to_string().into_bytes();
199
+ let wbuf = "CDEF" . to_string ( ) . into_bytes ( ) ;
172
200
let mut rbuf = Vec :: new ( ) ;
173
201
const EXPECT : & ' static [ u8 ] = b"abCDEF123456" ;
174
202
175
203
let mut f = tempfile ( ) . unwrap ( ) ;
176
204
f. write ( INITIAL ) . unwrap ( ) ;
177
205
let mut aiocb = AioCb :: from_slice ( f. as_raw_fd ( ) ,
178
206
2 , //offset
179
- & WBUF ,
207
+ & wbuf ,
180
208
0 , //priority
181
209
SigevNotify :: SigevNone ,
182
210
LioOpcode :: LIO_NOP ) ;
183
211
aiocb. write ( ) . unwrap ( ) ;
184
212
185
213
let err = poll_aio ( & mut aiocb) ;
186
214
assert ! ( err == Ok ( ( ) ) ) ;
187
- assert ! ( aiocb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
215
+ assert ! ( aiocb. aio_return( ) . unwrap( ) as usize == wbuf . len( ) ) ;
188
216
189
217
f. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
190
218
let len = f. read_to_end ( & mut rbuf) . unwrap ( ) ;
@@ -245,7 +273,7 @@ fn test_write_sigev_signal() {
245
273
fn test_lio_listio_wait ( ) {
246
274
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
247
275
const WBUF : & ' static [ u8 ] = b"CDEF" ;
248
- let mut rbuf = vec ! [ 0 ; 4 ] ;
276
+ let rbuf = Rc :: new ( vec ! [ 0 ; 4 ] . into_boxed_slice ( ) ) ;
249
277
let mut rbuf2 = Vec :: new ( ) ;
250
278
const EXPECT : & ' static [ u8 ] = b"abCDEF123456" ;
251
279
let mut f = tempfile ( ) . unwrap ( ) ;
@@ -260,9 +288,9 @@ fn test_lio_listio_wait() {
260
288
SigevNotify :: SigevNone ,
261
289
LioOpcode :: LIO_WRITE ) ;
262
290
263
- let mut rcb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
291
+ let mut rcb = AioCb :: from_boxed_slice ( f. as_raw_fd ( ) ,
264
292
8 , //offset
265
- & mut rbuf,
293
+ rbuf. clone ( ) ,
266
294
0 , //priority
267
295
SigevNotify :: SigevNone ,
268
296
LioOpcode :: LIO_READ ) ;
@@ -272,7 +300,7 @@ fn test_lio_listio_wait() {
272
300
assert ! ( wcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
273
301
assert ! ( rcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
274
302
}
275
- assert ! ( rbuf == b"3456" ) ;
303
+ assert ! ( rbuf. deref ( ) . deref ( ) == b"3456" ) ;
276
304
277
305
f. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
278
306
let len = f. read_to_end ( & mut rbuf2) . unwrap ( ) ;
@@ -287,7 +315,7 @@ fn test_lio_listio_wait() {
287
315
fn test_lio_listio_nowait ( ) {
288
316
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
289
317
const WBUF : & ' static [ u8 ] = b"CDEF" ;
290
- let mut rbuf = vec ! [ 0 ; 4 ] ;
318
+ let rbuf = Rc :: new ( vec ! [ 0 ; 4 ] . into_boxed_slice ( ) ) ;
291
319
let mut rbuf2 = Vec :: new ( ) ;
292
320
const EXPECT : & ' static [ u8 ] = b"abCDEF123456" ;
293
321
let mut f = tempfile ( ) . unwrap ( ) ;
@@ -302,9 +330,9 @@ fn test_lio_listio_nowait() {
302
330
SigevNotify :: SigevNone ,
303
331
LioOpcode :: LIO_WRITE ) ;
304
332
305
- let mut rcb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
333
+ let mut rcb = AioCb :: from_boxed_slice ( f. as_raw_fd ( ) ,
306
334
8 , //offset
307
- & mut rbuf,
335
+ rbuf. clone ( ) ,
308
336
0 , //priority
309
337
SigevNotify :: SigevNone ,
310
338
LioOpcode :: LIO_READ ) ;
@@ -316,7 +344,7 @@ fn test_lio_listio_nowait() {
316
344
assert ! ( wcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
317
345
assert ! ( rcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
318
346
}
319
- assert ! ( rbuf == b"3456" ) ;
347
+ assert ! ( rbuf. deref ( ) . deref ( ) == b"3456" ) ;
320
348
321
349
f. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
322
350
let len = f. read_to_end ( & mut rbuf2) . unwrap ( ) ;
@@ -331,7 +359,7 @@ fn test_lio_listio_nowait() {
331
359
fn test_lio_listio_signal ( ) {
332
360
const INITIAL : & ' static [ u8 ] = b"abcdef123456" ;
333
361
const WBUF : & ' static [ u8 ] = b"CDEF" ;
334
- let mut rbuf = vec ! [ 0 ; 4 ] ;
362
+ let rbuf = Rc :: new ( vec ! [ 0 ; 4 ] . into_boxed_slice ( ) ) ;
335
363
let mut rbuf2 = Vec :: new ( ) ;
336
364
const EXPECT : & ' static [ u8 ] = b"abCDEF123456" ;
337
365
let mut f = tempfile ( ) . unwrap ( ) ;
@@ -351,9 +379,9 @@ fn test_lio_listio_signal() {
351
379
SigevNotify :: SigevNone ,
352
380
LioOpcode :: LIO_WRITE ) ;
353
381
354
- let mut rcb = AioCb :: from_mut_slice ( f. as_raw_fd ( ) ,
382
+ let mut rcb = AioCb :: from_boxed_slice ( f. as_raw_fd ( ) ,
355
383
8 , //offset
356
- & mut rbuf,
384
+ rbuf. clone ( ) ,
357
385
0 , //priority
358
386
SigevNotify :: SigevNone ,
359
387
LioOpcode :: LIO_READ ) ;
@@ -368,7 +396,7 @@ fn test_lio_listio_signal() {
368
396
assert ! ( wcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
369
397
assert ! ( rcb. aio_return( ) . unwrap( ) as usize == WBUF . len( ) ) ;
370
398
}
371
- assert ! ( rbuf == b"3456" ) ;
399
+ assert ! ( rbuf. deref ( ) . deref ( ) == b"3456" ) ;
372
400
373
401
f. seek ( SeekFrom :: Start ( 0 ) ) . unwrap ( ) ;
374
402
let len = f. read_to_end ( & mut rbuf2) . unwrap ( ) ;
@@ -381,7 +409,7 @@ fn test_lio_listio_signal() {
381
409
#[ cfg( not( any( target_os = "ios" , target_os = "macos" ) ) ) ]
382
410
#[ should_panic( expected = "Can't read into an immutable buffer" ) ]
383
411
fn test_lio_listio_read_immutable ( ) {
384
- let rbuf = vec ! [ 0 ; 4 ] ;
412
+ let rbuf: & ' static [ u8 ] = b"abcd" ;
385
413
let f = tempfile ( ) . unwrap ( ) ;
386
414
387
415
0 commit comments