From: Mark Thom Date: Sat, 8 Aug 2020 19:11:09 +0000 (-0600) Subject: fix bugs in PausedPrologStream (#661) X-Git-Tag: v0.9.0~174^2~16 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=6e5d2d6a36dcc1c4cad68c4f9180b8a8339137d6;p=scryer-prolog.git fix bugs in PausedPrologStream (#661) --- diff --git a/src/machine/streams.rs b/src/machine/streams.rs index 7e07067a..1375cb65 100644 --- a/src/machine/streams.rs +++ b/src/machine/streams.rs @@ -6,7 +6,7 @@ use crate::machine::machine_errors::*; use crate::machine::machine_indices::*; use crate::machine::machine_state::*; -use std::cmp::{min, Ordering}; +use std::cmp::Ordering; use std::cell::RefCell; use std::error::Error; use std::fmt; @@ -110,7 +110,7 @@ fn parser_top_to_bytes(mut buf: Vec>) -> io::Result> { /* all these streams are closed automatically when the instance is * dropped. */ -pub enum StreamInstance { +enum StreamInstance { Bytes(Cursor>), InputFile(ClauseName, File), OutputFile(ClauseName, File, bool), // File, append. @@ -129,16 +129,20 @@ impl StreamInstance { StreamInstance::PausedPrologStream(ref mut put_back, ref mut stream) => { let mut index = 0; - while index < min(buf.len(), put_back.len()) { - let b = put_back.pop().unwrap(); - buf[index] = b; - index += 1; + while index < buf.len() { + if let Some(b) = put_back.pop() { + buf[index] = b; + index += 1; + } else { + break; + } } if index == buf.len() { Ok(buf.len()) } else { stream.read(&mut buf[index ..]) + .map(|bytes_read| bytes_read + index) } } StreamInstance::InputFile(_, ref mut file) => {