Skip to content

Commit

Permalink
add clippy rules for serialize crate (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Feb 14, 2025
1 parent dda7f06 commit 0f21371
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
15 changes: 10 additions & 5 deletions serialize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ edition.workspace = true
rust-version.workspace = true
metadata.docs.rs.workspace = true
package.metadata.release.workspace = true
keywords = ["cryptography", "serialization" ]
keywords = ["cryptography", "serialization"]

[lints]
workspace = true

[dependencies]
ark-serialize-derive = { workspace = true, optional = true }
Expand All @@ -27,11 +30,13 @@ rayon = { workspace = true, optional = true }
sha2.workspace = true
sha3.workspace = true
blake2.workspace = true
ark-test-curves = { workspace = true, default-features = false, features = [ "bls12_381_curve"] }
ark-test-curves = { workspace = true, default-features = false, features = [
"bls12_381_curve",
] }


[features]
default = []
parallel = [ "rayon" ]
std = [ "ark-std/std" ]
derive = [ "ark-serialize-derive" ]
parallel = ["rayon"]
std = ["ark-std/std"]
derive = ["ark-serialize-derive"]
12 changes: 6 additions & 6 deletions serialize/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ pub enum SerializationError {
impl ark_std::error::Error for SerializationError {}

impl From<io::Error> for SerializationError {
fn from(e: io::Error) -> SerializationError {
SerializationError::IoError(e)
fn from(e: io::Error) -> Self {
Self::IoError(e)
}
}

impl fmt::Display for SerializationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
SerializationError::NotEnoughSpace => write!(
Self::NotEnoughSpace => write!(
f,
"the last byte does not have enough space to encode the extra info bits"
),
SerializationError::InvalidData => write!(f, "the input buffer contained invalid data"),
SerializationError::UnexpectedFlags => write!(f, "the call expects empty flags"),
SerializationError::IoError(err) => write!(f, "I/O error: {:?}", err),
Self::InvalidData => write!(f, "the input buffer contained invalid data"),
Self::UnexpectedFlags => write!(f, "the call expects empty flags"),
Self::IoError(err) => write!(f, "I/O error: {:?}", err),
}
}
}
2 changes: 1 addition & 1 deletion serialize/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ impl Flags for EmptyFlags {

#[inline]
fn from_u8(_: u8) -> Option<Self> {
Some(EmptyFlags)
Some(Self)
}
}
40 changes: 19 additions & 21 deletions serialize/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl CanonicalDeserialize for usize {
) -> Result<Self, SerializationError> {
let mut bytes = [0u8; core::mem::size_of::<u64>()];
reader.read_exact(&mut bytes)?;
Ok(<u64>::from_le_bytes(bytes) as usize)
Ok(<u64>::from_le_bytes(bytes) as Self)
}
}

