Last active
November 12, 2016 01:27
-
-
Save allengeorge/038a66733d3d126458310413e0e52f56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl<T: Transport> BufferedTransport<T> { | |
fn get_bytes(&mut self) -> io::Result<&[u8]> { | |
if self.rpos == self.rbuf.len() { | |
self.rpos = 0; | |
self.rcap = try!(self.underlying.read(&mut self.rbuf)); | |
} | |
Ok(&self.rbuf[self.rpos..self.rcap]) | |
} | |
fn consume(&mut self, consumed: usize) { | |
self.rpos += cmp::min(self.rcap, self.rpos + consumed); | |
} | |
} | |
impl<T: TTransport> io::Read for TBufferedTransport<T> { | |
fn read(&mut self, b: &mut [u8]) -> io::Result<usize> { | |
let mut bytes_read = 0; | |
loop { | |
let buf = try!(self.get_bytes()); | |
let mut copyable = 0; | |
let remaining = b.len() - bytes_read; | |
copyable = cmp::min(remaining, buf.len()); | |
b[bytes_read..].copy_from_slice(&buf[0..copyable]); | |
bytes_read += copyable; | |
self.consume(copyable); | |
if bytes_read == b.len() || copyable == 0 { | |
break | |
} | |
} | |
Ok(bytes_read) | |
} | |
} |
Author
allengeorge
commented
Nov 12, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment