1
1
use futures_core:: task:: { Context , Poll } ;
2
2
use futures_io:: AsyncWrite ;
3
3
use futures_sink:: Sink ;
4
- use std:: fmt;
5
4
use std:: io;
6
5
use std:: pin:: Pin ;
7
6
use std:: marker:: Unpin ;
8
7
use pin_utils:: { unsafe_pinned, unsafe_unpinned} ;
9
8
10
- struct Block {
9
+ #[ derive( Debug ) ]
10
+ struct Block < Item > {
11
11
offset : usize ,
12
- bytes : Box < dyn AsRef < [ u8 ] > > ,
12
+ bytes : Item ,
13
13
}
14
14
15
15
/// Sink for the [`into_sink`](super::AsyncWriteExt::into_sink) method.
16
16
#[ must_use = "sinks do nothing unless polled" ]
17
17
#[ derive( Debug ) ]
18
- pub struct IntoSink < W > {
18
+ pub struct IntoSink < W , Item > {
19
19
writer : W ,
20
20
/// An outstanding block for us to push into the underlying writer, along with an offset of how
21
21
/// far into this block we have written already.
22
- buffer : Option < Block > ,
22
+ buffer : Option < Block < Item > > ,
23
23
}
24
24
25
- impl < W : Unpin > Unpin for IntoSink < W > { }
25
+ impl < W : Unpin , Item > Unpin for IntoSink < W , Item > { }
26
26
27
- impl < W : AsyncWrite > IntoSink < W > {
27
+ impl < W : AsyncWrite , Item : AsRef < [ u8 ] > > IntoSink < W , Item > {
28
28
unsafe_pinned ! ( writer: W ) ;
29
- unsafe_unpinned ! ( buffer: Option <Block >) ;
29
+ unsafe_unpinned ! ( buffer: Option <Block < Item > >) ;
30
30
31
31
pub ( super ) fn new ( writer : W ) -> Self {
32
32
IntoSink { writer, buffer : None }
33
33
}
34
34
35
- fn project < ' a > ( self : Pin < & ' a mut Self > ) -> ( Pin < & ' a mut W > , & ' a mut Option < Block > ) {
35
+ fn project < ' a > ( self : Pin < & ' a mut Self > ) -> ( Pin < & ' a mut W > , & ' a mut Option < Block < Item > > ) {
36
36
unsafe {
37
37
let this = self . get_unchecked_mut ( ) ;
38
38
( Pin :: new_unchecked ( & mut this. writer ) , & mut this. buffer )
@@ -49,7 +49,7 @@ impl<W: AsyncWrite> IntoSink<W> {
49
49
let ( mut writer, buffer) = self . project ( ) ;
50
50
if let Some ( buffer) = buffer {
51
51
loop {
52
- let bytes = ( * buffer. bytes ) . as_ref ( ) ;
52
+ let bytes = buffer. bytes . as_ref ( ) ;
53
53
let written = ready ! ( writer. as_mut( ) . poll_write( cx, & bytes[ buffer. offset..] ) ) ?;
54
54
buffer. offset += written;
55
55
if buffer. offset == bytes. len ( ) {
@@ -63,7 +63,7 @@ impl<W: AsyncWrite> IntoSink<W> {
63
63
64
64
}
65
65
66
- impl < W : AsyncWrite , Item : AsRef < [ u8 ] > + ' static > Sink < Item > for IntoSink < W > {
66
+ impl < W : AsyncWrite , Item : AsRef < [ u8 ] > > Sink < Item > for IntoSink < W , Item > {
67
67
type SinkError = io:: Error ;
68
68
69
69
fn poll_ready (
@@ -81,7 +81,7 @@ impl<W: AsyncWrite, Item: AsRef<[u8]> + 'static> Sink<Item> for IntoSink<W> {
81
81
) -> Result < ( ) , Self :: SinkError >
82
82
{
83
83
debug_assert ! ( self . as_mut( ) . buffer( ) . is_none( ) ) ;
84
- * self . as_mut ( ) . buffer ( ) = Some ( Block { offset : 0 , bytes : Box :: new ( item) } ) ;
84
+ * self . as_mut ( ) . buffer ( ) = Some ( Block { offset : 0 , bytes : item } ) ;
85
85
Ok ( ( ) )
86
86
}
87
87
@@ -105,10 +105,3 @@ impl<W: AsyncWrite, Item: AsRef<[u8]> + 'static> Sink<Item> for IntoSink<W> {
105
105
Poll :: Ready ( Ok ( ( ) ) )
106
106
}
107
107
}
108
-
109
- impl fmt:: Debug for Block {
110
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
111
- write ! ( f, "[... {}/{} bytes ...]" , self . offset, ( * self . bytes) . as_ref( ) . len( ) ) ?;
112
- Ok ( ( ) )
113
- }
114
- }
0 commit comments