Enum DynSolValue

Source
pub enum DynSolValue {
    Bool(bool),
    Int(I256, usize),
    Uint(U256, usize),
    FixedBytes(Word, usize),
    Address(Address),
    Function(Function),
    Bytes(Vec<u8>),
    String(String),
    Array(Vec<DynSolValue>),
    FixedArray(Vec<DynSolValue>),
    Tuple(Vec<DynSolValue>),
    CustomStruct {
        name: String,
        prop_names: Vec<String>,
        tuple: Vec<DynSolValue>,
    },
}
Expand description

A dynamic Solidity value.

It is broadly similar to serde_json::Value in that it is an enum of possible types, and the user must inspect and disambiguate.

§Examples

Basic usage:

use alloy_dyn_abi::{DynSolType, DynSolValue};

let ty: DynSolType = "uint64".parse()?;
let value: DynSolValue = 183u64.into();

let encoded: Vec<u8> = value.abi_encode();
let decoded: DynSolValue = ty.abi_decode(&encoded)?;

assert_eq!(decoded, value);

Coerce a string using DynSolType:

use alloy_dyn_abi::{DynSolType, DynSolValue};
use alloy_primitives::U256;

let ty: DynSolType = "(string, uint256)".parse()?;
let value = ty.coerce_str("(foo bar, 2.5 gwei)")?;
assert_eq!(
    value,
    DynSolValue::Tuple(vec![
        DynSolValue::String(String::from("foo bar")),
        DynSolValue::Uint(U256::from(2_500_000_000u64), 256)
    ]),
);

Variants§

§

Bool(bool)

A boolean.

§

Int(I256, usize)

A signed integer. The second parameter is the number of bits, not bytes.

§

Uint(U256, usize)

An unsigned integer. The second parameter is the number of bits, not bytes.

§

FixedBytes(Word, usize)

A fixed-length byte array. The second parameter is the number of bytes.

§

Address(Address)

An address.

§

Function(Function)

A function pointer.

§

Bytes(Vec<u8>)

A dynamic-length byte array.

§

String(String)

A string.

§

Array(Vec<DynSolValue>)

A dynamically-sized array of values.

§

FixedArray(Vec<DynSolValue>)

A fixed-size array of values.

§

Tuple(Vec<DynSolValue>)

A tuple of values.

§

CustomStruct

A named struct, treated as a tuple with a name parameter.

Fields

§name: String

The name of the struct.

§prop_names: Vec<String>

The struct’s prop names, in declaration order.

§tuple: Vec<DynSolValue>

The inner types.

Implementations§

Source§

impl DynSolValue

Source

