Skip to content

Clippy run and derive Default for Buffer #817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions src/client/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Connect for HttpConnector {
if let Some(key) = self.key(url) {
let host = url.host_str().expect("http scheme must have a host");
self.dns.as_ref().expect("dns workers lost").resolve(host);
self.resolving.entry(host.to_owned()).or_insert(Vec::new()).push(key.clone());
self.resolving.entry(host.to_owned()).or_insert_with(Vec::new).push(key.clone());
Ok(key)
} else {
Err(io::Error::new(io::ErrorKind::InvalidInput, "scheme must be http"))
Expand All @@ -101,25 +101,18 @@ impl Connect for HttpConnector {
Err(_) => return None
};
debug!("Http::resolved <- ({:?}, {:?})", host, addr);
match self.resolving.entry(host) {
Entry::Occupied(mut entry) => {
let resolved = entry.get_mut().remove(0);
if entry.get().is_empty() {
entry.remove();
}
let port = resolved.2;
match addr {
Ok(addr) => {
Some((resolved, TcpStream::connect(&SocketAddr::new(addr, port))
.map(HttpStream)))
},
Err(e) => Some((resolved, Err(e)))
}
}
_ => {
trace!("^-- resolved but not in hashmap?");
return None
if let Entry::Occupied(mut entry) = self.resolving.entry(host) {
let resolved = entry.get_mut().remove(0);
if entry.get().is_empty() {
entry.remove();
}
let port = resolved.2;
Some((resolved, addr.and_then(|addr| TcpStream::connect(&SocketAddr::new(addr, port))
.map(HttpStream))
))
} else {
trace!("^-- resolved but not in hashmap?");
None
}
}

Expand Down Expand Up @@ -167,7 +160,7 @@ impl<S: SslClient> Connect for HttpsConnector<S> {
if let Some(key) = self.key(url) {
let host = url.host_str().expect("http scheme must have a host");
self.http.dns.as_ref().expect("dns workers lost").resolve(host);
self.http.resolving.entry(host.to_owned()).or_insert(Vec::new()).push(key.clone());
self.http.resolving.entry(host.to_owned()).or_insert_with(Vec::new).push(key.clone());
Ok(key)
} else {
Err(io::Error::new(io::ErrorKind::InvalidInput, "scheme must be http or https"))
Expand Down
8 changes: 4 additions & 4 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ impl<H> Client<H> {

/*TODO
pub fn http() -> Config<HttpConnector> {

}

pub fn https() -> Config<HttpsConnector> {

}
*/
}
Expand Down Expand Up @@ -440,7 +440,7 @@ where C: Connect,
let now = scope.now();
let mut empty_keys = Vec::new();
{
for (key, mut vec) in scope.queue.iter_mut() {
for (key, mut vec) in &mut scope.queue {
while !vec.is_empty() && vec[0].deadline <= now {
let mut queued = vec.remove(0);
let _ = queued.handler.on_error(::Error::Timeout);
Expand Down Expand Up @@ -517,7 +517,7 @@ where C: Connect,
match connector.connect(&url) {
Ok(key) => {
let deadline = scope.now() + scope.connect_timeout;
scope.queue.entry(key).or_insert(Vec::new()).push(Queued {
scope.queue.entry(key).or_insert_with(Vec::new).push(Queued {
deadline: deadline,
handler: handler,
url: url
Expand Down
2 changes: 1 addition & 1 deletion src/header/common/accept_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Display for RangeUnit {
match *self {
RangeUnit::Bytes => f.write_str("bytes"),
RangeUnit::None => f.write_str("none"),
RangeUnit::Unregistered(ref x) => f.write_str(&x),
RangeUnit::Unregistered(ref x) => f.write_str(x),
}
}
}
2 changes: 1 addition & 1 deletion src/header/common/content_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl Display for ContentRangeSpec {
ContentRangeSpec::Unregistered { ref unit, ref resp } => {
try!(f.write_str(&unit));
try!(f.write_str(" "));
f.write_str(&resp)
f.write_str(resp)
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/header/common/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,10 @@ impl Header for Host {
}
};

let port = match idx {
Some(idx) => s[idx + 1..].parse().ok(),
None => None
};
let port = idx.and_then(|idx| s[idx + 1..].parse().ok());

match idx {
Some(idx) => s.truncate(idx),
None => ()
if let Some(idx) = idx {
s.truncate(idx)
}

Ok(Host {
Expand Down
14 changes: 7 additions & 7 deletions src/header/common/prefer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl fmt::Display for Preference {
Extension(ref name, ref value, ref params) => {
try!(write!(f, "{}", name));
if value != "" { try!(write!(f, "={}", value)); }
if params.len() > 0 {
if !params.is_empty() {
for &(ref name, ref value) in params {
try!(write!(f, "; {}", name));
if value != "" { try!(write!(f, "={}", value)); }
Expand Down Expand Up @@ -138,12 +138,12 @@ impl FromStr for Preference {
Some(param) => {
let rest: Vec<(String, String)> = params.map(|(l, r)| (l.to_owned(), r.to_owned())).collect();
match param {
("respond-async", "") => if rest.len() == 0 { Ok(RespondAsync) } else { Err(None) },
("return", "representation") => if rest.len() == 0 { Ok(ReturnRepresentation) } else { Err(None) },
("return", "minimal") => if rest.len() == 0 { Ok(ReturnMinimal) } else { Err(None) },
("handling", "strict") => if rest.len() == 0 { Ok(HandlingStrict) } else { Err(None) },
("handling", "leniant") => if rest.len() == 0 { Ok(HandlingLeniant) } else { Err(None) },
("wait", secs) => if rest.len() == 0 { secs.parse().map(Wait).map_err(Some) } else { Err(None) },
("respond-async", "") => if rest.is_empty() { Ok(RespondAsync) } else { Err(None) },
("return", "representation") => if rest.is_empty() { Ok(ReturnRepresentation) } else { Err(None) },
("return", "minimal") => if rest.is_empty() { Ok(ReturnMinimal) } else { Err(None) },
("handling", "strict") => if rest.is_empty() { Ok(HandlingStrict) } else { Err(None) },
("handling", "leniant") => if rest.is_empty() { Ok(HandlingLeniant) } else { Err(None) },
("wait", secs) => if rest.is_empty() { secs.parse().map(Wait).map_err(Some) } else { Err(None) },
(left, right) => Ok(Extension(left.to_owned(), right.to_owned(), rest))
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/header/common/preference_applied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Header for PreferenceApplied {
value.to_owned(),
vec![]
),
preference @ _ => preference.clone()
preference => preference.clone()
}).collect();
fmt_comma_delimited(f, &preferences)
}
Expand Down
4 changes: 2 additions & 2 deletions src/header/common/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl FromStr for Range {
type Err = ::Error;

fn from_str(s: &str) -> ::Result<Range> {
let mut iter = s.splitn(2, "=");
let mut iter = s.splitn(2, '=');

match (iter.next(), iter.next()) {
(Some("bytes"), Some(ranges)) => {
Expand All @@ -153,7 +153,7 @@ impl FromStr for ByteRangeSpec {
type Err = ::Error;

fn from_str(s: &str) -> ::Result<ByteRangeSpec> {
let mut parts = s.splitn(2, "-");
let mut parts = s.splitn(2, '-');

match (parts.next(), parts.next()) {
(Some(""), Some(end)) => {
Expand Down
6 changes: 3 additions & 3 deletions src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ impl<T: Header + Clone> HeaderClone for T {
impl Header + Send + Sync {
#[inline]
unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
mem::transmute(traitobject::data(self))
&*(traitobject::data(self) as *const T)
}

#[inline]
unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
mem::transmute(traitobject::data_mut(self))
&mut *(traitobject::data_mut(self) as *mut T)
}
}

Expand Down Expand Up @@ -497,7 +497,7 @@ impl<'a> fmt::Display for &'a (Header + Send + Sync) {
}
}

/// A wrapper around any Header with a Display impl that calls fmt_header.
/// A wrapper around any Header with a Display impl that calls `fmt_header`.
///
/// This can be used like so: `format!("{}", HeaderFormatter(&header))` to
/// get the representation of a Header which will be written to an
Expand Down
2 changes: 1 addition & 1 deletion src/header/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use header::shared::Charset;
pub fn from_one_raw_str<T: str::FromStr>(raw: &[Vec<u8>]) -> ::Result<T> {
if raw.len() != 1 || unsafe { raw.get_unchecked(0) } == b"" { return Err(::Error::Header) }
// we JUST checked that raw.len() == 1, so raw[0] WILL exist.
from_raw_str(& unsafe { raw.get_unchecked(0) })
from_raw_str( unsafe { raw.get_unchecked(0) })
}

/// Reads a raw string into a value.
Expand Down
2 changes: 1 addition & 1 deletion src/header/shared/charset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Charset {
Gb2312 => "GB2312",
Big5 => "5",
Koi8_R => "KOI8-R",
Ext(ref s) => &s
Ext(ref s) => s
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/header/shared/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::fmt::{self, Display};
// check that each char in the slice is either:
// 1. %x21, or
// 2. in the range %x23 to %x7E, or
// 3. in the range %x80 to %xFF
// 3. above %x80
fn check_slice_validity(slice: &str) -> bool {
slice.bytes().all(|c|
c == b'\x21' || (c >= b'\x23' && c <= b'\x7e') | (c >= b'\x80' && c <= b'\xff'))
c == b'\x21' || (c >= b'\x23' && c <= b'\x7e') | (c >= b'\x80'))
}

/// An entity tag, defined in [RFC7232](https://tools.ietf.org/html/rfc7232#section-2.3)
Expand Down
8 changes: 2 additions & 6 deletions src/http/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ptr;
const INIT_BUFFER_SIZE: usize = 4096;
const MAX_BUFFER_SIZE: usize = 8192 + 4096 * 100;

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Buffer {
vec: Vec<u8>,
read_pos: usize,
Expand All @@ -15,11 +15,7 @@ pub struct Buffer {

impl Buffer {
pub fn new() -> Buffer {
Buffer {
vec: Vec::new(),
read_pos: 0,
write_pos: 0,
}
Buffer::default()
}

pub fn reset(&mut self) {
Expand Down
15 changes: 5 additions & 10 deletions src/http/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,22 +508,17 @@ impl<K: Key, T: Transport, H: MessageHandler<T>> Conn<K, T, H> {
},
Err(e) => {
trace!("error reregistering: {:?}", e);
let _ = self.on_error(e.into());
self.on_error(e.into());
None
}
}
}

pub fn wakeup<F>(mut self, scope: &mut Scope<F>) -> Option<(Self, Option<Duration>)>
where F: MessageHandlerFactory<K, T, Output=H> {
loop {
match self.ctrl.1.try_recv() {
Ok(next) => {
trace!("woke up with {:?}", next);
self.state.update(next);
},
Err(_) => break
}
while let Ok(next) = self.ctrl.1.try_recv() {
trace!("woke up with {:?}", next);
self.state.update(next);
}
self.ready(EventSet::readable() | EventSet::writable(), scope)
}
Expand Down Expand Up @@ -872,7 +867,7 @@ impl<'a, K: Key + 'a> Seed<'a, K> {
}

pub fn key(&self) -> &K {
&self.0
self.0
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/server/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use net::Transport;

use super::{Handler, request, response};

/// A MessageHandler for a Server.
/// A `MessageHandler` for a Server.
///
/// This should be really thin glue between http::MessageHandler and
/// server::Handler, but largely just providing the proper types one
/// This should be really thin glue between `http::MessageHandler` and
/// `server::Handler`, but largely just providing the proper types one
/// would expect in a Server Handler.
pub struct Message<H: Handler<T>, T: Transport> {
handler: H,
Expand Down Expand Up @@ -55,4 +55,3 @@ impl<H: Handler<T>, T: Transport> http::MessageHandler<T> for Message<H, T> {
self.handler.on_remove(transport);
}
}

2 changes: 1 addition & 1 deletion src/server/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<'a> Response<'a> {
}

/// Creates a new Response that can be used to write to a network stream.
pub fn new<'a>(head: &'a mut http::MessageHead<StatusCode>) -> Response<'a> {
pub fn new(head: &mut http::MessageHead<StatusCode>) -> Response {
Response {
head: head
}
Expand Down