Skip to content

Commit 3dc8e7d

Browse files
committed
Merge pull request #416 from hyperium/into-cow
chore(stability): remove into_cow feature gate
2 parents 1b28515 + ccd4814 commit 3dc8e7d

File tree

4 files changed

+29
-108
lines changed

4 files changed

+29
-108
lines changed

src/header/mod.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
//! must implement the `Header` trait from this module. Several common headers
66
//! are already provided, such as `Host`, `ContentType`, `UserAgent`, and others.
77
use std::any::Any;
8-
use std::borrow::Cow::{Borrowed};
9-
use std::borrow::ToOwned;
8+
use std::borrow::{Cow, ToOwned};
109
use std::fmt;
11-
use std::raw::TraitObject;
1210
use std::collections::HashMap;
1311
use std::collections::hash_map::{Iter, Entry};
1412
use std::iter::{FromIterator, IntoIterator};
15-
use std::borrow::{Cow, IntoCow};
13+
use std::raw::TraitObject;
1614
use std::{mem, raw};
1715

1816
use httparse;
@@ -120,7 +118,7 @@ impl Headers {
120118
let mut headers = Headers::new();
121119
for header in raw {
122120
debug!("raw header: {:?}={:?}", header.name, &header.value[..]);
123-
let name = UniCase(header.name.to_owned().into_cow());
121+
let name = UniCase(Cow::Owned(header.name.to_owned()));
124122
let mut item = match headers.data.entry(name) {
125123
Entry::Vacant(entry) => entry.insert(Item::new_raw(vec![])),
126124
Entry::Occupied(entry) => entry.into_mut()
@@ -136,7 +134,7 @@ impl Headers {
136134
///
137135
/// The field is determined by the type of the value being set.
138136
pub fn set<H: Header + HeaderFormat>(&mut self, value: H) {
139-
self.data.insert(UniCase(Borrowed(header_name::<H>())),
137+
self.data.insert(UniCase(Cow::Borrowed(header_name::<H>())),
140138
Item::new_typed(Box::new(value)));
141139
}
142140

@@ -153,7 +151,7 @@ impl Headers {
153151
/// ```
154152
pub fn get_raw(&self, name: &str) -> Option<&[Vec<u8>]> {
155153
self.data
156-
.get(&UniCase(Borrowed(unsafe { mem::transmute::<&str, &str>(name) })))
154+
.get(&UniCase(Cow::Borrowed(unsafe { mem::transmute::<&str, &str>(name) })))
157155
.map(Item::raw)
158156
}
159157

@@ -166,23 +164,23 @@ impl Headers {
166164
/// # let mut headers = Headers::new();
167165
/// headers.set_raw("content-length", vec![b"5".to_vec()]);
168166
/// ```
169-
pub fn set_raw<K: IntoCow<'static, str>>(&mut self, name: K, value: Vec<Vec<u8>>) {
170-
self.data.insert(UniCase(name.into_cow()), Item::new_raw(value));
167+
pub fn set_raw<K: Into<Cow<'static, str>>>(&mut self, name: K, value: Vec<Vec<u8>>) {
168+
self.data.insert(UniCase(name.into()), Item::new_raw(value));
171169
}
172170

173171
/// Remove a header set by set_raw
174172
pub fn remove_raw(&mut self, name: &str) {
175-
self.data.remove(&UniCase(name.into_cow()));
173+
self.data.remove(&UniCase(Cow::Borrowed(name)));
176174
}
177175

178176
/// Get a reference to the header field's value, if it exists.
179177
pub fn get<H: Header + HeaderFormat>(&self) -> Option<&H> {
180-
self.data.get(&UniCase(Borrowed(header_name::<H>()))).and_then(Item::typed::<H>)
178+
self.data.get(&UniCase(Cow::Borrowed(header_name::<H>()))).and_then(Item::typed::<H>)
181179
}
182180

183181
/// Get a mutable reference to the header field's value, if it exists.
184182
pub fn get_mut<H: Header + HeaderFormat>(&mut self) -> Option<&mut H> {
185-
self.data.get_mut(&UniCase(Borrowed(header_name::<H>()))).and_then(Item::typed_mut::<H>)
183+
self.data.get_mut(&UniCase(Cow::Borrowed(header_name::<H>()))).and_then(Item::typed_mut::<H>)
186184
}
187185

188186
/// Returns a boolean of whether a certain header is in the map.
@@ -196,13 +194,13 @@ impl Headers {
196194
/// let has_type = headers.has::<ContentType>();
197195
/// ```
198196
pub fn has<H: Header + HeaderFormat>(&self) -> bool {
199-
self.data.contains_key(&UniCase(Borrowed(header_name::<H>())))
197+
self.data.contains_key(&UniCase(Cow::Borrowed(header_name::<H>())))
200198
}
201199

202200
/// Removes a header from the map, if one existed.
203201
/// Returns true if a header has been removed.
204202
pub fn remove<H: Header + HeaderFormat>(&mut self) -> bool {
205-
self.data.remove(&UniCase(Borrowed(header_name::<H>()))).is_some()
203+
self.data.remove(&UniCase(Cow::Borrowed(header_name::<H>()))).is_some()
206204
}
207205

208206
/// Returns an iterator over the header fields.
@@ -266,7 +264,7 @@ impl<'a> HeaderView<'a> {
266264
/// Check if a HeaderView is a certain Header.
267265
#[inline]
268266
pub fn is<H: Header>(&self) -> bool {
269-
UniCase(header_name::<H>().into_cow()) == *self.0
267+
UniCase(Cow::Borrowed(header_name::<H>())) == *self.0
270268
}
271269

272270
/// Get the Header name as a slice.

src/http.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//! Pieces pertaining to the HTTP message protocol.
2-
use std::borrow::{Cow, IntoCow, ToOwned};
2+
use std::borrow::{Cow, ToOwned};
33
use std::cmp::min;
44
use std::io::{self, Read, Write, BufRead};
5+
use std::num::FromPrimitive;
56

67
use httparse;
78

89
use buffer::BufReader;
910
use header::Headers;
1011
use method::Method;
12+
use status::StatusCode;
1113
use uri::RequestUri;
1214
use version::HttpVersion::{self, Http10, Http11};
1315
use HttpError:: HttpTooLargeError;
@@ -375,11 +377,17 @@ impl<'a> TryParse for httparse::Response<'a> {
375377
let mut res = httparse::Response::new(headers);
376378
Ok(match try!(res.parse(buf)) {
377379
httparse::Status::Complete(len) => {
380+
let code = res.code.unwrap();
381+
let reason = match <StatusCode as FromPrimitive>::from_u16(code) {
382+
Some(status) => match status.canonical_reason() {
383+
Some(reason) => Cow::Borrowed(reason),
384+
None => Cow::Owned(res.reason.unwrap().to_owned())
385+
},
386+
None => Cow::Owned(res.reason.unwrap().to_owned())
387+
};
378388
httparse::Status::Complete((Incoming {
379389
version: if res.version.unwrap() == 1 { Http11 } else { Http10 },
380-
subject: RawStatus(
381-
res.code.unwrap(), res.reason.unwrap().to_owned().into_cow()
382-
),
390+
subject: RawStatus(code, reason),
383391
headers: try!(Headers::from_raw(res.headers))
384392
}, len))
385393
},
@@ -405,15 +413,9 @@ pub const STAR: u8 = b'*';
405413
pub const LINE_ENDING: &'static str = "\r\n";
406414

407415
/// The raw status code and reason-phrase.
408-
#[derive(PartialEq, Debug)]
416+
#[derive(Clone, PartialEq, Debug)]
409417
pub struct RawStatus(pub u16, pub Cow<'static, str>);
410418

411-
impl Clone for RawStatus {
412-
fn clone(&self) -> RawStatus {
413-
RawStatus(self.0, self.1.clone().into_cow())
414-
}
415-
}
416-
417419
#[cfg(test)]
418420
mod tests {
419421
use std::io::{self, Write};

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![doc(html_root_url = "https://hyperium.github.io/hyper/hyper/index.html")]
2-
#![feature(core, into_cow)]
2+
#![feature(core)]
33
#![deny(missing_docs)]
44
#![cfg_attr(test, deny(warnings))]
55
#![cfg_attr(test, feature(test))]

src/status.rs

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use std::cmp::Ordering;
3131
/// Registry](http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml) which is
3232
/// the source for this enum (with one exception, 418 I'm a teapot, which is
3333
/// inexplicably not in the register).
34+
#[derive(Debug)]
3435
pub enum StatusCode {
3536
/// 100 Continue
3637
/// [[RFC7231, Section 6.2.1](https://tools.ietf.org/html/rfc7231#section-6.2.1)]
@@ -305,7 +306,7 @@ impl StatusCode {
305306

306307
StatusCode::NotExtended => Some("Not Extended"),
307308
StatusCode::NetworkAuthenticationRequired => Some("Network Authentication Required"),
308-
_ => None
309+
StatusCode::Unregistered(..) => None
309310
}
310311
}
311312

@@ -379,86 +380,6 @@ impl fmt::Display for StatusCode {
379380
}
380381
}
381382

382-
impl fmt::Debug for StatusCode {
383-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
384-
let s = match *self {
385-
StatusCode::Continue => "Continue",
386-
StatusCode::SwitchingProtocols => "SwitchingProtocols",
387-
StatusCode::Processing => "Processing",
388-
389-
StatusCode::Ok => "Ok",
390-
StatusCode::Created => "Created",
391-
StatusCode::Accepted => "Accepted",
392-
StatusCode::NonAuthoritativeInformation => "NonAuthoritativeInformation",
393-
StatusCode::NoContent => "NoContent",
394-
StatusCode::ResetContent => "ResetContent",
395-
StatusCode::PartialContent => "PartialContent",
396-
StatusCode::MultiStatus => "MultiStatus",
397-
StatusCode::AlreadyReported => "AlreadyReported",
398-
399-
StatusCode::ImUsed => "ImUsed",
400-
401-
StatusCode::MultipleChoices => "MultipleChoices",
402-
StatusCode::MovedPermanently => "MovedPermanently",
403-
StatusCode::Found => "Found",
404-
StatusCode::SeeOther => "SeeOther",
405-
StatusCode::NotModified => "NotModified",
406-
StatusCode::UseProxy => "UseProxy",
407-
408-
StatusCode::TemporaryRedirect => "TemporaryRedirect",
409-
StatusCode::PermanentRedirect => "PermanentRedirect",
410-
411-
StatusCode::BadRequest => "BadRequest",
412-
StatusCode::Unauthorized => "Unauthorized",
413-
StatusCode::PaymentRequired => "PaymentRequired",
414-
StatusCode::Forbidden => "Forbidden",
415-
StatusCode::NotFound => "NotFound",
416-
StatusCode::MethodNotAllowed => "MethodNotAllowed",
417-
StatusCode::NotAcceptable => "NotAcceptable",
418-
StatusCode::ProxyAuthenticationRequired => "ProxyAuthenticationRequired",
419-
StatusCode::RequestTimeout => "RequestTimeout",
420-
StatusCode::Conflict => "Conflict",
421-
StatusCode::Gone => "Gone",
422-
StatusCode::LengthRequired => "LengthRequired",
423-
StatusCode::PreconditionFailed => "PreconditionFailed",
424-
StatusCode::PayloadTooLarge => "PayloadTooLarge",
425-
StatusCode::UriTooLong => "UriTooLong",
426-
StatusCode::UnsupportedMediaType => "UnsupportedMediaType",
427-
StatusCode::RangeNotSatisfiable => "RangeNotSatisfiable",
428-
StatusCode::ExpectationFailed => "ExpectationFailed",
429-
StatusCode::ImATeapot => "ImATeapot",
430-
431-
StatusCode::UnprocessableEntity => "UnprocessableEntity",
432-
StatusCode::Locked => "Locked",
433-
StatusCode::FailedDependency => "FailedDependency",
434-
435-
StatusCode::UpgradeRequired => "UpgradeRequired",
436-
437-
StatusCode::PreconditionRequired => "PreconditionRequired",
438-
StatusCode::TooManyRequests => "TooManyRequests",
439-
440-
StatusCode::RequestHeaderFieldsTooLarge => "RequestHeaderFieldsTooLarge",
441-
442-
StatusCode::InternalServerError => "InternalServerError",
443-
StatusCode::NotImplemented => "NotImplemented",
444-
StatusCode::BadGateway => "BadGateway",
445-
StatusCode::ServiceUnavailable => "ServiceUnavailable",
446-
StatusCode::GatewayTimeout => "GatewayTimeout",
447-
StatusCode::HttpVersionNotSupported => "HttpVersionNotSupported",
448-
StatusCode::VariantAlsoNegotiates => "VariantAlsoNegotiates",
449-
StatusCode::InsufficientStorage => "InsufficientStorage",
450-
StatusCode::LoopDetected => "LoopDetected",
451-
452-
StatusCode::NotExtended => "NotExtended",
453-
StatusCode::NetworkAuthenticationRequired => "NetworkAuthenticationRequired",
454-
StatusCode::Unregistered(ref code) => {
455-
return write!(f, "Unregistered({})", code);
456-
}
457-
};
458-
f.write_str(s)
459-
}
460-
}
461-
462383
impl PartialEq for StatusCode {
463384
#[inline]
464385
fn eq(&self, other: &StatusCode) -> bool {

0 commit comments

Comments
 (0)