frame_support/traits/
error.rs1use codec::{Compact, Decode, Encode};
20use core::marker::PhantomData;
21
22pub trait PalletError: Encode + Decode {
33 const MAX_ENCODED_SIZE: usize;
39}
40
41macro_rules! impl_for_types {
42 (size: $size:expr, $($typ:ty),+) => {
43 $(
44 impl PalletError for $typ {
45 const MAX_ENCODED_SIZE: usize = $size;
46 }
47 )+
48 };
49}
50
51impl_for_types!(size: 0, (), crate::Never);
52impl_for_types!(size: 1, u8, i8, bool);
53impl_for_types!(size: 2, u16, i16, Compact<u8>);
54impl_for_types!(size: 4, u32, i32, Compact<u16>);
55impl_for_types!(size: 5, Compact<u32>);
56impl_for_types!(size: 8, u64, i64);
57impl_for_types!(size: 9, Compact<u64>);
58impl_for_types!(size: 12, core::time::Duration);
60impl_for_types!(size: 16, u128, i128);
61impl_for_types!(size: 17, Compact<u128>);
62
63impl<T> PalletError for PhantomData<T> {
64 const MAX_ENCODED_SIZE: usize = 0;
65}
66
67impl<T: PalletError> PalletError for core::ops::Range<T> {
68 const MAX_ENCODED_SIZE: usize = T::MAX_ENCODED_SIZE.saturating_mul(2);
69}
70
71impl<T: PalletError, const N: usize> PalletError for [T; N] {
72 const MAX_ENCODED_SIZE: usize = T::MAX_ENCODED_SIZE.saturating_mul(N);
73}
74
75impl<T: PalletError> PalletError for Option<T> {
76 const MAX_ENCODED_SIZE: usize = T::MAX_ENCODED_SIZE.saturating_add(1);
77}
78
79impl<T: PalletError, E: PalletError> PalletError for Result<T, E> {
80 const MAX_ENCODED_SIZE: usize = if T::MAX_ENCODED_SIZE > E::MAX_ENCODED_SIZE {
81 T::MAX_ENCODED_SIZE
82 } else {
83 E::MAX_ENCODED_SIZE
84 }
85 .saturating_add(1);
86}
87
88#[impl_trait_for_tuples::impl_for_tuples(1, 18)]
89impl PalletError for Tuple {
90 const MAX_ENCODED_SIZE: usize = {
91 let mut size = 0_usize;
92 for_tuples!( #(size = size.saturating_add(Tuple::MAX_ENCODED_SIZE);)* );
93 size
94 };
95}