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

feat: SolEnum and SolInterface #153

Merged
merged 22 commits into from
Jun 30, 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
64 changes: 29 additions & 35 deletions crates/dyn-abi/src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,15 @@ impl DynSolType {
}
Self::String => matches!(value, DynSolValue::String(_)),
Self::FixedBytes(size) => matches!(value, DynSolValue::FixedBytes(_, s) if s == size),
Self::FixedArray(t, size) => {
matches!(value, DynSolValue::FixedArray(v) if v.len() == *size && v.iter().all(|v| t.matches(v)))
}
Self::Tuple(types) => {
if let DynSolValue::CustomStruct { tuple: t, .. } = value {
types.iter().zip(t).all(|(t, v)| t.matches(v))
} else if let DynSolValue::Tuple(v) = value {
types.iter().zip(v).all(|(t, v)| t.matches(v))
} else {
false
}
}
Self::FixedArray(t, size) => matches!(
value,
DynSolValue::FixedArray(v) if v.len() == *size && v.iter().all(|v| t.matches(v))
),
Self::Tuple(types) => matches!(
value,
DynSolValue::CustomStruct { tuple, .. } | DynSolValue::Tuple(tuple)
if types.iter().zip(tuple).all(|(t, v)| t.matches(v))
),
Self::CustomStruct {
name,
prop_names,
Expand All @@ -126,7 +123,7 @@ impl DynSolType {
}
}
Self::CustomValue { name } => {
matches! {value, DynSolValue::CustomValue { name: n, .. } if name == n}
matches!(value, DynSolValue::CustomValue { name: n, .. } if name == n)
}
}
}
Expand Down Expand Up @@ -165,34 +162,31 @@ impl DynSolType {
"tuple length mismatch on dynamic detokenization",
))
}
Ok(DynSolValue::Tuple(
types
.iter()
.zip(tokens.into_owned())
.map(|(t, w)| t.detokenize(w))
.collect::<Result<_, _>>()?,
))
types
.iter()
.zip(tokens.into_owned())
.map(|(t, w)| t.detokenize(w))
.collect::<Result<_>>()
.map(DynSolValue::Tuple)
}
(DynSolType::Array(t), DynToken::DynSeq { contents, .. }) => Ok(DynSolValue::Array(
contents
.into_owned()
.into_iter()
.map(|tok| t.detokenize(tok))
.collect::<Result<_, _>>()?,
)),
(DynSolType::Array(t), DynToken::DynSeq { contents, .. }) => contents
.into_owned()
.into_iter()
.map(|tok| t.detokenize(tok))
.collect::<Result<_>>()
.map(DynSolValue::Array),
(DynSolType::FixedArray(t, size), DynToken::FixedSeq(tokens, _)) => {
if *size != tokens.len() {
return Err(crate::Error::custom(
"array length mismatch on dynamic detokenization",
))
}
Ok(DynSolValue::FixedArray(
tokens
.into_owned()
.into_iter()
.map(|tok| t.detokenize(tok))
.collect::<Result<_, _>>()?,
))
tokens
.into_owned()
.into_iter()
.map(|tok| t.detokenize(tok))
.collect::<Result<_>>()
.map(DynSolValue::FixedArray)
}
(
DynSolType::CustomStruct {
Expand All @@ -211,7 +205,7 @@ impl DynSolType {
.iter()
.zip(tokens.into_owned())
.map(|(t, w)| t.detokenize(w))
.collect::<Result<_, _>>()?;
.collect::<Result<_>>()?;

Ok(DynSolValue::CustomStruct {
name: name.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/dyn-abi/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl DynSolValue {
/// Tokenize this value into a [`DynToken`].
pub fn tokenize(&self) -> DynToken<'_> {
match self {
DynSolValue::Address(a) => (*a).into(),
DynSolValue::Address(a) => a.into_word().into(),
DynSolValue::Bool(b) => Word::with_last_byte(*b as u8).into(),
DynSolValue::Bytes(buf) => DynToken::PackedSeq(buf),
DynSolValue::FixedBytes(buf, _) => (*buf).into(),
Expand Down
7 changes: 0 additions & 7 deletions crates/primitives/src/bits/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ wrap_fixed_bytes!(
pub struct Address<20>;
);

impl From<Address> for FixedBytes<32> {
#[inline]
fn from(addr: Address) -> Self {
addr.into_word()
}
}

impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut buf = [0; 42];
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/src/bits/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ impl<const N: usize> FixedBytes<N> {
/// Instantiates a new fixed hash with cryptographically random content.
#[cfg(feature = "getrandom")]
pub fn try_random() -> Result<Self, getrandom::Error> {
let mut bytes: [_; N] = super::impl_core::uninit_array();
let mut bytes: [_; N] = crate::impl_core::uninit_array();
getrandom::getrandom_uninit(&mut bytes)?;
// SAFETY: The array is initialized by getrandom_uninit.
Ok(Self(unsafe { super::impl_core::array_assume_init(bytes) }))
Ok(Self(unsafe { crate::impl_core::array_assume_init(bytes) }))
}

/// Concatenate two `FixedBytes`.
Expand Down
1 change: 0 additions & 1 deletion crates/primitives/src/bits/impl_core.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/primitives/src/bits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ mod rlp;

#[cfg(feature = "serde")]
mod serde;

mod impl_core;
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl<T> Ext for T {
}

/// [`core::array::try_from_fn`]
#[inline]
pub(crate) fn try_from_fn<F, T, E, const N: usize>(mut cb: F) -> Result<[T; N], E>
where
F: FnMut(usize) -> Result<T, E>,
Expand Down Expand Up @@ -64,6 +65,15 @@ where
Ok(unsafe { array_assume_init(array) })
}

/// [`slice::split_array_ref`]
#[inline]
#[track_caller]
pub(crate) fn split_array_ref<T, const N: usize>(slice: &[T]) -> (&[T; N], &[T]) {
let (a, b) = slice.split_at(N);
// SAFETY: a points to [T; N]? Yes it's [T] of length N (checked by split_at)
unsafe { (&*(a.as_ptr() as *const [T; N]), b) }
}

/// [`MaybeUninit::slice_assume_init_mut`]
#[inline(always)]
unsafe fn slice_assume_init_mut<T>(slice: &mut [MaybeUninit<T>]) -> &mut [T] {
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub use bits::{
mod bytes;
pub use self::bytes::Bytes;

#[cfg(feature = "getrandom")]
mod impl_core;

mod signed;
pub use signed::{BigIntConversionError, ParseSignedError, Sign, Signed};

Expand Down
1 change: 1 addition & 0 deletions crates/sol-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
syn-solidity = { workspace = true, features = ["visit", "visit-mut"] }

heck = "0.4"
proc-macro2.workspace = true
quote.workspace = true
syn = { workspace = true, features = ["extra-traits"] }
Expand Down
Loading