async_rs/util/
io.rs

1use crate::sys::AsSysFd;
2use std::{
3    fmt,
4    io::{self, IoSlice, IoSliceMut, Read, Write},
5};
6
7/// A synchronous IO handle
8pub struct IOHandle<H: Read + Write + AsSysFd + Send + 'static>(pub(crate) H);
9
10impl<H: Read + Write + AsSysFd + Send + 'static> IOHandle<H> {
11    /// Instantiate a new IO handle
12    pub fn new(io: H) -> Self {
13        Self(io)
14    }
15}
16
17impl<H: Read + Write + AsSysFd + Send + 'static> Read for IOHandle<H> {
18    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
19        self.0.read(buf)
20    }
21
22    fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
23        self.0.read_vectored(bufs)
24    }
25
26    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
27        self.0.read_to_end(buf)
28    }
29
30    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
31        self.0.read_to_string(buf)
32    }
33
34    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
35        self.0.read_exact(buf)
36    }
37}
38
39impl<H: Read + Write + AsSysFd + Send + 'static> Write for IOHandle<H> {
40    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
41        self.0.write(buf)
42    }
43
44    fn flush(&mut self) -> io::Result<()> {
45        self.0.flush()
46    }
47
48    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
49        self.0.write_vectored(bufs)
50    }
51
52    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
53        self.0.write_all(buf)
54    }
55
56    fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
57        self.0.write_fmt(fmt)
58    }
59}
60
61impl<H: Read + Write + AsSysFd + Send + 'static> fmt::Debug for IOHandle<H> {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        f.debug_tuple("IOHandle").finish()
64    }
65}
66
67#[allow(unsafe_code)]
68#[cfg(feature = "async-io")]
69unsafe impl<H: Read + Write + AsSysFd + Send + 'static> async_io::IoSafe for IOHandle<H> {}