convert to using Cow

This commit is contained in:
Sean McArthur
2014-11-27 12:02:59 -08:00
parent c77cd3d3da
commit 4263a01f1c
3 changed files with 49 additions and 37 deletions

View File

@@ -1,9 +1,10 @@
//! Pieces pertaining to the HTTP message protocol.
use std::borrow::{Borrowed, Owned};
use std::cmp::min;
use std::fmt;
use std::io::{mod, Reader, IoResult, BufWriter};
use std::num::from_u16;
use std::str::{mod, SendStr, Slice, Owned};
use std::str::{mod, SendStr};
use url::Url;
@@ -559,9 +560,15 @@ pub fn read_request_line<R: Reader>(stream: &mut R) -> HttpResult<RequestLine> {
pub type StatusLine = (HttpVersion, RawStatus);
/// The raw status code and reason-phrase.
#[deriving(PartialEq, Show, Clone)]
#[deriving(PartialEq, Show)]
pub struct RawStatus(pub u16, pub SendStr);
impl Clone for RawStatus {
fn clone(&self) -> RawStatus {
RawStatus(self.0, (*self.1).clone().into_cow())
}
}
/// Read the StatusLine, such as `HTTP/1.1 200 OK`.
///
/// > The first line of a response message is the status-line, consisting
@@ -632,7 +639,7 @@ pub fn read_status<R: Reader>(stream: &mut R) -> HttpResult<RawStatus> {
Some(status) => match status.canonical_reason() {
Some(phrase) => {
if phrase == reason {
Slice(phrase)
Borrowed(phrase)
} else {
Owned(reason.into_string())
}
@@ -657,7 +664,7 @@ fn expect(r: IoResult<u8>, expected: u8) -> HttpResult<()> {
#[cfg(test)]
mod tests {
use std::io::{mod, MemReader, MemWriter};
use std::str::{Slice, Owned};
use std::borrow::{Borrowed, Owned};
use test::Bencher;
use uri::RequestUri;
use uri::RequestUri::{Star, AbsoluteUri, AbsolutePath, Authority};
@@ -727,8 +734,8 @@ mod tests {
assert_eq!(read_status(&mut mem(s)), result);
}
read("200 OK\r\n", Ok(RawStatus(200, Slice("OK"))));
read("404 Not Found\r\n", Ok(RawStatus(404, Slice("Not Found"))));
read("200 OK\r\n", Ok(RawStatus(200, Borrowed("OK"))));
read("404 Not Found\r\n", Ok(RawStatus(404, Borrowed("Not Found"))));
read("200 crazy pants\r\n", Ok(RawStatus(200, Owned("crazy pants".to_string()))));
}
@@ -774,7 +781,7 @@ mod tests {
#[bench]
fn bench_read_status(b: &mut Bencher) {
b.bytes = b"404 Not Found\r\n".len() as u64;
b.iter(|| assert_eq!(read_status(&mut mem("404 Not Found\r\n")), Ok(RawStatus(404, Slice("Not Found")))));
b.iter(|| assert_eq!(read_status(&mut mem("404 Not Found\r\n")), Ok(RawStatus(404, Borrowed("Not Found")))));
}
}