1
1
use super :: copy:: CopyBuffer ;
2
2
3
+ use crate :: future:: poll_fn;
3
4
use crate :: io:: { AsyncRead , AsyncWrite } ;
4
5
5
- use std:: future:: Future ;
6
6
use std:: io;
7
7
use std:: pin:: Pin ;
8
8
use std:: task:: { Context , Poll } ;
@@ -13,13 +13,6 @@ enum TransferState {
13
13
Done ( u64 ) ,
14
14
}
15
15
16
- struct CopyBidirectional < ' a , A : ?Sized , B : ?Sized > {
17
- a : & ' a mut A ,
18
- b : & ' a mut B ,
19
- a_to_b : TransferState ,
20
- b_to_a : TransferState ,
21
- }
22
-
23
16
fn transfer_one_direction < A , B > (
24
17
cx : & mut Context < ' _ > ,
25
18
state : & mut TransferState ,
48
41
}
49
42
}
50
43
}
51
-
52
- impl < ' a , A , B > Future for CopyBidirectional < ' a , A , B >
53
- where
54
- A : AsyncRead + AsyncWrite + Unpin + ?Sized ,
55
- B : AsyncRead + AsyncWrite + Unpin + ?Sized ,
56
- {
57
- type Output = io:: Result < ( u64 , u64 ) > ;
58
-
59
- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
60
- // Unpack self into mut refs to each field to avoid borrow check issues.
61
- let CopyBidirectional {
62
- a,
63
- b,
64
- a_to_b,
65
- b_to_a,
66
- } = & mut * self ;
67
-
68
- let a_to_b = transfer_one_direction ( cx, a_to_b, & mut * a, & mut * b) ?;
69
- let b_to_a = transfer_one_direction ( cx, b_to_a, & mut * b, & mut * a) ?;
70
-
71
- // It is not a problem if ready! returns early because transfer_one_direction for the
72
- // other direction will keep returning TransferState::Done(count) in future calls to poll
73
- let a_to_b = ready ! ( a_to_b) ;
74
- let b_to_a = ready ! ( b_to_a) ;
75
-
76
- Poll :: Ready ( Ok ( ( a_to_b, b_to_a) ) )
77
- }
78
- }
79
-
80
44
/// Copies data in both directions between `a` and `b`.
81
45
///
82
46
/// This function returns a future that will read from both streams,
@@ -110,11 +74,18 @@ where
110
74
A : AsyncRead + AsyncWrite + Unpin + ?Sized ,
111
75
B : AsyncRead + AsyncWrite + Unpin + ?Sized ,
112
76
{
113
- CopyBidirectional {
114
- a,
115
- b,
116
- a_to_b : TransferState :: Running ( CopyBuffer :: new ( ) ) ,
117
- b_to_a : TransferState :: Running ( CopyBuffer :: new ( ) ) ,
118
- }
77
+ let mut a_to_b = TransferState :: Running ( CopyBuffer :: new ( ) ) ;
78
+ let mut b_to_a = TransferState :: Running ( CopyBuffer :: new ( ) ) ;
79
+ poll_fn ( |cx| {
80
+ let a_to_b = transfer_one_direction ( cx, & mut a_to_b, a, b) ?;
81
+ let b_to_a = transfer_one_direction ( cx, & mut b_to_a, b, a) ?;
82
+
83
+ // It is not a problem if ready! returns early because transfer_one_direction for the
84
+ // other direction will keep returning TransferState::Done(count) in future calls to poll
85
+ let a_to_b = ready ! ( a_to_b) ;
86
+ let b_to_a = ready ! ( b_to_a) ;
87
+
88
+ Poll :: Ready ( Ok ( ( a_to_b, b_to_a) ) )
89
+ } )
119
90
. await
120
91
}
0 commit comments