1 | use crate::lock::BiLock; |
2 | use core::fmt; |
3 | use futures_core::ready; |
4 | use futures_core::task::{Context, Poll}; |
5 | use futures_io::{AsyncRead, AsyncWrite, IoSlice, IoSliceMut}; |
6 | use std::io; |
7 | use std::pin::Pin; |
8 | |
9 | /// The readable half of an object returned from `AsyncRead::split`. |
10 | #[derive (Debug)] |
11 | pub struct ReadHalf<T> { |
12 | handle: BiLock<T>, |
13 | } |
14 | |
15 | /// The writable half of an object returned from `AsyncRead::split`. |
16 | #[derive (Debug)] |
17 | pub struct WriteHalf<T> { |
18 | handle: BiLock<T>, |
19 | } |
20 | |
21 | fn lock_and_then<T, U, E, F>(lock: &BiLock<T>, cx: &mut Context<'_>, f: F) -> Poll<Result<U, E>> |
22 | where |
23 | F: FnOnce(Pin<&mut T>, &mut Context<'_>) -> Poll<Result<U, E>>, |
24 | { |
25 | let mut l: BiLockGuard<'_, T> = ready!(lock.poll_lock(cx)); |
26 | f(l.as_pin_mut(), cx) |
27 | } |
28 | |
29 | pub(super) fn split<T: AsyncRead + AsyncWrite>(t: T) -> (ReadHalf<T>, WriteHalf<T>) { |
30 | let (a: BiLock, b: BiLock) = BiLock::new(t); |
31 | (ReadHalf { handle: a }, WriteHalf { handle: b }) |
32 | } |
33 | |
34 | impl<T: Unpin> ReadHalf<T> { |
35 | /// Attempts to put the two "halves" of a split `AsyncRead + AsyncWrite` back |
36 | /// together. Succeeds only if the `ReadHalf<T>` and `WriteHalf<T>` are |
37 | /// a matching pair originating from the same call to `AsyncReadExt::split`. |
38 | pub fn reunite(self, other: WriteHalf<T>) -> Result<T, ReuniteError<T>> { |
39 | self.handle |
40 | .reunite(other.handle) |
41 | .map_err(|err: ReuniteError| ReuniteError(ReadHalf { handle: err.0 }, WriteHalf { handle: err.1 })) |
42 | } |
43 | } |
44 | |
45 | impl<T: Unpin> WriteHalf<T> { |
46 | /// Attempts to put the two "halves" of a split `AsyncRead + AsyncWrite` back |
47 | /// together. Succeeds only if the `ReadHalf<T>` and `WriteHalf<T>` are |
48 | /// a matching pair originating from the same call to `AsyncReadExt::split`. |
49 | pub fn reunite(self, other: ReadHalf<T>) -> Result<T, ReuniteError<T>> { |
50 | other.reunite(self) |
51 | } |
52 | } |
53 | |
54 | impl<R: AsyncRead> AsyncRead for ReadHalf<R> { |
55 | fn poll_read( |
56 | self: Pin<&mut Self>, |
57 | cx: &mut Context<'_>, |
58 | buf: &mut [u8], |
59 | ) -> Poll<io::Result<usize>> { |
60 | lock_and_then(&self.handle, cx, |l: Pin<&mut R>, cx: &mut Context<'_>| l.poll_read(cx, buf)) |
61 | } |
62 | |
63 | fn poll_read_vectored( |
64 | self: Pin<&mut Self>, |
65 | cx: &mut Context<'_>, |
66 | bufs: &mut [IoSliceMut<'_>], |
67 | ) -> Poll<io::Result<usize>> { |
68 | lock_and_then(&self.handle, cx, |l: Pin<&mut R>, cx: &mut Context<'_>| l.poll_read_vectored(cx, bufs)) |
69 | } |
70 | } |
71 | |
72 | impl<W: AsyncWrite> AsyncWrite for WriteHalf<W> { |
73 | fn poll_write( |
74 | self: Pin<&mut Self>, |
75 | cx: &mut Context<'_>, |
76 | buf: &[u8], |
77 | ) -> Poll<io::Result<usize>> { |
78 | lock_and_then(&self.handle, cx, |l, cx| l.poll_write(cx, buf)) |
79 | } |
80 | |
81 | fn poll_write_vectored( |
82 | self: Pin<&mut Self>, |
83 | cx: &mut Context<'_>, |
84 | bufs: &[IoSlice<'_>], |
85 | ) -> Poll<io::Result<usize>> { |
86 | lock_and_then(&self.handle, cx, |l, cx| l.poll_write_vectored(cx, bufs)) |
87 | } |
88 | |
89 | fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
90 | lock_and_then(&self.handle, cx, |l, cx| l.poll_flush(cx)) |
91 | } |
92 | |
93 | fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
94 | lock_and_then(&self.handle, cx, |l, cx| l.poll_close(cx)) |
95 | } |
96 | } |
97 | |
98 | /// Error indicating a `ReadHalf<T>` and `WriteHalf<T>` were not two halves |
99 | /// of a `AsyncRead + AsyncWrite`, and thus could not be `reunite`d. |
100 | pub struct ReuniteError<T>(pub ReadHalf<T>, pub WriteHalf<T>); |
101 | |
102 | impl<T> fmt::Debug for ReuniteError<T> { |
103 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
104 | f.debug_tuple(name:"ReuniteError" ).field(&"..." ).finish() |
105 | } |
106 | } |
107 | |
108 | impl<T> fmt::Display for ReuniteError<T> { |
109 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
110 | write!(f, "tried to reunite a ReadHalf and WriteHalf that don't form a pair" ) |
111 | } |
112 | } |
113 | |
114 | #[cfg (feature = "std" )] |
115 | impl<T: core::any::Any> std::error::Error for ReuniteError<T> {} |
116 | |