Skip to content

Commit

Permalink
Clippy suggested improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
de-sh committed May 12, 2022
1 parent 0d5fd8a commit 7a1bf06
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion benchmarks/simplerouter/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ pub fn read_u16(stream: &mut Bytes) -> Result<u16, Error> {
}

fn read_u8(stream: &mut Bytes) -> Result<u8, Error> {
if stream.len() < 1 {
if stream.is_empty() {
return Err(Error::MalformedPacket);
}

Expand Down
27 changes: 12 additions & 15 deletions benchmarks/simplerouter/src/protocol/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ pub(crate) mod connect {
len
}

fn read(connect_flags: u8, mut bytes: &mut Bytes) -> Result<Option<LastWill>, Error> {
fn read(connect_flags: u8, bytes: &mut Bytes) -> Result<Option<LastWill>, Error> {
let last_will = match connect_flags & 0b100 {
0 if (connect_flags & 0b0011_1000) != 0 => {
return Err(Error::IncorrectPacketFormat);
}
0 => None,
_ => {
let will_topic = read_mqtt_bytes(&mut bytes)?;
let will_topic = read_mqtt_bytes(bytes)?;
let will_topic = std::str::from_utf8(&will_topic)?.to_owned();
let will_message = read_mqtt_bytes(&mut bytes)?;
let will_message = read_mqtt_bytes(bytes)?;
let will_qos = qos((connect_flags & 0b11000) >> 3)?;
Some(LastWill {
topic: will_topic,
Expand Down Expand Up @@ -208,19 +208,19 @@ pub(crate) mod connect {
}
}

fn read(connect_flags: u8, mut bytes: &mut Bytes) -> Result<Option<Login>, Error> {
fn read(connect_flags: u8, bytes: &mut Bytes) -> Result<Option<Login>, Error> {
let username = match connect_flags & 0b1000_0000 {
0 => String::new(),
_ => {
let username = read_mqtt_bytes(&mut bytes)?;
let username = read_mqtt_bytes(bytes)?;
std::str::from_utf8(&username)?.to_owned()
}
};

let password = match connect_flags & 0b0100_0000 {
0 => String::new(),
_ => {
let password = read_mqtt_bytes(&mut bytes)?;
let password = read_mqtt_bytes(bytes)?;
std::str::from_utf8(&password)?.to_owned()
}
};
Expand Down Expand Up @@ -392,7 +392,7 @@ pub(crate) mod publish {

// FIXME: Remove indexes and use get method
let stream = &self.raw[self.fixed_header.fixed_header_len..];
let topic_len = view_u16(&stream)? as usize;
let topic_len = view_u16(stream)? as usize;

let stream = &stream[2..];
let topic = view_str(stream, topic_len)?;
Expand All @@ -401,8 +401,7 @@ pub(crate) mod publish {
0 => 0,
1 => {
let stream = &stream[topic_len..];
let pkid = view_u16(stream)?;
pkid
view_u16(stream)?
}
v => return Err(Error::InvalidQoS(v)),
};
Expand All @@ -417,7 +416,7 @@ pub(crate) mod publish {
pub fn view_topic(&self) -> Result<&str, Error> {
// FIXME: Remove indexes
let stream = &self.raw[self.fixed_header.fixed_header_len..];
let topic_len = view_u16(&stream)? as usize;
let topic_len = view_u16(stream)? as usize;

let stream = &stream[2..];
let topic = view_str(stream, topic_len)?;
Expand Down Expand Up @@ -496,7 +495,7 @@ pub(crate) mod publish {
buffer.put_u16(pkid);
}

buffer.extend_from_slice(&payload);
buffer.extend_from_slice(payload);

// TODO: Returned length is wrong in other packets. Fix it
Ok(1 + count + len)
Expand Down Expand Up @@ -567,8 +566,7 @@ pub(crate) mod subscribe {
qos,
};

let mut filters = Vec::new();
filters.push(filter);
let filters = vec![filter];
Subscribe { pkid: 0, filters }
}

Expand Down Expand Up @@ -680,8 +678,7 @@ pub(crate) mod suback {
}

pub fn len(&self) -> usize {
let len = 2 + self.return_codes.len();
len
2 + self.return_codes.len()
}

pub fn read(fixed_header: FixedHeader, mut bytes: Bytes) -> Result<Self, Error> {
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/simplerouter/src/protocol/v5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,10 +1316,10 @@ pub(crate) mod subscribe {
let requested_qos = options & 0b0000_0011;

let nolocal = options >> 2 & 0b0000_0001;
let nolocal = !(nolocal == 0);
let nolocal = nolocal != 0;

let preserve_retain = options >> 3 & 0b0000_0001;
let preserve_retain = !(preserve_retain == 0);
let preserve_retain = preserve_retain != 0;

let retain_forward_rule = (options >> 4) & 0b0000_0011;
let retain_forward_rule = match retain_forward_rule {
Expand Down

0 comments on commit 7a1bf06

Please sign in to comment.