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: make TokenType::is_dynamic a constant #114

Merged
merged 1 commit into from
Jun 15, 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
57 changes: 18 additions & 39 deletions crates/sol-types/src/coder/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>;
Expand Down Expand Up @@ -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<Self> {
Expand Down Expand Up @@ -216,16 +213,11 @@ impl<T, const N: usize> AsRef<[T; N]> for FixedSeqToken<T, N> {
}

impl<T: TokenType, const N: usize> TokenType for FixedSeqToken<T, N> {
#[inline]
fn is_dynamic() -> bool {
T::is_dynamic()
}
const DYNAMIC: bool = T::DYNAMIC;

#[inline]
fn decode_from(dec: &mut Decoder<'_>) -> Result<Self> {
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()
Expand All @@ -236,7 +228,7 @@ impl<T: TokenType, const N: usize> TokenType for FixedSeqToken<T, N> {

#[inline]
fn head_words(&self) -> usize {
if Self::is_dynamic() {
if Self::DYNAMIC {
1
} else {
self.0.iter().map(TokenType::head_words).sum()
Expand All @@ -245,7 +237,7 @@ impl<T: TokenType, const N: usize> TokenType for FixedSeqToken<T, N> {

#[inline]
fn tail_words(&self) -> usize {
if Self::is_dynamic() {
if Self::DYNAMIC {
N
} else {
0
Expand All @@ -254,7 +246,7 @@ impl<T: TokenType, const N: usize> TokenType for FixedSeqToken<T, N> {

#[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))
Expand All @@ -263,7 +255,7 @@ impl<T: TokenType, const N: usize> TokenType for FixedSeqToken<T, N> {

#[inline]
fn tail_append(&self, enc: &mut Encoder) {
if Self::is_dynamic() {
if Self::DYNAMIC {
self.encode_sequence(enc)
}
}
Expand Down Expand Up @@ -332,10 +324,7 @@ impl<T> AsRef<[T]> for DynSeqToken<T> {
}

impl<T: TokenType> TokenType for DynSeqToken<T> {
#[inline]
fn is_dynamic() -> bool {
true
}
const DYNAMIC: bool = true;

fn decode_from(dec: &mut Decoder<'_>) -> Result<Self> {
let mut child = dec.take_indirection()?;
Expand Down Expand Up @@ -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<Self> {
Expand Down Expand Up @@ -478,25 +464,21 @@ 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<Self> {
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()
};

let res = Self::decode_sequence(&mut child)?;

if !is_dynamic {
if !Self::DYNAMIC {
dec.take_offset(child);
}

Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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() )+;

Expand Down Expand Up @@ -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<Self> {
Expand Down
6 changes: 3 additions & 3 deletions crates/sol-types/src/types/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -54,7 +54,7 @@ pub trait SolType {
const ENCODED_SIZE: Option<usize> = 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>;
Expand Down