Expand All @@ -173,7 +173,7 @@ impl CanonicalDeserialize for BigUint {
compress: Compress,
validate: Validate,
) -> Result<Self, SerializationError> {
Ok(BigUint::from_bytes_le(&Vec::<u8>::deserialize_with_mode(
Ok(Self::from_bytes_le(&Vec::<u8>::deserialize_with_mode(
reader, compress, validate,
)?))
}
Expand Down Expand Up @@ -234,7 +234,7 @@ impl<T: Valid> Valid for Option<T> {
where
Self: 'a,
{
T::batch_check(batch.map(Option::as_ref).filter(Option::is_some).flatten())
T::batch_check(batch.map(Self::as_ref).filter(Option::is_some).flatten())
}
}

Expand All @@ -246,11 +246,9 @@ impl<T: CanonicalDeserialize> CanonicalDeserialize for Option<T> {
validate: Validate,
) -> Result<Self, SerializationError> {
let is_some = bool::deserialize_with_mode(&mut reader, compress, validate)?;
let data = if is_some {
Some(T::deserialize_with_mode(&mut reader, compress, validate)?)
} else {
None
};
let data = is_some
.then(|| T::deserialize_with_mode(&mut reader, compress, validate))
.transpose()?;

Ok(data)
}
Expand Down Expand Up @@ -287,7 +285,7 @@ impl<T: Send + Sync> CanonicalDeserialize for PhantomData<T> {
_compress: Compress,
_validate: Validate,
) -> Result<Self, SerializationError> {
Ok(PhantomData)
Ok(Self)
}
}

Expand Down Expand Up @@ -352,7 +350,7 @@ impl<T: CanonicalDeserialize + ToOwned + Sync + Send> CanonicalDeserialize
compress: Compress,
validate: Validate,
) -> Result<Self, SerializationError> {
Ok(ark_std::sync::Arc::new(T::deserialize_with_mode(
Ok(Self::new(T::deserialize_with_mode(
reader, compress, validate,
)?))
}
Expand Down Expand Up @@ -398,7 +396,7 @@ where

impl<T> CanonicalDeserialize for Cow<'_, T>
where
T: ToOwned + Valid + Valid + Sync + Send,
T: ToOwned + Valid + Sync + Send,
<T as ToOwned>::Owned: CanonicalDeserialize + Valid + Send,
{
#[inline]
Expand All @@ -420,7 +418,7 @@ impl<T: CanonicalSerialize, const N: usize> CanonicalSerialize for [T; N] {
mut writer: W,
compress: Compress,
) -> Result<(), SerializationError> {
for item in self.iter() {
for item in self {
item.serialize_with_mode(&mut writer, compress)?;
}
Ok(())
Expand Down Expand Up @@ -465,7 +463,7 @@ impl<T: CanonicalDeserialize, const N: usize> CanonicalDeserialize for [T; N] {
Validate::No,
)?);
}
if let Validate::Yes = validate {
if validate == Validate::Yes {
T::batch_check(array.iter())?
}
Ok(array.into_inner().ok().unwrap())
Expand Down Expand Up @@ -515,7 +513,7 @@ impl<T: CanonicalDeserialize> CanonicalDeserialize for Vec<T> {
let len = u64::deserialize_with_mode(&mut reader, compress, validate)?
.try_into()
.map_err(|_| SerializationError::NotEnoughSpace)?;
let mut values = Vec::with_capacity(len);
let mut values = Self::with_capacity(len);
for _ in 0..len {
values.push(T::deserialize_with_mode(
&mut reader,
Expand All @@ -524,7 +522,7 @@ impl<T: CanonicalDeserialize> CanonicalDeserialize for Vec<T> {
)?);
}

if let Validate::Yes = validate {
if validate == Validate::Yes {
T::batch_check(values.iter())?
}
Ok(values)
Expand Down Expand Up @@ -612,7 +610,7 @@ impl<T: CanonicalDeserialize> CanonicalDeserialize for VecDeque<T> {
let len = u64::deserialize_with_mode(&mut reader, compress, validate)?
.try_into()
.map_err(|_| SerializationError::NotEnoughSpace)?;
let mut values = VecDeque::with_capacity(len);
let mut values = Self::with_capacity(len);
for _ in 0..len {
values.push_back(T::deserialize_with_mode(
&mut reader,
Expand All @@ -621,7 +619,7 @@ impl<T: CanonicalDeserialize> CanonicalDeserialize for VecDeque<T> {
)?);
}

if let Validate::Yes = validate {
if validate == Validate::Yes {
T::batch_check(values.iter())?
}
Ok(values)
Expand Down Expand Up @@ -674,7 +672,7 @@ impl<T: CanonicalDeserialize> CanonicalDeserialize for LinkedList<T> {
let len = u64::deserialize_with_mode(&mut reader, compress, validate)?
.try_into()
.map_err(|_| SerializationError::NotEnoughSpace)?;
let mut values = LinkedList::new();
let mut values = Self::new();
for _ in 0..len {
values.push_back(T::deserialize_with_mode(
&mut reader,
Expand All @@ -683,7 +681,7 @@ impl<T: CanonicalDeserialize> CanonicalDeserialize for LinkedList<T> {
)?);
}

if let Validate::Yes = validate {
if validate == Validate::Yes {
T::batch_check(values.iter())?
}
Ok(values)
Expand Down Expand Up @@ -753,7 +751,7 @@ impl CanonicalDeserialize for String {
validate: Validate,
) -> Result<Self, SerializationError> {
let bytes = <Vec<u8>>::deserialize_with_mode(reader, compress, validate)?;
String::from_utf8(bytes).map_err(|_| SerializationError::InvalidData)
Self::from_utf8(bytes).map_err(|_| SerializationError::InvalidData)
}
}

Expand Down Expand Up @@ -822,7 +820,7 @@ where
) -> Result<(), SerializationError> {
let len = self.len() as u64;
len.serialize_with_mode(&mut writer, compress)?;
for (k, v) in self.iter() {
for (k, v) in self {
k.serialize_with_mode(&mut writer, compress)?;
v.serialize_with_mode(&mut writer, compress)?;
}
Expand Down
2 changes: 1 addition & 1 deletion serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub trait CanonicalSerializeHashExt: CanonicalSerialize {
impl<T: CanonicalSerialize> CanonicalSerializeHashExt for T {}

#[inline]
pub fn buffer_bit_byte_size(modulus_bits: usize) -> (usize, usize) {
pub const fn buffer_bit_byte_size(modulus_bits: usize) -> (usize, usize) {
let byte_size = buffer_byte_size(modulus_bits);
((byte_size * 8), byte_size)
}
Expand Down
3 changes: 2 additions & 1 deletion serialize/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl CanonicalDeserialize for Dummy {
assert_eq!(<[u8; 2]>::deserialize_compressed(reader)?, [100u8, 200u8])
},
}
Ok(Dummy)
Ok(Self)
}
}

Expand Down Expand Up @@ -215,6 +215,7 @@ fn test_rc_arc() {
}

#[test]
#[allow(clippy::zero_sized_map_values)]
fn test_btreemap() {
let mut map = BTreeMap::new();
map.insert(0u64, Dummy);
Expand Down

0 comments on commit 0f21371

Please sign in to comment.