futures_codec/
fuse.rs

1use futures::io::{AsyncRead, AsyncWrite};
2use pin_project::pin_project;
3use std::io::Error;
4use std::marker::Unpin;
5use std::ops::{Deref, DerefMut};
6use std::pin::Pin;
7use std::task::{Context, Poll};
8
9#[pin_project]
10#[derive(Debug)]
11pub(crate) struct Fuse<T, U> {
12    #[pin]
13    pub t: T,
14    pub u: U,
15}
16
17impl<T, U> Fuse<T, U> {
18    pub(crate) fn new(t: T, u: U) -> Self {
19        Self { t, u }
20    }
21}
22
23impl<T, U> Deref for Fuse<T, U> {
24    type Target = T;
25
26    fn deref(&self) -> &T {
27        &self.t
28    }
29}
30
31impl<T, U> DerefMut for Fuse<T, U> {
32    fn deref_mut(&mut self) -> &mut T {
33        &mut self.t
34    }
35}
36
37impl<T: AsyncRead + Unpin, U> AsyncRead for Fuse<T, U> {
38    fn poll_read(
39        self: Pin<&mut Self>,
40        cx: &mut Context<'_>,
41        buf: &mut [u8],
42    ) -> Poll<Result<usize, Error>> {
43        self.project().t.poll_read(cx, buf)
44    }
45}
46
47impl<T: AsyncWrite + Unpin, U> AsyncWrite for Fuse<T, U> {
48    fn poll_write(
49        self: Pin<&mut Self>,
50        cx: &mut Context,
51        buf: &[u8],
52    ) -> Poll<Result<usize, Error>> {
53        self.project().t.poll_write(cx, buf)
54    }
55    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Error>> {
56        self.project().t.poll_flush(cx)
57    }
58    fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Error>> {
59        self.project().t.poll_close(cx)
60    }
61}