asynchronous_codec/
fuse.rs

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