Skip to main content

tfserver/structures/
temp_transport.rs

1use std::io;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
5use crate::structures::transport::AsyncReadWrite;
6
7///A container, that can store reference of the stream, and be used inside other structures, that does not accepts the references.
8pub struct TempTransport<'a, T: AsyncReadWrite> {
9    base_transport: &'a mut T,
10}
11
12impl<'a, T: AsyncReadWrite> TempTransport<'a, T> {
13    pub fn new(transport: &'a mut T) -> Self {
14        TempTransport {
15            base_transport: transport,
16        }
17    }
18}
19
20impl<'a, T: AsyncReadWrite> AsyncRead for TempTransport<'a, T> {
21    fn poll_read(
22        mut self: Pin<&mut Self>,
23        cx: &mut Context<'_>,
24        buf: &mut ReadBuf<'_>,
25    ) -> Poll<io::Result<()>> {
26        Pin::new(&mut *self.base_transport).poll_read(cx, buf)
27    }
28}
29
30impl<'a, T: AsyncReadWrite> AsyncWrite for TempTransport<'a, T> {
31    fn poll_write(
32        mut self: Pin<&mut Self>,
33        cx: &mut Context<'_>,
34        buf: &[u8],
35    ) -> Poll<io::Result<usize>> {
36        Pin::new(&mut *self.base_transport).poll_write(cx, buf)
37    }
38
39    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
40        Pin::new(&mut *self.base_transport).poll_flush(cx)
41    }
42
43    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
44        Pin::new(&mut *self.base_transport).poll_shutdown(cx)
45    }
46}