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