File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,35 @@ pub fn split<T: AsyncRead + AsyncWrite>(stream: T) -> (ReadHalf<T>, WriteHalf<T>
16
16
#[ derive( Debug ) ]
17
17
pub struct ReadHalf < T > ( Arc < Mutex < T > > ) ;
18
18
19
+ impl < T > ReadHalf < T > {
20
+ /// Reunites with a previously split [`WriteHalf`].
21
+ ///
22
+ /// # Panics
23
+ ///
24
+ /// If this [`ReadHalf`] and the given [`WriteHalf`] do not originate from
25
+ /// the same [`split`] operation this method will panic.
26
+ /// This can be checked ahead of time by comparing the stored pointer
27
+ /// of the two halves.
28
+ #[ track_caller]
29
+ pub fn unsplit ( self , w : WriteHalf < T > ) -> T
30
+ where
31
+ T : Unpin ,
32
+ {
33
+ if Arc :: ptr_eq ( & self . 0 , & w. 0 ) {
34
+ drop ( w) ;
35
+ let inner = Arc :: try_unwrap ( self . 0 ) . expect ( "`Arc::try_unwrap` failed" ) ;
36
+ inner. into_inner ( )
37
+ } else {
38
+ #[ cold]
39
+ fn panic_unrelated ( ) -> ! {
40
+ panic ! ( "Unrelated `WriteHalf` passed to `ReadHalf::unsplit`." )
41
+ }
42
+
43
+ panic_unrelated ( )
44
+ }
45
+ }
46
+ }
47
+
19
48
impl < T : AsyncRead > AsyncRead for ReadHalf < T > {
20
49
async fn read < B : IoBufMut > ( & mut self , buf : B ) -> BufResult < usize , B > {
21
50
self . 0 . lock ( ) . await . read ( buf) . await
You can’t perform that action at this time.
0 commit comments