• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::net::TcpStream;
2 
3 use crate::Connection;
4 
5 impl Connection for TcpStream {
6     type Error = std::io::Error;
7 
read(&mut self) -> Result<u8, Self::Error>8     fn read(&mut self) -> Result<u8, Self::Error> {
9         use std::io::Read;
10 
11         self.set_nonblocking(false)?;
12 
13         let mut buf = [0u8];
14         match Read::read_exact(self, &mut buf) {
15             Ok(_) => Ok(buf[0]),
16             Err(e) => Err(e),
17         }
18     }
19 
read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>20     fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
21         use std::io::Read;
22 
23         self.set_nonblocking(false)?;
24 
25         Read::read_exact(self, buf)
26     }
27 
peek(&mut self) -> Result<Option<u8>, Self::Error>28     fn peek(&mut self) -> Result<Option<u8>, Self::Error> {
29         self.set_nonblocking(true)?;
30 
31         let mut buf = [0u8];
32         match Self::peek(self, &mut buf) {
33             Ok(_) => Ok(Some(buf[0])),
34             Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => Ok(None),
35             Err(e) => Err(e),
36         }
37     }
38 
write(&mut self, byte: u8) -> Result<(), Self::Error>39     fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
40         use std::io::Write;
41 
42         Write::write_all(self, &[byte])
43     }
44 
write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>45     fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
46         use std::io::Write;
47 
48         Write::write_all(self, buf)
49     }
50 
flush(&mut self) -> Result<(), Self::Error>51     fn flush(&mut self) -> Result<(), Self::Error> {
52         use std::io::Write;
53 
54         Write::flush(self)
55     }
56 
on_session_start(&mut self) -> Result<(), Self::Error>57     fn on_session_start(&mut self) -> Result<(), Self::Error> {
58         // see issue #28
59         self.set_nodelay(true)
60     }
61 }
62