Skip to content

Commit

Permalink
Fix up some issues indicated by clippy (#116)
Browse files Browse the repository at this point in the history
* Remove lifetimes

* Remove return keyword

* Switch match to if/if-let

* Remove unneeded borrowing

* Switch iterator call to direct reference

* Let WebSocketKey derive Default

* Remove simple closure

* Use helper function

* Switch Vec-ref to remove extra reference
  • Loading branch information
Daniel Lockyer authored and Michael Eden committed Apr 18, 2017
1 parent fc93ffb commit a27bca8
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 64 deletions.
11 changes: 4 additions & 7 deletions examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,10 @@ fn main() {
return;
}
};
match message.opcode {
Type::Close => {
let _ = sender.send_message(&message);
// If it's a close message, just send it and then return.
return;
}
_ => (),
if Type::Close == message.opcode {
let _ = sender.send_message(&message);
// If it's a close message, just send it and then return.
return;
}
// Send the message
match sender.send_message(&message) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl<S> Client<S>
}

/// Returns an iterator over incoming data frames.
pub fn incoming_dataframes<'a>(&'a mut self) -> DataFrameIterator<'a, Receiver, BufReader<S>> {
pub fn incoming_dataframes(&mut self) -> DataFrameIterator<Receiver, BufReader<S>> {
self.receiver.incoming_dataframes(&mut self.stream)
}

Expand Down
4 changes: 2 additions & 2 deletions src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ impl DataFrameable for DataFrame {
}

#[inline(always)]
fn reserved<'a>(&'a self) -> &'a [bool; 3] {
fn reserved(&self) -> &[bool; 3] {
&self.reserved
}

