8
8
//! All items of this library are only available when the `std` feature of this
9
9
//! library is activated, and it is activated by default.
10
10
11
+ #![ cfg_attr( feature = "read_initializer" , feature( read_initializer) ) ]
12
+
11
13
#![ cfg_attr( not( feature = "std" ) , no_std) ]
12
14
13
15
#![ warn( missing_docs, missing_debug_implementations, rust_2018_idioms, unreachable_pub) ]
19
21
20
22
#![ doc( html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.18/futures_io" ) ]
21
23
24
+ #[ cfg( all( feature = "read_initializer" , not( feature = "unstable" ) ) ) ]
25
+ compile_error ! ( "The `read_initializer` feature requires the `unstable` feature as an explicit opt-in to unstable features" ) ;
26
+
22
27
#[ cfg( feature = "std" ) ]
23
28
mod if_std {
24
29
use std:: cmp;
25
30
use std:: io;
26
31
use std:: ops:: DerefMut ;
27
32
use std:: pin:: Pin ;
28
- use std:: ptr;
29
33
use std:: task:: { Context , Poll } ;
30
34
31
35
// Re-export some types from `std::io` so that users don't have to deal
@@ -40,45 +44,9 @@ mod if_std {
40
44
SeekFrom as SeekFrom ,
41
45
} ;
42
46
43
- /// A type used to conditionally initialize buffers passed to `AsyncRead`
44
- /// methods, modeled after `std`.
45
- #[ derive( Debug ) ]
46
- pub struct Initializer ( bool ) ;
47
-
48
- impl Initializer {
49
- /// Returns a new `Initializer` which will zero out buffers.
50
- #[ inline]
51
- pub fn zeroing ( ) -> Initializer {
52
- Initializer ( true )
53
- }
54
-
55
- /// Returns a new `Initializer` which will not zero out buffers.
56
- ///
57
- /// # Safety
58
- ///
59
- /// This method may only be called by `AsyncRead`ers which guarantee
60
- /// that they will not read from the buffers passed to `AsyncRead`
61
- /// methods, and that the return value of the method accurately reflects
62
- /// the number of bytes that have been written to the head of the buffer.
63
- #[ inline]
64
- pub unsafe fn nop ( ) -> Initializer {
65
- Initializer ( false )
66
- }
67
-
68
- /// Indicates if a buffer should be initialized.
69
- #[ inline]
70
- pub fn should_initialize ( & self ) -> bool {
71
- self . 0
72
- }
73
-
74
- /// Initializes a buffer if necessary.
75
- #[ inline]
76
- pub fn initialize ( & self , buf : & mut [ u8 ] ) {
77
- if self . should_initialize ( ) {
78
- unsafe { ptr:: write_bytes ( buf. as_mut_ptr ( ) , 0 , buf. len ( ) ) }
79
- }
80
- }
81
- }
47
+ #[ cfg( feature = "read_initializer" ) ]
48
+ #[ allow( unreachable_pub) ] // https://github.com/rust-lang/rust/issues/57411
49
+ pub use io:: Initializer as Initializer ;
82
50
83
51
/// Read bytes asynchronously.
84
52
///
@@ -99,6 +67,7 @@ mod if_std {
99
67
/// This method is `unsafe` because and `AsyncRead`er could otherwise
100
68
/// return a non-zeroing `Initializer` from another `AsyncRead` type
101
69
/// without an `unsafe` block.
70
+ #[ cfg( feature = "read_initializer" ) ]
102
71
#[ inline]
103
72
unsafe fn initializer ( & self ) -> Initializer {
104
73
Initializer :: zeroing ( )
@@ -342,6 +311,7 @@ mod if_std {
342
311
343
312
macro_rules! deref_async_read {
344
313
( ) => {
314
+ #[ cfg( feature = "read_initializer" ) ]
345
315
unsafe fn initializer( & self ) -> Initializer {
346
316
( * * self ) . initializer( )
347
317
}
@@ -373,6 +343,7 @@ mod if_std {
373
343
P : DerefMut + Unpin ,
374
344
P :: Target : AsyncRead ,
375
345
{
346
+ #[ cfg( feature = "read_initializer" ) ]
376
347
unsafe fn initializer ( & self ) -> Initializer {
377
348
( * * self ) . initializer ( )
378
349
}
@@ -390,12 +361,11 @@ mod if_std {
390
361
}
391
362
}
392
363
393
- /// `unsafe` because the `io::Read` type must not access the buffer
394
- /// before reading data into it.
395
- macro_rules! unsafe_delegate_async_read_to_stdio {
364
+ macro_rules! delegate_async_read_to_stdio {
396
365
( ) => {
366
+ #[ cfg( feature = "read_initializer" ) ]
397
367
unsafe fn initializer( & self ) -> Initializer {
398
- Initializer :: nop ( )
368
+ io :: Read :: initializer ( self )
399
369
}
400
370
401
371
fn poll_read( mut self : Pin <& mut Self >, _: & mut Context <' _>, buf: & mut [ u8 ] )
@@ -413,11 +383,11 @@ mod if_std {
413
383
}
414
384
415
385
impl AsyncRead for & [ u8 ] {
416
- unsafe_delegate_async_read_to_stdio ! ( ) ;
386
+ delegate_async_read_to_stdio ! ( ) ;
417
387
}
418
388
419
389
impl < T : AsRef < [ u8 ] > + Unpin > AsyncRead for io:: Cursor < T > {
420
- unsafe_delegate_async_read_to_stdio ! ( ) ;
390
+ delegate_async_read_to_stdio ! ( ) ;
421
391
}
422
392
423
393
macro_rules! deref_async_write {
0 commit comments