Commit ff2723d7 authored by Hero Bird's avatar Hero Bird

[pdsl_core] Fix tons of clippy warnings

parent f7be0c59
......@@ -18,7 +18,7 @@
/// Flips all bytes in the byte slice inplace.
fn invert_bytes(bytes: &mut [u8]) {
for byte in bytes.into_iter() {
for byte in bytes.iter_mut() {
*byte = !*byte;
}
}
......@@ -41,7 +41,7 @@ macro_rules! impl_slice_as_array {
return None
}
Some(unsafe {
core::mem::transmute::<*const T, &[T; $n]>(slice.as_ptr())
&*(slice.as_ptr() as *const [T; $n])
})
}
};
......@@ -70,9 +70,9 @@ fn bytes_ops_byte<F>(lhs: &mut [u8], rhs: u8, ops: F) -> bool
where
F: Copy + Fn(u8, u8) -> (u8, bool)
{
assert!(lhs.len() >= 1);
assert!(!lhs.is_empty());
let mut carry = rhs;
for lhs in lhs.into_iter().rev() {
for lhs in lhs.iter_mut().rev() {
if carry == 0 {
return false
}
......@@ -80,7 +80,7 @@ where
*lhs = res;
carry = u8::from(ovfl);
}
if carry == 0 { false } else { true }
carry != 0
}
/// Generic carry-operation implementation for two byte slices
......@@ -99,17 +99,17 @@ where
F: Copy + Fn(u8, u8) -> (u8, bool)
{
assert_eq!(lhs.len(), rhs.len());
assert!(lhs.len() > 0);
debug_assert!(rhs.len() > 0);
assert!(!lhs.is_empty());
debug_assert!(!rhs.is_empty());
let mut carry = 0;
for (lhs, rhs) in lhs.into_iter().zip(rhs.into_iter()).rev() {
for (lhs, rhs) in lhs.iter_mut().zip(rhs.iter()).rev() {
let (res1, carry1) = ops(*lhs, carry);
let (res2, carry2) = ops(res1, *rhs);
debug_assert!(!(carry1 && carry2));
*lhs = res2;
carry = u8::from(carry1 || carry2);
}
if carry == 0 { false } else { true }
carry != 0
}
/// Generic carry-operation implementation for two byte slices.
......
......@@ -51,19 +51,17 @@ impl Keccak256Hasher {
pub fn finish64(self) -> [u8; 8] {
let mut arr = [0; 8];
let res = self.finish256();
for n in 0..8 {
arr[n] = res[n];
}
arr[..8].clone_from_slice(&res[..8]);
arr
}
}
fn bytes8_to_u64(bytes: [u8; 8]) -> u64 {
let mut val = 0;
for n_byte in 0..7 {
val |= (bytes[n_byte] as u64) << (n_byte * 8);
}
val
bytes
.iter()
.enumerate()
.map(|(n, &ext)| u64::from(ext) << (n * 8))
.fold(0u64, |acc, ext| acc | ext)
}
impl Hasher for Keccak256Hasher {
......
......@@ -20,7 +20,7 @@ use crate::{
storage::Key,
};
const BUMP_ALLOC_LOG_TARGET: &'static str = "bump_alloc";
const BUMP_ALLOC_LOG_TARGET: &str = "bump_alloc";
/// An allocator that is meant to allocate contract storage at
/// compile-time by simply bumping its current allocation key.
......@@ -68,7 +68,7 @@ impl Allocator for BumpAlloc {
cannot allocate zero (0) bytes"
)
}
let key = self.offset_key.clone();
let key = self.offset_key;
self.inc_offset_key(size);
log::info!(
target: BUMP_ALLOC_LOG_TARGET,
......
......@@ -24,7 +24,7 @@ use crate::{
use parity_codec::{Encode, Decode};
const CC_ALLOC_LOG_TARGET: &'static str = "cc_alloc";
const CC_ALLOC_LOG_TARGET: &str = "cc_alloc";
/// An allocator for the contract storage.
///
......@@ -195,7 +195,7 @@ impl CellChunkAlloc {
///
/// The reverse of `key_to_chunk_index`.
fn chunk_index_to_key(&self, index: u32) -> Key {
let chunk_offset: u64 = (1 << 32) * (index as u64);
let chunk_offset: u64 = (1 << 32) * u64::from(index);
self.chunks_offset_key() + chunk_offset
}
......
......@@ -137,7 +137,6 @@ impl<T> Cache<T> {
self
.entries
.get(&n)
.into()
}
/// Returns a mutable reference to the cached value at position `n` if any.
......@@ -145,7 +144,6 @@ impl<T> Cache<T> {
self
.entries
.get_mut(&n)
.into()
}
/// Returns the cache entry at position `n`.
......@@ -216,6 +214,13 @@ impl<T> CacheGuard<T> {
/// Returns an immutable reference to the internal cache entry.
///
/// Used to returns references from the inside to the outside.
///
/// # Devs & Internals
///
/// Note the very critically looking `allow(clippy::mut_from_ref)`.
/// We might change this in the future and we should be very careful
/// about its usage!
#[allow(clippy::mut_from_ref)]
fn elems_mut(&self) -> &mut Cache<T> {
unsafe { &mut *self.cache.as_ptr() }
}
......
......@@ -14,6 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with pDSL. If not, see <http://www.gnu.org/licenses/>.
// We allow having no generalization for hasher of the hashmap implementation.
// This might change in future versions of the pDSL.
#![allow(clippy::implicit_hasher)]
use crate::storage::{
self,
chunk::SyncChunk,
......@@ -141,18 +145,6 @@ impl<K, V> HashMap<K, V> {
}
}
/// Converts the given bytes into a `u32` value.
///
/// The first byte in the array will be the most significant byte.
fn bytes4_to_u32(bytes: [u8; 4]) -> u32 {
let mut res = 0;
res |= (bytes[0] as u32) << 24;
res |= (bytes[1] as u32) << 16;
res |= (bytes[2] as u32) << 8;
res |= (bytes[3] as u32) << 0;
res
}
/// Converts the given slice into an array with fixed size of 4.
///
/// Returns `None` if the slice's length is not 4.
......@@ -164,9 +156,7 @@ where
return None
}
let mut array = [T::default(); 4];
for i in 0..4 {
array[i] = bytes[i];
}
array[..4].clone_from_slice(&bytes[..4]);
Some(array)
}
......@@ -300,7 +290,7 @@ where
{
// Convert the first 4 bytes in the keccak256 hash
// of the key into a big-endian unsigned integer.
let probe_start = bytes4_to_u32(
let probe_start = u32::from_be_bytes(
slice_as_array4(
&(hash::keccak256(key.borrow())[0..4])
).expect(
......@@ -433,10 +423,7 @@ where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
match self.get(key) {
Some(_) => true,
None => false,
}
self.get(key).is_some()
}
/// Returns an immutable reference to the entry corresponding to the key.
......
......@@ -374,7 +374,7 @@ where
) {
Entry::Occupied(val) => {
self.next_vacant.set(n);
debug_assert!(self.len() >= 1);
debug_assert!(!self.is_empty());
self.len.set(self.len() - 1);
Some(val)
},
......
......@@ -252,7 +252,7 @@ where
/// Removes the last element from the vector and returns it,
/// or `None` if the vector is empty.
pub fn pop(&mut self) -> Option<T> {
if self.len() == 0 {
if self.is_empty() {
return None
}
let last_index = self.len() - 1;
......@@ -328,7 +328,7 @@ where
///
/// Returns `None` and does not remove if `n` is out of bounds.
pub fn swap_remove(&mut self, n: u32) -> Option<T> {
if self.len() == 0 {
if self.is_empty() {
return None
}
self
......
......@@ -18,7 +18,7 @@ use crate::byte_utils;
use parity_codec::{Encode, Decode};
const KEY_LOG_TARGET: &'static str = "key";
const KEY_LOG_TARGET: &str = "key";
/// Typeless generic key into contract storage.
///
......@@ -60,11 +60,9 @@ impl core::fmt::Display for Key {
bytes[28], bytes[29], bytes[30], bytes[31],
)?;
} else {
let mut counter = 0;
for byte in self.as_bytes() {
for (n, byte) in self.as_bytes().iter().enumerate() {
write!(f, "{:02X}", byte)?;
counter += 1;
if counter % 4 == 0 && counter != 32 {
if n % 4 == 0 && n != 32 {
write!(f, "_")?;
}
}
......@@ -103,58 +101,52 @@ impl core::ops::Sub for Key {
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct KeyDiff([u8; 32]);
impl KeyDiff {
/// Returns the byte slice of this key difference.
fn as_bytes(&self) -> &[u8] {
&self.0
}
macro_rules! impl_try_to_prim {
(
$( #[$attr:meta] )*
$name:ident, $prim:ty, $conv:ident
) => {
impl KeyDiff {
$( #[$attr] )*
pub fn $name(&self) -> Option<$prim> {
const KEY_BYTES: usize = 32;
const PRIM_BYTES: usize = core::mem::size_of::<$prim>();
if self.as_bytes().iter().take(KEY_BYTES - PRIM_BYTES).any(|&byte| byte != 0x0) {
return None
}
let value = <$prim>::from_be_bytes(
*byte_utils::$conv(&self.as_bytes()[(KEY_BYTES - PRIM_BYTES)..KEY_BYTES])
.unwrap()
);
Some(value)
}
}
};
}
impl_try_to_prim!(
/// Tries to convert the key difference to a `u32` if possible.
///
/// Returns `None` if the resulting value is out of bounds.
pub fn try_to_u32(&self) -> Option<u32> {
const KEY_BYTES: usize = 32;
const U32_BYTES: usize = core::mem::size_of::<u32>();
if self.as_bytes().into_iter().take(KEY_BYTES - U32_BYTES).any(|&byte| byte != 0x0) {
return None
}
let value = u32::from_be_bytes(
*byte_utils::slice4_as_array4(&self.as_bytes()[(KEY_BYTES - U32_BYTES)..KEY_BYTES])
.unwrap()
);
Some(value)
}
try_to_u32, u32, slice4_as_array4
);
impl_try_to_prim!(
/// Tries to convert the key difference to a `u64` if possible.
///
/// Returns `None` if the resulting value is out of bounds.
pub fn try_to_u64(&self) -> Option<u64> {
const KEY_BYTES: usize = 32;
const U64_BYTES: usize = core::mem::size_of::<u64>();
if self.as_bytes().into_iter().take(KEY_BYTES - U64_BYTES).any(|&byte| byte != 0x0) {
return None
}
let value = u64::from_be_bytes(
*byte_utils::slice8_as_array8(&self.as_bytes()[(KEY_BYTES - U64_BYTES)..KEY_BYTES])
.unwrap()
);
Some(value)
}
try_to_u64, u64, slice8_as_array8
);
impl_try_to_prim!(
/// Tries to convert the key difference to a `u128` if possible.
///
/// Returns `None` if the resulting value is out of bounds.
pub fn try_to_u128(&self) -> Option<u128> {
const KEY_BYTES: usize = 32;
const U128_BYTES: usize = core::mem::size_of::<u128>();
if self.as_bytes().into_iter().take(KEY_BYTES - U128_BYTES).any(|&byte| byte != 0x0) {
return None
}
let value = u128::from_be_bytes(
*byte_utils::slice16_as_array16(&self.as_bytes()[(KEY_BYTES - U128_BYTES)..KEY_BYTES])
.unwrap()
);
Some(value)
try_to_u128, u128, slice16_as_array16
);
impl KeyDiff {
/// Returns the byte slice of this key difference.
fn as_bytes(&self) -> &[u8] {
&self.0
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment