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(dyb-abi): impl ResolveSolType for Rc #189

Merged
merged 1 commit into from
Jul 13, 2023
Merged
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
67 changes: 27 additions & 40 deletions crates/dyn-abi/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,13 @@ pub trait ResolveSolType {
}

impl ResolveSolType for str {
#[inline]
fn resolve(&self) -> DynAbiResult<DynSolType> {
TypeSpecifier::try_from(self)?.resolve()
}
}

impl<T> ResolveSolType for &T
where
T: ResolveSolType,
{
fn resolve(&self) -> DynAbiResult<DynSolType> {
(**self).resolve()
}
}

impl<T> ResolveSolType for &mut T
where
T: ResolveSolType,
{
fn resolve(&self) -> DynAbiResult<DynSolType> {
(**self).resolve()
}
}

impl<T> ResolveSolType for alloc::boxed::Box<T>
where
T: ResolveSolType,
{
fn resolve(&self) -> DynAbiResult<DynSolType> {
(**self).resolve()
}
}

impl<T> ResolveSolType for alloc::sync::Arc<T>
where
T: ResolveSolType,
{
fn resolve(&self) -> DynAbiResult<DynSolType> {
(**self).resolve()
TypeSpecifier::parse(self)?.resolve()
}
}

impl ResolveSolType for RootType<'_> {
/// Resolve the type string into a basic Solidity type.
fn resolve(&self) -> DynAbiResult<DynSolType> {
match self.span() {
"address" => Ok(DynSolType::Address),
Expand Down Expand Up @@ -152,8 +116,9 @@ impl ResolveSolType for TypeStem<'_> {
impl ResolveSolType for TypeSpecifier<'_> {
#[inline]
fn resolve(&self) -> Result<DynSolType, DynAbiError> {
let ty = self.stem.resolve()?;
Ok(ty.array_wrap_from_iter(self.sizes.iter().copied()))
self.stem
.resolve()
.map(|ty| ty.array_wrap_from_iter(self.sizes.iter().copied()))
}
}

Expand Down Expand Up @@ -225,6 +190,28 @@ impl ResolveSolType for EventParam {
}
}

macro_rules! deref_impl {
($($(#[$attr:meta])* [$($gen:tt)*] $t:ty),+ $(,)?) => {$(
$(#[$attr])*
impl<$($gen)*> ResolveSolType for $t {
#[inline]
fn resolve(&self) -> DynAbiResult<DynSolType> {
(**self).resolve()
}
}
)+};
}

deref_impl! {
[] alloc::string::String,
[T: ?Sized + ResolveSolType] &T,
[T: ?Sized + ResolveSolType] &mut T,
[T: ?Sized + ResolveSolType] alloc::boxed::Box<T>,
[T: ?Sized + alloc::borrow::ToOwned + ResolveSolType] alloc::borrow::Cow<'_, T>,
[T: ?Sized + ResolveSolType] alloc::rc::Rc<T>,
[T: ?Sized + ResolveSolType] alloc::sync::Arc<T>,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down