Skip to content

Commit

Permalink
feat(dyn-abi): add match functions to value and doc aliases (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Aug 22, 2023
1 parent 23d108e commit 691ddf1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
17 changes: 12 additions & 5 deletions crates/dyn-abi/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,15 @@ impl DynSolType {
}
}

/// Check that a given [`DynSolValue`] matches this type.
/// Check that the given [`DynSolValue`]s match these types.
///
/// See [`Self::matches`] for more information.
#[inline]
pub fn matches_many(types: &[Self], values: &[DynSolValue]) -> bool {
types.len() == values.len() && types.iter().zip(values).all(|(t, v)| t.matches(v))
}

/// Check that the given [`DynSolValue`] matches this type.
///
/// Note: this will not check any names, but just the types; e.g for
/// `CustomStruct`, when the "eip712" feature is enabled, this will only
Expand All @@ -238,10 +246,9 @@ impl DynSolType {
value,
DynSolValue::FixedArray(v) if v.len() == *size && v.iter().all(|v| t.matches(v))
),
Self::Tuple(types) => match value {
as_tuple!(DynSolValue tuple) => types.iter().zip(tuple).all(|(t, v)| t.matches(v)),
_ => false,
},
Self::Tuple(types) => {
matches!(value, as_tuple!(DynSolValue tuple) if types.iter().zip(tuple).all(|(t, v)| t.matches(v)))
}
#[cfg(feature = "eip712")]
Self::CustomStruct {
name: _,
Expand Down
18 changes: 18 additions & 0 deletions crates/dyn-abi/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,24 @@ impl DynSolValue {
}
}

/// Check that these values have the same type as the given [`DynSolType`]s.
///
/// See [`DynSolType::matches`] for more information.
#[doc(alias = "types_check")] // from ethabi
#[inline(always)]
pub fn matches_many(values: &[Self], types: &[DynSolType]) -> bool {
DynSolType::matches_many(types, values)
}

/// Check that this value has the same type as the given [`DynSolType`].
///
/// See [`DynSolType::matches`] for more information.
#[doc(alias = "type_check")] // from ethabi
#[inline(always)]
pub fn matches(&self, ty: &DynSolType) -> bool {
ty.matches(self)
}

/// Returns the number of words this type uses in the head of the ABI blob.
#[inline]
pub(crate) fn head_words(&self) -> usize {
Expand Down

0 comments on commit 691ddf1

Please sign in to comment.