1
- use crate :: stream:: { StreamExt , TryStreamExt , Fuse } ;
1
+ use crate :: stream:: { StreamExt , Fuse } ;
2
2
use core:: fmt;
3
3
use core:: pin:: Pin ;
4
4
use futures_core:: future:: Future ;
5
5
use futures_core:: ready;
6
6
use futures_core:: stream:: { TryStream , Stream } ;
7
7
use futures_core:: task:: { Context , Poll } ;
8
8
use futures_sink:: Sink ;
9
+ use pin_project_lite:: pin_project;
9
10
10
- /// Future for the [`send_all`](super::SinkExt::send_all) method.
11
- #[ allow( explicit_outlives_requirements) ] // https://github.com/rust-lang/rust/issues/60993
12
- #[ must_use = "futures do nothing unless you `.await` or poll them" ]
13
- pub struct SendAll < ' a , Si , St >
14
- where
15
- Si : ?Sized ,
16
- St : ?Sized + TryStream ,
17
- {
18
- sink : & ' a mut Si ,
19
- stream : Fuse < & ' a mut St > ,
20
- buffered : Option < St :: Ok > ,
11
+ pin_project ! {
12
+ /// Future for the [`send_all`](super::SinkExt::send_all) method.
13
+ #[ allow( explicit_outlives_requirements) ] // https://github.com/rust-lang/rust/issues/60993
14
+ #[ must_use = "futures do nothing unless you `.await` or poll them" ]
15
+ pub struct SendAll <' a, Si , St >
16
+ where
17
+ Si : ?Sized ,
18
+ St : TryStream ,
19
+ {
20
+ sink: & ' a mut Si ,
21
+ #[ pin]
22
+ stream: Fuse <St >,
23
+ buffered: Option <St :: Ok >,
24
+ }
21
25
}
22
26
23
27
impl < Si , St > fmt:: Debug for SendAll < ' _ , Si , St >
24
28
where
25
29
Si : fmt:: Debug + ?Sized ,
26
- St : fmt:: Debug + ? Sized + TryStream ,
30
+ St : fmt:: Debug + TryStream ,
27
31
St :: Ok : fmt:: Debug ,
28
32
{
29
33
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -35,21 +39,14 @@ where
35
39
}
36
40
}
37
41
38
- // Pinning is never projected to any fields
39
- impl < Si , St > Unpin for SendAll < ' _ , Si , St >
40
- where
41
- Si : Unpin + ?Sized ,
42
- St : TryStream + Unpin + ?Sized ,
43
- { }
44
-
45
42
impl < ' a , Si , St , Ok , Error > SendAll < ' a , Si , St >
46
43
where
47
44
Si : Sink < Ok , Error = Error > + Unpin + ?Sized ,
48
- St : TryStream < Ok = Ok , Error = Error > + Stream + Unpin + ? Sized ,
45
+ St : TryStream < Ok = Ok , Error = Error > + Stream ,
49
46
{
50
47
pub ( super ) fn new (
51
48
sink : & ' a mut Si ,
52
- stream : & ' a mut St ,
49
+ stream : St ,
53
50
) -> Self {
54
51
Self {
55
52
sink,
@@ -59,17 +56,18 @@ where
59
56
}
60
57
61
58
fn try_start_send (
62
- & mut self ,
59
+ self : Pin < & mut Self > ,
63
60
cx : & mut Context < ' _ > ,
64
61
item : St :: Ok ,
65
62
) -> Poll < Result < ( ) , Si :: Error > > {
66
- debug_assert ! ( self . buffered. is_none( ) ) ;
67
- match Pin :: new ( & mut self . sink ) . poll_ready ( cx) ? {
63
+ let this = self . project ( ) ;
64
+ debug_assert ! ( this. buffered. is_none( ) ) ;
65
+ match Pin :: new ( & mut * this. sink ) . poll_ready ( cx) ? {
68
66
Poll :: Ready ( ( ) ) => {
69
- Poll :: Ready ( Pin :: new ( & mut self . sink ) . start_send ( item) )
67
+ Poll :: Ready ( Pin :: new ( & mut * this . sink ) . start_send ( item) )
70
68
}
71
69
Poll :: Pending => {
72
- self . buffered = Some ( item) ;
70
+ * this . buffered = Some ( item) ;
73
71
Poll :: Pending
74
72
}
75
73
}
@@ -79,32 +77,32 @@ where
79
77
impl < Si , St , Ok , Error > Future for SendAll < ' _ , Si , St >
80
78
where
81
79
Si : Sink < Ok , Error = Error > + Unpin + ?Sized ,
82
- St : Stream < Item = Result < Ok , Error > > + Unpin + ? Sized ,
80
+ St : Stream < Item = Result < Ok , Error > > ,
83
81
{
84
82
type Output = Result < ( ) , Error > ;
85
83
86
84
fn poll (
87
85
mut self : Pin < & mut Self > ,
88
86
cx : & mut Context < ' _ > ,
89
87
) -> Poll < Self :: Output > {
90
- let this = & mut * self ;
91
88
// If we've got an item buffered already, we need to write it to the
92
89
// sink before we can do anything else
93
- if let Some ( item) = this . buffered . take ( ) {
94
- ready ! ( this . try_start_send( cx, item) ) ?
90
+ if let Some ( item) = self . as_mut ( ) . project ( ) . buffered . take ( ) {
91
+ ready ! ( self . as_mut ( ) . try_start_send( cx, item) ) ?
95
92
}
96
93
97
94
loop {
98
- match this. stream . try_poll_next_unpin ( cx) ? {
95
+ let this = self . as_mut ( ) . project ( ) ;
96
+ match this. stream . try_poll_next ( cx) ? {
99
97
Poll :: Ready ( Some ( item) ) => {
100
- ready ! ( this . try_start_send( cx, item) ) ?
98
+ ready ! ( self . as_mut ( ) . try_start_send( cx, item) ) ?
101
99
}
102
100
Poll :: Ready ( None ) => {
103
- ready ! ( Pin :: new( & mut this. sink) . poll_flush( cx) ) ?;
101
+ ready ! ( Pin :: new( this. sink) . poll_flush( cx) ) ?;
104
102
return Poll :: Ready ( Ok ( ( ) ) )
105
103
}
106
104
Poll :: Pending => {
107
- ready ! ( Pin :: new( & mut this. sink) . poll_flush( cx) ) ?;
105
+ ready ! ( Pin :: new( this. sink) . poll_flush( cx) ) ?;
108
106
return Poll :: Pending
109
107
}
110
108
}
0 commit comments