stylus_sdk/abi/export/
internal.rs

1// Copyright 2024, Offchain Labs, Inc.
2// For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/main/licenses/COPYRIGHT.md
3
4//! This module provides functions for code generated by `stylus-sdk-proc` for the `export-abi` command.
5//! Most users shouldn't call these.
6
7use core::any::TypeId;
8
9use alloy_primitives::{Address, FixedBytes, Signed, Uint};
10
11use crate::abi::Bytes;
12
13/// Represents a unique Solidity Type.
14pub struct InnerType {
15    /// Full interface string.
16    pub name: String,
17    /// Unique identifier for de-duplication when printing interfaces.
18    pub id: TypeId,
19}
20
21/// Trait for collecting structs and error types.
22pub trait InnerTypes {
23    /// Collect any structs and errors under the type.
24    /// Empty for primitives.
25    fn inner_types() -> Vec<InnerType> {
26        vec![]
27    }
28}
29
30impl<O, E> InnerTypes for Result<O, E>
31where
32    O: InnerTypes,
33    E: InnerTypes,
34{
35    fn inner_types() -> Vec<InnerType> {
36        let mut out = O::inner_types();
37        out.extend(E::inner_types());
38        out
39    }
40}
41
42impl<T: InnerTypes> InnerTypes for Vec<T> {
43    fn inner_types() -> Vec<InnerType> {
44        T::inner_types()
45    }
46}
47
48impl<const N: usize, T: InnerTypes> InnerTypes for [T; N] {
49    fn inner_types() -> Vec<InnerType> {
50        T::inner_types()
51    }
52}
53
54macro_rules! impl_inner {
55    ($ty:ident $($rest:ident)+) => {
56        impl_inner!($ty);
57        impl_inner!($($rest)+);
58    };
59    ($ty:ident) => {
60        impl InnerTypes for $ty {}
61    };
62}
63
64impl_inner!(bool u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 String Address Bytes);
65
66impl<const B: usize, const L: usize> InnerTypes for Uint<B, L> {}
67impl<const B: usize, const L: usize> InnerTypes for Signed<B, L> {}
68impl<const N: usize> InnerTypes for FixedBytes<N> {}
69
70macro_rules! impl_tuple {
71    () => {
72        impl InnerTypes for () {}
73    };
74    ($first:ident $(, $rest:ident)*) => {
75        impl<$first: InnerTypes $(, $rest: InnerTypes)*> InnerTypes for ( $first $(, $rest)* , ) {
76            fn inner_types() -> Vec<InnerType> {
77                vec![]
78            }
79        }
80
81        impl_tuple! { $($rest),* }
82    };
83}
84
85impl_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X);