alloy_primitives/bits/
rlp.rs

1use super::FixedBytes;
2use alloy_rlp::{length_of_length, Decodable, Encodable, MaxEncodedLen, MaxEncodedLenAssoc};
3
4impl<const N: usize> Decodable for FixedBytes<N> {
5    #[inline]
6    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
7        Decodable::decode(buf).map(Self)
8    }
9}
10
11impl<const N: usize> Encodable for FixedBytes<N> {
12    #[inline]
13    fn length(&self) -> usize {
14        self.0.length()
15    }
16
17    #[inline]
18    fn encode(&self, out: &mut dyn bytes::BufMut) {
19        self.0.encode(out);
20    }
21}
22
23// cannot implement this with const generics due to Rust issue #76560:
24// https://github.com/rust-lang/rust/issues/76560
25macro_rules! fixed_bytes_max_encoded_len {
26    ($($sz:literal),+) => {$(
27        unsafe impl MaxEncodedLen<{ $sz + length_of_length($sz) }> for FixedBytes<$sz> {}
28    )+};
29}
30
31fixed_bytes_max_encoded_len!(0, 1, 2, 4, 8, 16, 20, 32, 64, 128, 256, 512, 1024);
32
33unsafe impl<const N: usize> MaxEncodedLenAssoc for FixedBytes<N> {
34    const LEN: usize = N + length_of_length(N);
35}