(&mut self)
| 27 | } |
| 28 | |
| 29 | pub(crate) fn read<T: DeserializeOwned>(&mut self) -> Result<T, Error> { |
| 30 | let newline = self.buf.iter().position(|&b| b == b'\n'); |
| 31 | if let Some(idx) = newline { |
| 32 | let t = from_slice(&self.buf[..idx]); |
| 33 | self.buf.drain(..idx+1); |
| 34 | return Ok(t?); |
| 35 | } |
| 36 | |
| 37 | let mut buf: [u8; 256] = unsafe { mem::uninitialized() }; |
| 38 | |
| 39 | loop { |
| 40 | let len = self.stream.read(&mut buf[..]); |
| 41 | match len { |
| 42 | Ok(len) => { |
| 43 | if len == 0 { |
| 44 | thread::sleep(Duration::new(0, 1_000_000)); |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | let newline = buf[..len].iter().position(|&b| b == b'\n'); |
| 49 | if let Some(idx) = newline { |
| 50 | self.buf.extend(buf[..idx].iter()); |
| 51 | let t = from_slice(&self.buf[..]); |
| 52 | self.buf.clear(); |
| 53 | self.buf.extend(buf[idx+1..len].iter()); |
| 54 | return Ok(t?); |
| 55 | } else { |
| 56 | // no data; continue |
| 57 | self.buf.extend(buf[..len].iter()) |
| 58 | } |
| 59 | }, |
| 60 | Err(e) => { |
| 61 | if e.kind() == ErrorKind::Interrupted { |
| 62 | continue; |
| 63 | } else { |
| 64 | Err(e)? |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | pub(crate) fn write<T: Serialize>(&mut self, value: &T) -> Result<(), Error> { |
| 72 | to_writer(&mut self.stream, value)?; |
no outgoing calls
no test coverage detected