Coverage Report

Created: 2025-02-21 07:11

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