@@ -15,6 +15,13 @@ pub struct SplitStream<S>(BiLock<S>);
15
15
16
16
impl < S > Unpin for SplitStream < S > { }
17
17
18
+ impl < S > SplitStream < S > {
19
+ /// Returns `true` if the `SplitStream<S>` and `SplitSink<S>` originate from the same call to `StreamExt::split`.
20
+ pub fn is_pair_of < Item > ( & self , other : & SplitSink < S , Item > ) -> bool {
21
+ other. is_pair_of ( & self )
22
+ }
23
+ }
24
+
18
25
impl < S : Unpin > SplitStream < S > {
19
26
/// Attempts to put the two "halves" of a split `Stream + Sink` back
20
27
/// together. Succeeds only if the `SplitStream<S>` and `SplitSink<S>` are
@@ -60,6 +67,13 @@ impl<S: Sink<Item> + Unpin, Item> SplitSink<S, Item> {
60
67
}
61
68
}
62
69
70
+ impl < S , Item > SplitSink < S , Item > {
71
+ /// Returns `true` if the `SplitStream<S>` and `SplitSink<S>` originate from the same call to `StreamExt::split`.
72
+ pub fn is_pair_of ( & self , other : & SplitStream < S > ) -> bool {
73
+ self . lock . is_pair_of ( & other. 0 )
74
+ }
75
+ }
76
+
63
77
impl < S : Sink < Item > , Item > SplitSink < S , Item > {
64
78
fn poll_flush_slot (
65
79
mut inner : Pin < & mut S > ,
@@ -142,3 +156,69 @@ impl<T, Item> fmt::Display for ReuniteError<T, Item> {
142
156
143
157
#[ cfg( feature = "std" ) ]
144
158
impl < T : core:: any:: Any , Item > std:: error:: Error for ReuniteError < T , Item > { }
159
+
160
+ #[ cfg( test) ]
161
+ mod tests {
162
+ use super :: * ;
163
+ use crate :: { sink:: Sink , stream:: StreamExt } ;
164
+ use core:: marker:: PhantomData ;
165
+
166
+ struct NopStream < Item > {
167
+ phantom : PhantomData < Item > ,
168
+ }
169
+
170
+ impl < Item > Stream for NopStream < Item > {
171
+ type Item = Item ;
172
+
173
+ fn poll_next ( self : Pin < & mut Self > , _cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
174
+ todo ! ( )
175
+ }
176
+ }
177
+
178
+ impl < Item > Sink < Item > for NopStream < Item > {
179
+ type Error = ( ) ;
180
+
181
+ fn poll_ready (
182
+ self : Pin < & mut Self > ,
183
+ _cx : & mut Context < ' _ > ,
184
+ ) -> Poll < Result < ( ) , Self :: Error > > {
185
+ todo ! ( )
186
+ }
187
+
188
+ fn start_send ( self : Pin < & mut Self > , _item : Item ) -> Result < ( ) , Self :: Error > {
189
+ todo ! ( )
190
+ }
191
+
192
+ fn poll_flush (
193
+ self : Pin < & mut Self > ,
194
+ _cx : & mut Context < ' _ > ,
195
+ ) -> Poll < Result < ( ) , Self :: Error > > {
196
+ todo ! ( )
197
+ }
198
+
199
+ fn poll_close (
200
+ self : Pin < & mut Self > ,
201
+ _cx : & mut Context < ' _ > ,
202
+ ) -> Poll < Result < ( ) , Self :: Error > > {
203
+ todo ! ( )
204
+ }
205
+ }
206
+
207
+ #[ test]
208
+ fn test_pairing ( ) {
209
+ let s1 = NopStream :: < ( ) > { phantom : PhantomData } ;
210
+ let ( sink1, stream1) = s1. split ( ) ;
211
+ assert ! ( sink1. is_pair_of( & stream1) ) ;
212
+ assert ! ( stream1. is_pair_of( & sink1) ) ;
213
+
214
+ let s2 = NopStream :: < ( ) > { phantom : PhantomData } ;
215
+ let ( sink2, stream2) = s2. split ( ) ;
216
+ assert ! ( sink2. is_pair_of( & stream2) ) ;
217
+ assert ! ( stream2. is_pair_of( & sink2) ) ;
218
+
219
+ assert ! ( !sink1. is_pair_of( & stream2) ) ;
220
+ assert ! ( !stream1. is_pair_of( & sink2) ) ;
221
+ assert ! ( !sink2. is_pair_of( & stream1) ) ;
222
+ assert ! ( !stream2. is_pair_of( & sink1) ) ;
223
+ }
224
+ }
0 commit comments