#[inline(always)]
fn payload<'a>(&'a self) -> Cow<'a, [u8]> {
fn payload(&self) -> Cow<[u8]> {
Cow::Borrowed(&self.data)
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/header/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ impl FromStr for WebSocketAccept {
}
let mut array = [0u8; 20];
let mut iter = vec.into_iter();
for i in array.iter_mut() {
for i in &mut array {
*i = iter.next().unwrap();
}
Ok(WebSocketAccept(array))
}
Err(_) => {
return Err(WebSocketError::ProtocolError("Invalid Sec-WebSocket-Accept "));
}
Err(_) => Err(WebSocketError::ProtocolError("Invalid Sec-WebSocket-Accept ")),
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/header/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct WebSocketExtensions(pub Vec<Extension>);
impl Deref for WebSocketExtensions {
type Target = Vec<Extension>;

fn deref<'a>(&'a self) -> &'a Vec<Extension> {
fn deref(&self) -> &Vec<Extension> {
&self.0
}
}
Expand Down Expand Up @@ -69,7 +69,7 @@ impl FromStr for Extension {
impl fmt::Display for Extension {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{}", self.name));
for param in self.params.iter() {
for param in &self.params {
try!(write!(f, "; {}", param));
}
Ok(())
Expand Down Expand Up @@ -98,9 +98,8 @@ impl Parameter {
impl fmt::Display for Parameter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{}", self.name));
match self.value {
Some(ref x) => try!(write!(f, "={}", x)),
None => (),
if let Some(ref x) = self.value {
try!(write!(f, "={}", x));
}
Ok(())
}
Expand All @@ -112,7 +111,7 @@ impl Header for WebSocketExtensions {
}

fn parse_header(raw: &[Vec<u8>]) -> hyper::Result<WebSocketExtensions> {
from_comma_delimited(raw).map(|vec| WebSocketExtensions(vec))
from_comma_delimited(raw).map(WebSocketExtensions)
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/header/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serialize::base64::{ToBase64, FromBase64, STANDARD};
use result::{WebSocketResult, WebSocketError};

/// Represents a Sec-WebSocket-Key header.
#[derive(PartialEq, Clone, Copy)]
#[derive(PartialEq, Clone, Copy, Default)]
pub struct WebSocketKey(pub [u8; 16]);

impl Debug for WebSocketKey {
Expand All @@ -29,15 +29,13 @@ impl FromStr for WebSocketKey {
}
let mut array = [0u8; 16];
let mut iter = vec.into_iter();
for i in array.iter_mut() {
for i in &mut array {
*i = iter.next().unwrap();
}

Ok(WebSocketKey(array))
}
Err(_) => {
return Err(WebSocketError::ProtocolError("Invalid Sec-WebSocket-Accept"));
}
Err(_) => Err(WebSocketError::ProtocolError("Invalid Sec-WebSocket-Accept")),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/header/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Origin(pub String);

impl Deref for Origin {
type Target = String;
fn deref<'a>(&'a self) -> &'a String {
fn deref(&self) -> &String {
&self.0
}
}
Expand All @@ -21,7 +21,7 @@ impl Header for Origin {
}

fn parse_header(raw: &[Vec<u8>]) -> hyper::Result<Origin> {
from_one_raw_str(raw).map(|s| Origin(s))
from_one_raw_str(raw).map(Origin)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/header/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct WebSocketProtocol(pub Vec<String>);

impl Deref for WebSocketProtocol {
type Target = Vec<String>;
fn deref<'a>(&'a self) -> &'a Vec<String> {
fn deref(&self) -> &Vec<String> {
&self.0
}
}
Expand All @@ -23,7 +23,7 @@ impl Header for WebSocketProtocol {
}

fn parse_header(raw: &[Vec<u8>]) -> hyper::Result<WebSocketProtocol> {
from_comma_delimited(raw).map(|vec| WebSocketProtocol(vec))
from_comma_delimited(raw).map(WebSocketProtocol)
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,13 @@ macro_rules! upsert_header {
Some($pat:pat) => $some_match:expr,
None => $default:expr
}) => {{
match $headers.has::<$header>() {
true => {
match $headers.get_mut::<$header>() {
Some($pat) => { $some_match; },
None => (),
};
if $headers.has::<$header>() {
if let Some($pat) = $headers.get_mut::<$header>() {
$some_match
}
false => {
$headers.set($default);
},
};
} else {
$headers.set($default);
}
}}
}

Expand Down
6 changes: 3 additions & 3 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ impl<'a> ws::dataframe::DataFrame for Message<'a> {
}

#[inline(always)]
fn reserved<'b>(&'b self) -> &'b [bool; 3] {
fn reserved(&self) -> &[bool; 3] {
FALSE_RESERVED_BITS
}

fn payload<'b>(&'b self) -> Cow<'b, [u8]> {
fn payload(&self) -> Cow<[u8]> {
let mut buf = Vec::with_capacity(self.size());
self.write_payload(&mut buf).ok();
Cow::Owned(buf)
Expand Down Expand Up @@ -188,7 +188,7 @@ impl<'a, 'b> ws::Message<'b, &'b Message<'a>> for Message<'a> {
Some(Opcode::Text) => Message::text(try!(bytes_to_string(&data[..]))),
Some(Opcode::Binary) => Message::binary(data),
Some(Opcode::Close) => {
if data.len() > 0 {
if !data.is_empty() {
let status_code = try!((&data[..]).read_u16::<BigEndian>());
let reason = try!(bytes_to_string(&data[2..]));
Message::close_because(status_code, reason)
Expand Down
2 changes: 1 addition & 1 deletion src/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<R> Reader<R>
}

/// Returns an iterator over incoming data frames.
pub fn incoming_dataframes<'a>(&'a mut self) -> DataFrameIterator<'a, Receiver, BufReader<R>> {
pub fn incoming_dataframes(&mut self) -> DataFrameIterator<Receiver, BufReader<R>> {
self.receiver.incoming_dataframes(&mut self.stream)
}

Expand Down
26 changes: 13 additions & 13 deletions src/server/upgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,17 @@ impl Display for HyperIntoWsError {
impl Error for HyperIntoWsError {
fn description(&self) -> &str {
use self::HyperIntoWsError::*;
match self {
&MethodNotGet => "Request method must be GET",
&UnsupportedHttpVersion => "Unsupported request HTTP version",
&UnsupportedWebsocketVersion => "Unsupported WebSocket version",
&NoSecWsKeyHeader => "Missing Sec-WebSocket-Key header",
&NoWsUpgradeHeader => "Invalid Upgrade WebSocket header",
&NoUpgradeHeader => "Missing Upgrade WebSocket header",
&NoWsConnectionHeader => "Invalid Connection WebSocket header",
&NoConnectionHeader => "Missing Connection WebSocket header",
&Io(ref e) => e.description(),
&Parsing(ref e) => e.description(),
match *self {
MethodNotGet => "Request method must be GET",
UnsupportedHttpVersion => "Unsupported request HTTP version",
UnsupportedWebsocketVersion => "Unsupported WebSocket version",
NoSecWsKeyHeader => "Missing Sec-WebSocket-Key header",
NoWsUpgradeHeader => "Invalid Upgrade WebSocket header",
NoUpgradeHeader => "Missing Upgrade WebSocket header",
NoWsConnectionHeader => "Invalid Connection WebSocket header",
NoConnectionHeader => "Missing Connection WebSocket header",
Io(ref e) => e.description(),
Parsing(ref e) => e.description(),
}
}

Expand Down Expand Up @@ -413,9 +413,9 @@ fn validate(
None => return Err(HyperIntoWsError::NoUpgradeHeader),
};

fn check_connection_header(headers: &Vec<ConnectionOption>) -> bool {
fn check_connection_header(headers: &[ConnectionOption]) -> bool {
for header in headers {
if let &ConnectionOption::ConnectionHeader(ref h) = header {
if let ConnectionOption::ConnectionHeader(ref h) = *header {
if UniCase(h as &str) == UniCase("upgrade") {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub trait AsTcpStream {

impl AsTcpStream for TcpStream {
fn as_tcp(&self) -> &TcpStream {
&self
self
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/ws/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ pub trait DataFrame {
/// What type of data does this dataframe contain?
fn opcode(&self) -> u8;
/// Reserved bits of this dataframe
fn reserved<'a>(&'a self) -> &'a [bool; 3];
fn reserved(&self) -> &[bool; 3];
/// Entire payload of the dataframe. If not known then implement
/// write_payload as that is the actual method used when sending the
/// dataframe over the wire.
fn payload<'a>(&'a self) -> Cow<'a, [u8]>;
fn payload(&self) -> Cow<[u8]>;

/// How long (in bytes) is this dataframe's payload
fn size(&self) -> usize {
Expand Down Expand Up @@ -95,12 +95,12 @@ impl<'a, D> DataFrame for &'a D
}

#[inline(always)]
fn reserved<'b>(&'b self) -> &'b [bool; 3] {
fn reserved(&self) -> &[bool; 3] {
D::reserved(self)
}

#[inline(always)]
fn payload<'b>(&'b self) -> Cow<'b, [u8]> {
fn payload(&self) -> Cow<[u8]> {
D::payload(self)
}

Expand Down
5 changes: 2 additions & 3 deletions src/ws/util/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ pub fn write_header<W>(writer: &mut W, header: DataFrameHeader) -> WebSocketResu
}

// Write 'Masking-key'
match header.mask {
Some(mask) => try!(writer.write_all(&mask)),
None => (),
if let Some(mask) = header.mask {
try!(writer.write_all(&mask))
}

Ok(())
Expand Down

0 comments on commit a27bca8

Please sign in to comment.