Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: some clippy lints #251

Merged
merged 10 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/dyn-abi/benches/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ fn parse(c: &mut Criterion) {
b.iter(|| {
let kw = KEYWORDS.choose(rng).unwrap();
DynSolType::parse(black_box(*kw)).unwrap()
})
});
});
g.bench_function("complex", |b| {
b.iter(|| {
let complex = COMPLEX.choose(rng).unwrap();
DynSolType::parse(black_box(*complex)).unwrap()
})
});
});

g.finish();
Expand All @@ -48,7 +48,7 @@ fn format(c: &mut Criterion) {
b.iter(|| {
let kw = unsafe { keyword_types.choose(rng).unwrap_unchecked() };
black_box(kw).sol_type_name()
})
});
});
g.bench_function("complex", |b| {
let complex_types = COMPLEX
Expand All @@ -60,7 +60,7 @@ fn format(c: &mut Criterion) {
b.iter(|| {
let complex = unsafe { complex_types.choose(rng).unwrap_unchecked() };
black_box(complex).sol_type_name()
})
});
});

g.finish();
Expand Down
4 changes: 2 additions & 2 deletions crates/dyn-abi/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl<'a> arbitrary::Arbitrary<'a> for DynSolType {
Choice::CustomStruct => {
let name = u.arbitrary::<AString>()?.0;
let (prop_names, tuple) = u
.arbitrary_iter::<(AString, DynSolType)>()?
.arbitrary_iter::<(AString, Self)>()?
.flatten()
.map(|(a, b)| (a.0, b))
.unzip();
Expand Down Expand Up @@ -466,7 +466,7 @@ impl DynSolValue {
any::<Address>().prop_map(Self::Address),
int_strategy::<I256>().prop_map(|(x, sz)| Self::Int(x, sz)),
int_strategy::<U256>().prop_map(|(x, sz)| Self::Uint(x, sz)),
(any::<B256>(), 1..=32usize).prop_map(|(x, sz)| DynSolValue::FixedBytes(x, sz)),
(any::<B256>(), 1..=32usize).prop_map(|(x, sz)| Self::FixedBytes(x, sz)),
any::<Vec<u8>>().prop_map(Self::Bytes),
any::<String>().prop_map(Self::String),
]
Expand Down
26 changes: 13 additions & 13 deletions crates/dyn-abi/src/eip712/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ impl DynSolType {
/// Coerce a [`serde_json::Value`] to a [`DynSolValue`] via this type.
pub fn coerce(&self, value: &serde_json::Value) -> DynAbiResult<DynSolValue> {
match self {
DynSolType::Address => address(value),
DynSolType::Function => function(value),
DynSolType::Bool => bool(value),
DynSolType::Int(n) => int(*n, value),
DynSolType::Uint(n) => uint(*n, value),
DynSolType::FixedBytes(n) => fixed_bytes(*n, value),
DynSolType::String => string(value),
DynSolType::Bytes => bytes(value),
DynSolType::Array(inner) => array(inner, value),
DynSolType::FixedArray(inner, n) => fixed_array(inner, *n, value),
DynSolType::Tuple(inner) => tuple(inner, value),
DynSolType::CustomStruct {
Self::Address => address(value),
Self::Function => function(value),
Self::Bool => bool(value),
Self::Int(n) => int(*n, value),
Self::Uint(n) => uint(*n, value),
Self::FixedBytes(n) => fixed_bytes(*n, value),
Self::String => string(value),
Self::Bytes => bytes(value),
Self::Array(inner) => array(inner, value),
Self::FixedArray(inner, n) => fixed_array(inner, *n, value),
Self::Tuple(inner) => tuple(inner, value),
Self::CustomStruct {
name,
prop_names,
tuple,
Expand Down Expand Up @@ -342,6 +342,6 @@ mod tests {
.into()
]
}
)
);
}
}
2 changes: 1 addition & 1 deletion crates/dyn-abi/src/eip712/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,6 @@ mod tests {
"Person(address wallet,string name)".try_into().unwrap(),
]
}
)
);
}
}
24 changes: 11 additions & 13 deletions crates/dyn-abi/src/eip712/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,23 @@ impl<'de> Deserialize<'de> for Resolver {

impl From<Eip712Types> for Resolver {
fn from(types: Eip712Types) -> Self {
let mut graph = Resolver::default();
graph.ingest_types(&types);
graph
Self::from(&types)
}
}

impl From<&Eip712Types> for Resolver {
#[inline]
fn from(types: &Eip712Types) -> Self {
let mut graph = Resolver::default();
let mut graph = Self::default();
graph.ingest_types(types);
graph
}
}

impl From<&Resolver> for Eip712Types {
fn from(resolver: &Resolver) -> Self {
let mut types = Eip712Types::default();
for (name, ty) in resolver.nodes.iter() {
let mut types = Self::default();
for (name, ty) in &resolver.nodes {
types.insert(name.clone(), ty.props.clone());
}
types
Expand All @@ -269,7 +268,7 @@ impl From<&Resolver> for Eip712Types {
impl Resolver {
/// Instantiate a new resolver from a `SolStruct` type.
pub fn from_struct<S: SolStruct>() -> Self {
let mut resolver = Resolver::default();
let mut resolver = Self::default();
resolver.ingest_sol_struct::<S>();
resolver
}
Expand Down Expand Up @@ -326,10 +325,9 @@ impl Resolver {
// Insert the edges into the graph
{
let entry = self.edges.entry(type_name.clone()).or_default();
type_def
.props
.iter()
.for_each(|prop| entry.push(prop.root_type_name().to_owned()));
for prop in &type_def.props {
entry.push(prop.root_type_name().to_owned());
}
} // entry dropped here

// Insert the node into the graph
Expand All @@ -338,7 +336,7 @@ impl Resolver {

/// Ingest a `Types` object into the resolver, discarding any invalid types.
pub fn ingest_types(&mut self, types: &Eip712Types) {
for (type_name, props) in types.iter() {
for (type_name, props) in types {
if let Ok(ty) = TypeDef::new(type_name.clone(), props.to_vec()) {
self.ingest(ty);
}
Expand All @@ -364,7 +362,7 @@ impl Resolver {

if !resolution.contains(&this_type) {
resolution.push(this_type);
for edge in edges.iter() {
for edge in edges {
let rt = edge.as_str().try_into()?;
self.linearize_into(resolution, rt)?;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/dyn-abi/src/eip712/typed_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
#[derive(
Clone, Debug, Default, PartialEq, Eq, Serialize, Deref, DerefMut, From, Into, IntoIterator,
)]
pub struct Eip712Types(#[into_iterator(ref, ref mut, owned)] BTreeMap<String, Vec<PropertyDef>>);
pub struct Eip712Types(#[into_iterator(owned, ref, ref_mut)] BTreeMap<String, Vec<PropertyDef>>);

impl<'de> Deserialize<'de> for Eip712Types {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Expand Down
33 changes: 15 additions & 18 deletions crates/dyn-abi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,59 +57,56 @@ impl fmt::Display for DynAbiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "eip712")]
DynAbiError::TypeMismatch { expected, actual } => {
Self::TypeMismatch { expected, actual } => {
write!(f, "Type mismatch, expected: {expected:?}, actual: {actual}")
}
#[cfg(feature = "eip712")]
DynAbiError::MissingType(name) => write!(f, "Missing type in type resolution: {name}"),
Self::MissingType(name) => write!(f, "Missing type in type resolution: {name}"),
#[cfg(feature = "eip712")]
DynAbiError::CircularDependency(dep) => write!(f, "Circular dependency: {dep}"),
Self::CircularDependency(dep) => write!(f, "Circular dependency: {dep}"),
#[cfg(feature = "eip712")]
DynAbiError::InvalidPropertyDefinition(def) => {
Self::InvalidPropertyDefinition(def) => {
write!(f, "Invalid property definition: {def}")
}

DynAbiError::HexError(h) => h.fmt(f),
DynAbiError::TypeParserError(e) => e.fmt(f),
Self::HexError(h) => h.fmt(f),
Self::TypeParserError(e) => e.fmt(f),
}
}
}

impl From<hex::FromHexError> for DynAbiError {
fn from(e: hex::FromHexError) -> Self {
DynAbiError::HexError(e)
Self::HexError(e)
}
}

#[allow(dead_code)]
impl DynAbiError {
#[cfg(feature = "eip712")]
#[inline]
pub(crate) fn type_mismatch(
expected: crate::DynSolType,
actual: &serde_json::Value,
) -> DynAbiError {
DynAbiError::TypeMismatch {
pub(crate) fn type_mismatch(expected: crate::DynSolType, actual: &serde_json::Value) -> Self {
Self::TypeMismatch {
expected,
actual: actual.clone(),
}
}

#[cfg(feature = "eip712")]
#[inline]
pub(crate) fn invalid_property_def(def: &str) -> DynAbiError {
DynAbiError::InvalidPropertyDefinition(def.into())
pub(crate) fn invalid_property_def(def: &str) -> Self {
Self::InvalidPropertyDefinition(def.into())
}

#[cfg(feature = "eip712")]
#[inline]
pub(crate) fn missing_type(name: &str) -> DynAbiError {
DynAbiError::MissingType(name.into())
pub(crate) fn missing_type(name: &str) -> Self {
Self::MissingType(name.into())
}

#[cfg(feature = "eip712")]
#[inline]
pub(crate) fn circular_dependency(dep: &str) -> DynAbiError {
DynAbiError::CircularDependency(dep.into())
pub(crate) fn circular_dependency(dep: &str) -> Self {
Self::CircularDependency(dep.into())
}
}
4 changes: 2 additions & 2 deletions crates/dyn-abi/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ mod tests {
);

assert_eq!(
parse(r#"tuple(address,bytes, (bool, (string, uint256)[][3]))[2]"#),
parse("tuple(address,bytes, (bool, (string, uint256)[][3]))[2]"),
Ok(DynSolType::FixedArray(
Box::new(DynSolType::Tuple(vec![
DynSolType::Address,
Expand Down Expand Up @@ -462,7 +462,7 @@ mod tests {
Ok(())
);
assert_eq!(
TypeSpecifier::try_from(r#"tuple(address,bytes, (bool, (string, uint256)[][3]))[2]"#)
TypeSpecifier::try_from("tuple(address,bytes, (bool, (string, uint256)[][3]))[2]")
.unwrap()
.try_basic_solidity(),
Ok(())
Expand Down
22 changes: 13 additions & 9 deletions crates/dyn-abi/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ impl From<String> for DynSolValue {
}
}

impl From<Vec<DynSolValue>> for DynSolValue {
impl From<Vec<Self>> for DynSolValue {
#[inline]
fn from(value: Vec<Self>) -> Self {
Self::Array(value)
}
}

impl<const N: usize> From<[DynSolValue; N]> for DynSolValue {
impl<const N: usize> From<[Self; N]> for DynSolValue {
#[inline]
fn from(value: [Self; N]) -> Self {
Self::FixedArray(value.to_vec())
Expand Down Expand Up @@ -541,7 +541,7 @@ impl DynSolValue {
if val.is_dynamic() {
return 1
}
sum += val.head_words()
sum += val.head_words();
}
sum
}
Expand Down Expand Up @@ -577,7 +577,7 @@ impl DynSolValue {
let mut sum = 0;
for val in tuple {
any_dynamic = any_dynamic || val.is_dynamic();
sum += val.total_words()
sum += val.total_words();
}
any_dynamic as usize * sum
}
Expand Down Expand Up @@ -610,9 +610,11 @@ impl DynSolValue {

as_fixed_seq!(s) => {
if s.iter().any(Self::is_dynamic) {
enc.append_indirection()
enc.append_indirection();
} else {
s.iter().for_each(|inner| inner.head_append(enc))
for inner in s {
inner.head_append(enc);
}
}
}
}
Expand Down Expand Up @@ -662,13 +664,15 @@ impl DynSolValue {
} else {
bytes[start] &= 0x7f;
}
buf.extend_from_slice(&bytes[start..])
buf.extend_from_slice(&bytes[start..]);
}
Self::Uint(num, size) => {
buf.extend_from_slice(&num.to_be_bytes::<32>()[(32 - *size)..])
buf.extend_from_slice(&num.to_be_bytes::<32>()[(32 - *size)..]);
}
as_fixed_seq!(inner) | Self::Array(inner) => {
inner.iter().for_each(|v| v.encode_packed_to(buf))
for val in inner {
val.encode_packed_to(buf);
}
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/json-abi/benches/json_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ fn serde_(g: &mut BenchmarkGroup<'_, WallTime>, name: &str, s: &str) {

g.bench_function(format!("{name}/ser/alloy"), |b| {
let abi = serde_json::from_str::<A>(s).unwrap();
b.iter(|| serde_json::to_string(black_box(&abi)).unwrap())
b.iter(|| serde_json::to_string(black_box(&abi)).unwrap());
});
g.bench_function(format!("{name}/ser/ethabi"), |b| {
let abi = serde_json::from_str::<E>(s).unwrap();
b.iter(|| serde_json::to_string(black_box(&abi)).unwrap())
b.iter(|| serde_json::to_string(black_box(&abi)).unwrap());
});

g.bench_function(format!("{name}/de/alloy"), |b| {
b.iter(|| -> A { serde_json::from_str(black_box(s)).unwrap() })
b.iter(|| -> A { serde_json::from_str(black_box(s)).unwrap() });
});
g.bench_function(format!("{name}/de/ethabi"), |b| {
b.iter(|| -> E { serde_json::from_str(black_box(s)).unwrap() })
b.iter(|| -> E { serde_json::from_str(black_box(s)).unwrap() });
});
}

Expand Down Expand Up @@ -55,10 +55,10 @@ fn signature_(g: &mut BenchmarkGroup<'_, WallTime>, name: &str, s: &str) {
assert_eq!(alloy.signature(), ethabi.signature());

g.bench_function(format!("{name}/alloy"), |b| {
b.iter(|| black_box(&alloy).signature())
b.iter(|| black_box(&alloy).signature());
});
g.bench_function(format!("{name}/ethabi"), |b| {
b.iter(|| black_box(&ethabi).signature())
b.iter(|| black_box(&ethabi).signature());
});
}

Expand Down
2 changes: 1 addition & 1 deletion crates/json-abi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl Iterator for IntoItems {
iter_impl!(traits IntoItems);

impl<'de> Deserialize<'de> for JsonAbi {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<JsonAbi, D::Error> {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_seq(JsonAbiVisitor)
}
}
Expand Down
Loading