/// An asynchronous reader. /// /// This acts as any other stream, with the exception that reading from it won't block. Instead, /// the buffer will only be partially updated based on how much the internal buffer holds. pub struct AsyncReader { recv: mpsc::Receiver>, } impl Read for AsyncReader { /// Read from the byte stream. /// /// This will never block, but try to drain the event queue until empty. If the total number of /// bytes written is lower than the buffer's length, the event queue is empty or that the event /// stream halted. fn read(&mut self, buf: &mut [u8]) -> io::Result { let mut total = 0; loop { if total >= buf.len() { break; } match self.recv.try_recv() { Ok(Ok(b)) => { buf[total] = b; total += 1; } Ok(Err(e)) => return Err(e), Err(_) => break, } } Ok(total) } } impl AsyncReader { pub fn new(reader: R) -> Self { let (send, recv) = mpsc::channel(); thread::spawn(move || { for i in reader.bytes() { if send.send(i).is_err() { return; } } }); Self { recv: recv } }