Coverage Report

Created: 2026-02-14 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.21/src/io/split.rs
Line
Count
Source
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
{
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: 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
0
    pub fn reunite(self, other: WriteHalf<T>) -> Result<T, ReuniteError<T>> {
39
0
        self.handle
40
0
            .reunite(other.handle)
41
0
            .map_err(|err| ReuniteError(ReadHalf { handle: err.0 }, WriteHalf { handle: err.1 }))
42
0
    }
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
0
    pub fn reunite(self, other: ReadHalf<T>) -> Result<T, ReuniteError<T>> {
50
0
        other.reunite(self)
51
0
    }
52
}
53
54
impl<R: AsyncRead> AsyncRead for ReadHalf<R> {
55
0
    fn poll_read(
56
0
        self: Pin<&mut Self>,
57
0
        cx: &mut Context<'_>,
58
0
        buf: &mut [u8],
59
0
    ) -> Poll<io::Result<usize>> {
60
0
        lock_and_then(&self.handle, cx, |l, cx| l.poll_read(cx, buf))
61
0
    }
62
63
0
    fn poll_read_vectored(
64
0
        self: Pin<&mut Self>,
65
0
        cx: &mut Context<'_>,
66
0
        bufs: &mut [IoSliceMut<'_>],
67
0
    ) -> Poll<io::Result<usize>> {
68
0
        lock_and_then(&self.handle, cx, |l, cx| l.poll_read_vectored(cx, bufs))
69
0
    }
70
}
71
72
impl<W: AsyncWrite> AsyncWrite for WriteHalf<W> {
73
0
    fn poll_write(
74
0
        self: Pin<&mut Self>,
75
0
        cx: &mut Context<'_>,
76
0
        buf: &[u8],
77
0
    ) -> Poll<io::Result<usize>> {
78
0
        lock_and_then(&self.handle, cx, |l, cx| l.poll_write(cx, buf))
79
0
    }
80
81
0
    fn poll_write_vectored(
82
0
        self: Pin<&mut Self>,
83
0
        cx: &mut Context<'_>,
84
0
        bufs: &[IoSlice<'_>],
85
0
    ) -> Poll<io::Result<usize>> {
86
0
        lock_and_then(&self.handle, cx, |l, cx| l.poll_write_vectored(cx, bufs))
87
0
    }
88
89
0
    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
90
0
        lock_and_then(&self.handle, cx, |l, cx| l.poll_flush(cx))
91
0
    }
92
93
0
    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
94
0
        lock_and_then(&self.handle, cx, |l, cx| l.poll_close(cx))
95
0
    }
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
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104
0
        f.debug_tuple("ReuniteError").field(&"...").finish()
105
0
    }
106
}
107
108
impl<T> fmt::Display for ReuniteError<T> {
109
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110
0
        write!(f, "tried to reunite a ReadHalf and WriteHalf that don't form a pair")
111
0
    }
112
}
113
114
#[cfg(feature = "std")]
115
impl<T: core::any::Any> std::error::Error for ReuniteError<T> {}