Move BufReader up the stack in Stream.

Stream now has an `Inner` enum, and wraps an instance of that enum in a
BufReader. This allows Stream itself to implement BufRead trivially, and
simplify some of the match dispatching. Having Stream implement BufRead
means we can make use of `read_line` instead of our own `read_next_line`
(not done in this PR yet).

Also, removes the `Cursor` variant of the Inner enum in favor of using
the `Test` variant everywhere, since it's strictly more powerful.
This commit is contained in:
Jacob Hoffman-Andrews
2020-11-28 12:04:28 -08:00
parent a0b88926fa
commit 131a0264d1
4 changed files with 86 additions and 93 deletions

View File

@@ -3,7 +3,7 @@ use crate::stream::Stream;
use crate::unit::Unit;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::io::{Cursor, Write};
use std::io::Write;
use std::sync::{Arc, Mutex};
mod agent_test;
@@ -29,7 +29,7 @@ where
}
#[allow(clippy::write_with_newline)]
pub fn make_response(
pub(crate) fn make_response(
status: u16,
status_text: &str,
headers: Vec<&str>,
@@ -42,9 +42,7 @@ pub fn make_response(
}
write!(&mut buf, "\r\n").ok();
buf.append(&mut body);
let cursor = Cursor::new(buf);
let write: Vec<u8> = vec![];
Ok(Stream::Test(Box::new(cursor), write))
Ok(Stream::from_vec(buf))
}
pub(crate) fn resolve_handler(unit: &Unit) -> Result<Stream, Error> {