pub trait SolTypeEncode: SolTokenType + Sealed {
type AlloyType: AlloySolType;
const DEFAULT_VALUE: Self::DefaultType;
// Required method
fn tokenize(&self) -> Self::TokenType<'_>;
// Provided method
fn encode(&self) -> Vec<u8> ⓘ { ... }
}
Expand description
A Rust/ink! equivalent of a Solidity ABI type that implements logic for Solidity ABI encoding.
§Rust/ink! to Solidity ABI type mapping
Rust/ink! type | Solidity ABI type | Notes |
---|---|---|
bool | bool | |
iN for N ∈ {8,16,32,64,128} | intN | e.g i8 ↔ int8 |
uN for N ∈ {8,16,32,64,128} | uintN | e.g u8 ↔ uint8 |
U256 | uint256 | |
String | string | |
Box<str> | string | |
Address / H160 | address | Address is a type alias for the H160 type used for addresses in pallet-revive |
[T; N] for const N: usize | T[N] | e.g. [i8; 64] ↔ int8[64] |
Vec<T> | T[] | e.g. Vec<i8> ↔ int8[] |
Box<[T]> | T[] | e.g. Box<[i8]> ↔ int8[] |
FixedBytes<N> for 1 <= N <= 32 | bytesN | e.g. FixedBytes<32> ↔ bytes32 , FixedBytes<N> is just a newtype wrapper for [u8; N] |
DynBytes | bytes | DynBytes is just a newtype wrapper for Vec<u8> that also implements From<Box<[u8]>> |
(T1, T2, T3, ... T12) | (U1, U2, U3, ... U12) | where T1 ↔ U1 , … T12 ↔ U12 e.g. (bool, u8, Address) ↔ (bool, uint8, address) |
&str , &mut str | string | |
&T , &mut T , Box<T> | T | e.g. &i8 ↔ int8 |
&[T] , &mut [T] | T[] | e.g. &[i8] ↔ int8[] |
Option<T> | (bool, T) | e.g. Option<u8> ↔ (bool, uint8) |
Ref: https://docs.soliditylang.org/en/latest/abi-spec.html#types
§Option<T>
representation
Rust’s Option<T>
type doesn’t have a semantically equivalent Solidity ABI type,
because Solidity enums are field-less.
So Option<T>
is mapped to a tuple representation instead (i.e. (bool, T)
),
because this representation allows preservation of semantic information in Solidity,
by using the bool
as a “flag” indicating the variant
(i.e. false
for None
and true
for Some
) such that:
Option::None
is mapped to(false, <default_value>)
where<default_value>
is the zero bytes only representation ofT
(e.g.0u8
foru8
orVec::new()
forVec<T>
)Option::Some(value)
is mapped to(true, value)
The resulting type in Solidity can be represented as a struct with a field for the “flag” and another for the data.
Note that enum
in Solidity is encoded as uint8
in Solidity ABI
encoding, while the encoding for bool
is equivalent to the encoding
of uint8
, with true
equivalent to 1
and false
equivalent to 0
.
Therefore, the bool
“flag” can be safely interpreted as a bool
or enum
(or even
uint8
) in Solidity code.
§Note
This trait is sealed and cannot be implemented for types outside ink_primitives
.
Required Associated Constants§
Sourceconst DEFAULT_VALUE: Self::DefaultType
const DEFAULT_VALUE: Self::DefaultType
An encodable representation of the default value for this type.
Required Associated Types§
Required Methods§
Sourcefn tokenize(&self) -> Self::TokenType<'_>
fn tokenize(&self) -> Self::TokenType<'_>
Tokenizes the given value into a Self::AlloyType
token.
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.