pub fn arbitrary_from_type( ty: &DynSolType, u: &mut Unstructured<'_>, ) -> Result<Self>

Generate an arbitrary DynSolValue from the given DynSolType.

Source

pub fn type_strategy(ty: &DynSolType) -> SBoxedStrategy<Self>

Create a proptest strategy to generate DynSolValues from the given type.

Source

pub fn value_strategy(&self) -> SBoxedStrategy<Self>

Create a proptest strategy to generate DynSolValues from the given value’s type.

Source§

impl DynSolValue

Source

pub fn as_type(&self) -> Option<DynSolType>

The Solidity type. This returns the Solidity type corresponding to this value, if it is known. A type will not be known if the value contains an empty sequence, e.g. T[0].

Source

pub fn sol_type_name(&self) -> Option<Cow<'static, str>>

The Solidity type name. This returns the Solidity type corresponding to this value, if it is known. A type will not be known if the value contains an empty sequence, e.g. T[0].

Source

pub const fn is_word(&self) -> bool

Trust if this value is encoded as a single word. False otherwise.

Source

pub fn as_word(&self) -> Option<Word>

Fallible cast to a single word. Will succeed for any single-word type.

Source

pub const fn as_address(&self) -> Option<Address>

Fallible cast to the contents of a variant DynSolValue {.

Source

pub const fn as_bool(&self) -> Option<bool>

Fallible cast to the contents of a variant.

Source

pub fn as_bytes(&self) -> Option<&[u8]>

Fallible cast to the contents of a variant.

Source

pub const fn as_fixed_bytes(&self) -> Option<(&[u8], usize)>

Fallible cast to the contents of a variant.

Source

pub const fn as_int(&self) -> Option<(I256, usize)>

Fallible cast to the contents of a variant.

Source

pub const fn as_uint(&self) -> Option<(U256, usize)>

Fallible cast to the contents of a variant.

Source

pub fn as_str(&self) -> Option<&str>

Fallible cast to the contents of a variant.

Source

pub fn as_tuple(&self) -> Option<&[Self]>

Fallible cast to the contents of a variant.

Source

pub fn as_array(&self) -> Option<&[Self]>

Fallible cast to the contents of a variant.

Source

pub fn as_fixed_array(&self) -> Option<&[Self]>

Fallible cast to the contents of a variant.

Source

pub fn as_custom_struct(&self) -> Option<(&str, &[String], &[Self])>

Fallible cast to the contents of a variant.

Source

pub fn has_custom_struct(&self) -> bool

Returns whether this type is contains a custom struct.

Source

pub const fn is_sequence(&self) -> bool

Returns true if the value is a sequence type.

Source

pub fn as_fixed_seq(&self) -> Option<&[Self]>

Fallible cast to a fixed-size array. Any of a FixedArray, a Tuple, or a CustomStruct.

Source

pub fn as_packed_seq(&self) -> Option<&[u8]>

Fallible cast to a packed sequence. Any of a String, or a Bytes.

Source

pub fn is_dynamic(&self) -> bool

Returns true if the value is an instance of a dynamically sized type.

Source

pub fn matches_many(values: &[Self], types: &[DynSolType]) -> bool

Check that these values have the same type as the given DynSolTypes.

See DynSolType::matches for more information.

Source

pub fn matches(&self, ty: &DynSolType) -> bool

Check that this value has the same type as the given DynSolType.

See DynSolType::matches for more information.

Source

pub fn head_append(&self, enc: &mut Encoder)

Append this data to the head of an in-progress blob via the encoder.

Source

pub fn tail_append(&self, enc: &mut Encoder)

Append this data to the tail of an in-progress blob via the encoder.

Source

pub fn abi_encode_packed(&self) -> Vec<u8>

Non-standard Packed Mode ABI encoding.

Note that invalid value sizes will saturate to the maximum size, e.g. Uint(x, 300) will behave the same as Uint(x, 256).

See SolType::abi_encode_packed for more details.

Source

pub fn abi_encode_packed_to(&self, buf: &mut Vec<u8>)

Non-standard Packed Mode ABI encoding.

See abi_encode_packed for more details.

Source

pub fn abi_packed_encoded_size(&self) -> usize

Returns the length of this value when ABI-encoded in Non-standard Packed Mode.

See abi_encode_packed for more details.

Source

pub fn tokenize(&self) -> DynToken<'_>

Tokenize this value into a DynToken.

Source

pub fn abi_encode(&self) -> Vec<u8>

Encode this value into a byte array by wrapping it into a 1-element sequence.

Source

pub fn abi_encode_params(&self) -> Vec<u8>

Encode this value into a byte array suitable for passing to a function. If this value is a tuple, it is encoded as is. Otherwise, it is wrapped into a 1-element sequence.

§Examples
// Encoding for function foo(address)
DynSolValue::Address(_).abi_encode_params();

// Encoding for function foo(address, uint256)
DynSolValue::Tuple(vec![
    DynSolValue::Address(_),
    DynSolValue::Uint(_, 256),
]).abi_encode_params();
Source

pub fn abi_encode_sequence(&self) -> Option<Vec<u8>>

If this value is a fixed sequence, encode it into a byte array. If this value is not a fixed sequence, return None.

Trait Implementations§

Source§

impl<'a> Arbitrary<'a> for DynSolValue

Source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
Source§

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

impl Arbitrary for DynSolValue

Source§

type Parameters = (u32, u32, u32)

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
Source§

type Strategy = Recursive<DynSolValue, fn(BoxedStrategy<DynSolValue>) -> TupleUnion<((u32, Arc<BoxedStrategy<DynSolValue>>), (u32, Arc<Map<Flatten<Map<BoxedStrategy<DynSolValue>, fn(<BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>>, fn(<Flatten<Map<BoxedStrategy<DynSolValue>, fn(<BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>> as Strategy>::Value) -> DynSolValue>>), (u32, Arc<Map<Flatten<Map<BoxedStrategy<DynSolValue>, fn(<BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>>, fn(<Flatten<Map<BoxedStrategy<DynSolValue>, fn(<BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>> as Strategy>::Value) -> DynSolValue>>), (u32, Arc<Map<VecStrategy<BoxedStrategy<DynSolValue>>, fn(<VecStrategy<BoxedStrategy<DynSolValue>> as Strategy>::Value) -> DynSolValue>>), (u32, Arc<BoxedStrategy<DynSolValue>>))>>

The type of Strategy used to generate values of type Self.
Source§

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
Source§

fn arbitrary_with(args: Self::Parameters) -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
Source§

impl Clone for DynSolValue

Source§

fn clone(&self) -> DynSolValue

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DynSolValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> From<[DynSolValue; N]> for DynSolValue

Source§

fn from(value: [Self; N]) -> Self

Converts to this type from the input type.
Source§

impl From<Address> for DynSolValue

Source§

fn from(value: Address) -> Self

Converts to this type from the input type.
Source§

impl From<FixedBytes<32>> for DynSolValue

Source§

fn from(value: Word) -> Self

Converts to this type from the input type.
Source§

impl From<Signed<256, 4>> for DynSolValue

Source§

fn from(value: I256) -> Self

Converts to this type from the input type.
Source§

impl From<String> for DynSolValue

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<Uint<256, 4>> for DynSolValue

Source§

fn from(value: U256) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<DynSolValue>> for DynSolValue

Source§

fn from(value: Vec<Self>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for DynSolValue

Source§

fn from(value: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for DynSolValue

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for DynSolValue

Source§

fn from(value: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for DynSolValue

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for DynSolValue

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for DynSolValue

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for DynSolValue

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for DynSolValue

Source§

fn from(value: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for DynSolValue

Source§

fn from(value: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for DynSolValue

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for DynSolValue

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for DynSolValue

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for DynSolValue

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for DynSolValue

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for DynSolValue

Source§

fn eq(&self, other: &DynSolValue) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for DynSolValue

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pipe for T
where T: ?Sized,

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
Source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
Source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V