Refactor code based on clippy suggestions

This commit is contained in:
2022-08-25 17:45:16 -04:00
parent 9a9b5335f1
commit b97b2fe6d0
16 changed files with 145 additions and 204 deletions

View File

@@ -11,13 +11,16 @@ impl Bytes {
}
pub fn from_utf8(s: &str) -> Bytes {
Bytes(s.as_bytes().iter().map(|c| c.clone()).collect())
Bytes(s.as_bytes().to_vec())
}
#[allow(dead_code)]
pub fn to_utf8(&self) -> String {
let Bytes(v) = self;
String::from(std::str::from_utf8(&v).unwrap())
String::from(std::str::from_utf8(v).unwrap())
}
pub fn to_sub_utf8(&self, length: usize) -> String {
Bytes(self.0[..length].to_vec()).to_utf8()
}
pub fn len(&self) -> usize {
@@ -69,7 +72,7 @@ impl Bytes {
pub fn is_ascii(&self) -> bool {
let Bytes(v) = self;
for &c in v.iter() {
if c < 32 || c > 127 {
if !(32..=127).contains(&c) {
return false;
}
}
@@ -103,7 +106,7 @@ impl Bytes {
h.last().unwrap().1
}
pub fn pad_pkcs7(&mut self, block_size: usize) -> () {
pub fn pad_pkcs7(&mut self, block_size: usize) {
let Bytes(v) = self;
let padding_value = (block_size - v.len() % block_size) as u8;
for _ in 0..padding_value {
@@ -118,7 +121,7 @@ impl Bytes {
let last_block_index = self.len() / block_size - 1;
let last_block = self.get_block(last_block_index, block_size).0;
let pad_byte = last_block[block_size - 1];
if pad_byte < 1 || pad_byte > 16 {
if !(1..=16).contains(&pad_byte) {
return false;
}
for i in 0..(pad_byte as usize) {
@@ -127,10 +130,10 @@ impl Bytes {
return false;
}
}
return true;
true
}
pub fn remove_pkcs7(&mut self, block_size: usize) -> () {
pub fn remove_pkcs7(&mut self, block_size: usize) {
if !self.has_valid_pkcs7(block_size) {
return;
}
@@ -141,7 +144,7 @@ impl Bytes {
}
}
pub fn flip_bit(&mut self, byte_index: usize, bit_index: usize) -> () {
pub fn flip_bit(&mut self, byte_index: usize, bit_index: usize) {
let Bytes(v) = self;
let flip_mask: u8 = 0b1 << bit_index;
v[byte_index] ^= flip_mask;