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