Skip to content

Commit

Permalink
Add FromAbiIter trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Nov 14, 2023
1 parent cd47364 commit c55974d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub use self::contract::{
};
pub use self::signature::{extend_signature_with_id, sign_with_signature_id};
pub use self::traits::{
FromAbi, FromPlainAbi, IgnoreName, IntoAbi, IntoPlainAbi, WithAbiType, WithPlainAbiType,
FromAbi, FromAbiIter, FromPlainAbi, IgnoreName, IntoAbi, IntoPlainAbi, WithAbiType,
WithPlainAbiType,
};
pub use self::ty::{AbiHeaderType, AbiType, NamedAbiType, PlainAbiType};
pub use self::value::{AbiHeader, AbiValue, NamedAbiValue, PlainAbiValue};
Expand Down
28 changes: 28 additions & 0 deletions src/abi/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,34 @@ impl<T: FromAbi> FromAbi for Rc<T> {
}
}

/// A wrapper around ABI values iterator that converts
/// each item using the [`FromAbi`] trait.
///
/// It should be used to parse fields as tuple items
/// for some struct `T` (which must implement [`WithAbiType`]).
pub trait FromAbiIter<T> {
/// Advances the iterator and returns the next value.
fn next_value<V: FromAbi>(&mut self) -> Result<V>;
}

impl<T, I> FromAbiIter<T> for I
where
T: WithAbiType,
I: Iterator<Item = AbiValue>,
{
fn next_value<V: FromAbi>(&mut self) -> Result<V> {
match Iterator::next(self) {
Some(value) => V::from_abi(value),
None => Err(anyhow::Error::from(
crate::abi::error::AbiError::TypeMismatch {
expected: T::abi_type().to_string().into_boxed_str(),
ty: Box::from("tuple part"),
},
)),
}
}
}

#[cfg(test)]
mod tests {
use ahash::HashSet;
Expand Down

0 comments on commit c55974d

Please sign in to comment.