diff --git a/crates/sol-types/src/coder/token.rs b/crates/sol-types/src/coder/token.rs index afdaf896ab..b73f652c2d 100644 --- a/crates/sol-types/src/coder/token.rs +++ b/crates/sol-types/src/coder/token.rs @@ -38,7 +38,7 @@ use sealed::Sealed; /// state between abi-encoded blobs, and rust types. pub trait TokenType: Sealed + Sized { /// True if the token represents a dynamically-sized type. - fn is_dynamic() -> bool; + const DYNAMIC: bool; /// Decode a token from a decoder. fn decode_from(dec: &mut Decoder<'_>) -> Result; @@ -151,10 +151,7 @@ impl AsRef<[u8]> for WordToken { } impl TokenType for WordToken { - #[inline] - fn is_dynamic() -> bool { - false - } + const DYNAMIC: bool = false; #[inline] fn decode_from(dec: &mut Decoder<'_>) -> Result { @@ -216,16 +213,11 @@ impl AsRef<[T; N]> for FixedSeqToken { } impl TokenType for FixedSeqToken { - #[inline] - fn is_dynamic() -> bool { - T::is_dynamic() - } + const DYNAMIC: bool = T::DYNAMIC; #[inline] fn decode_from(dec: &mut Decoder<'_>) -> Result { - let is_dynamic = Self::is_dynamic(); - - let mut child = if is_dynamic { + let mut child = if Self::DYNAMIC { dec.take_indirection()? } else { dec.raw_child() @@ -236,7 +228,7 @@ impl TokenType for FixedSeqToken { #[inline] fn head_words(&self) -> usize { - if Self::is_dynamic() { + if Self::DYNAMIC { 1 } else { self.0.iter().map(TokenType::head_words).sum() @@ -245,7 +237,7 @@ impl TokenType for FixedSeqToken { #[inline] fn tail_words(&self) -> usize { - if Self::is_dynamic() { + if Self::DYNAMIC { N } else { 0 @@ -254,7 +246,7 @@ impl TokenType for FixedSeqToken { #[inline] fn head_append(&self, enc: &mut Encoder) { - if Self::is_dynamic() { + if Self::DYNAMIC { enc.append_indirection(); } else { self.0.iter().for_each(|inner| inner.head_append(enc)) @@ -263,7 +255,7 @@ impl TokenType for FixedSeqToken { #[inline] fn tail_append(&self, enc: &mut Encoder) { - if Self::is_dynamic() { + if Self::DYNAMIC { self.encode_sequence(enc) } } @@ -332,10 +324,7 @@ impl AsRef<[T]> for DynSeqToken { } impl TokenType for DynSeqToken { - #[inline] - fn is_dynamic() -> bool { - true - } + const DYNAMIC: bool = true; fn decode_from(dec: &mut Decoder<'_>) -> Result { let mut child = dec.take_indirection()?; @@ -413,10 +402,7 @@ impl AsRef<[u8]> for PackedSeqToken { } impl TokenType for PackedSeqToken { - #[inline] - fn is_dynamic() -> bool { - true - } + const DYNAMIC: bool = true; #[inline] fn decode_from(dec: &mut Decoder<'_>) -> Result { @@ -478,17 +464,13 @@ macro_rules! tuple_impls { #[allow(non_snake_case)] impl<$($ty: TokenType,)+> TokenType for ($($ty,)+) { - #[inline] - fn is_dynamic() -> bool { - $( <$ty as TokenType>::is_dynamic() )||+ - } + const DYNAMIC: bool = $( <$ty as TokenType>::DYNAMIC )||+; #[inline] fn decode_from(dec: &mut Decoder<'_>) -> Result { - let is_dynamic = Self::is_dynamic(); // The first element in a dynamic Tuple is an offset to the Tuple's data // For a static Tuple the data begins right away - let mut child = if is_dynamic { + let mut child = if Self::DYNAMIC { dec.take_indirection()? } else { dec.raw_child() @@ -496,7 +478,7 @@ macro_rules! tuple_impls { let res = Self::decode_sequence(&mut child)?; - if !is_dynamic { + if !Self::DYNAMIC { dec.take_offset(child); } @@ -505,7 +487,7 @@ macro_rules! tuple_impls { #[inline] fn head_words(&self) -> usize { - if Self::is_dynamic() { + if Self::DYNAMIC { 1 } else { let ($($ty,)+) = self; @@ -515,7 +497,7 @@ macro_rules! tuple_impls { #[inline] fn tail_words(&self) -> usize { - if Self::is_dynamic() { + if Self::DYNAMIC { self.total_words() } else { 0 @@ -529,7 +511,7 @@ macro_rules! tuple_impls { } fn head_append(&self, enc: &mut Encoder) { - if Self::is_dynamic() { + if Self::DYNAMIC { enc.append_indirection(); } else { let ($($ty,)+) = self; @@ -540,7 +522,7 @@ macro_rules! tuple_impls { } fn tail_append(&self, enc: &mut Encoder) { - if Self::is_dynamic() { + if Self::DYNAMIC { let ($($ty,)+) = self; let head_words = 0 $( + $ty.head_words() )+; @@ -585,10 +567,7 @@ macro_rules! tuple_impls { } impl TokenType for () { - #[inline] - fn is_dynamic() -> bool { - false - } + const DYNAMIC: bool = false; #[inline] fn decode_from(_dec: &mut Decoder<'_>) -> Result { diff --git a/crates/sol-types/src/types/type.rs b/crates/sol-types/src/types/type.rs index 3689d2c0c5..6a584780d2 100644 --- a/crates/sol-types/src/types/type.rs +++ b/crates/sol-types/src/types/type.rs @@ -20,8 +20,8 @@ use crate::{no_std_prelude::*, token::TokenSeq, Result, TokenType, Word}; /// ``` /// /// These types are zero cost representations of Solidity types. They do not -/// exist at runtime. They ONLY information about the type, they do not carry -/// data +/// exist at runtime. They ONLY contain information about the type, they do not +/// carry any data. /// /// ### Implementer's Guide /// @@ -54,7 +54,7 @@ pub trait SolType { const ENCODED_SIZE: Option = Some(32); /// Whether the encoded size is dynamic. - const DYNAMIC: bool = { Self::ENCODED_SIZE.is_none() }; + const DYNAMIC: bool = Self::ENCODED_SIZE.is_none(); /// The name of the type in Solidity. fn sol_type_name() -> Cow<'static, str>;