tonic/transport/channel/service/
io.rs

1use std::io::{self, IoSlice};
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use hyper::rt;
6use hyper_util::client::legacy::connect::{Connected as HyperConnected, Connection};
7
8pub(in crate::transport) trait Io:
9    rt::Read + rt::Write + Send + 'static
10{
11}
12
13impl<T> Io for T where T: rt::Read + rt::Write + Send + 'static {}
14
15pub(crate) struct BoxedIo(Pin<Box<dyn Io>>);
16
17impl BoxedIo {
18    pub(in crate::transport) fn new<I: Io>(io: I) -> Self {
19        BoxedIo(Box::pin(io))
20    }
21}
22
23impl Connection for BoxedIo {
24    fn connected(&self) -> HyperConnected {
25        HyperConnected::new()
26    }
27}
28
29impl rt::Read for BoxedIo {
30    fn poll_read(
31        mut self: Pin<&mut Self>,
32        cx: &mut Context<'_>,
33        buf: rt::ReadBufCursor<'_>,
34    ) -> Poll<io::Result<()>> {
35        Pin::new(&mut self.0).poll_read(cx, buf)
36    }
37}
38
39impl rt::Write for BoxedIo {
40    fn poll_write(
41        mut self: Pin<&mut Self>,
42        cx: &mut Context<'_>,
43        buf: &[u8],
44    ) -> Poll<io::Result<usize>> {
45        Pin::new(&mut self.0).poll_write(cx, buf)
46    }
47
48    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
49        Pin::new(&mut self.0).poll_flush(cx)
50    }
51
52    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
53        Pin::new(&mut self.0).poll_shutdown(cx)
54    }
55
56    fn poll_write_vectored(
57        mut self: Pin<&mut Self>,
58        cx: &mut Context<'_>,
59        bufs: &[IoSlice<'_>],
60    ) -> Poll<Result<usize, io::Error>> {
61        Pin::new(&mut self.0).poll_write_vectored(cx, bufs)
62    }
63
64    fn is_write_vectored(&self) -> bool {
65        self.0.is_write_vectored()
66    }
67}