feat(header): introduce header::Raw (#869)

The Raw type repesents the raw bytes of a header-value.

Having a special type allows a couple of benefits:

- The exact representation has become private, allowing "uglier"
internals. Specifically, since the common case is for a header to only
have 1 line of bytes, an enum is used to skip allocating a Vec for only
1 line. Additionally, a Cow<'static, [u8]> is used, so static bytes
don't require a copy. Finally, since we can use static bytes, when
parsing, we can compare the incoming bytes against a couple of the most
common header-values, and possibly remove another copy.

- As its own type, the `Headers.set_raw` method can be generic over
`Into<Raw>`, which allows for more ergnomic method calls.

BREAKING CHANGE: `Header::parse_header` now receives `&Raw`, instead of
  a `&[Vec<u8>]`. `Raw` provides several methods to ease using it, but
  may require some changes to existing code.
This commit is contained in:
Sean McArthur
2016-07-23 12:54:16 -07:00
committed by GitHub
parent d67dbc6028
commit 50ccdaa7e7
27 changed files with 489 additions and 245 deletions

View File

@@ -3,7 +3,7 @@ use std::str;
use unicase::UniCase;
use header::{Header};
use header::{Header, Raw};
/// The `Expect` header.
///
@@ -34,16 +34,15 @@ impl Header for Expect {
NAME
}
fn parse_header(raw: &[Vec<u8>]) -> ::Result<Expect> {
if raw.len() == 1 {
fn parse_header(raw: &Raw) -> ::Result<Expect> {
if let Some(line) = raw.one() {
let text = unsafe {
// safe because:
// 1. we just checked raw.len == 1
// 2. we don't actually care if it's utf8, we just want to
// 1. we don't actually care if it's utf8, we just want to
// compare the bytes with the "case" normalized. If it's not
// utf8, then the byte comparison will fail, and we'll return
// None. No big deal.
str::from_utf8_unchecked(raw.get_unchecked(0))
str::from_utf8_unchecked(line)
};
if UniCase(text) == EXPECT_CONTINUE {
Ok(Expect::Continue)