elrond_codec/impl_for_types/
impl_phantom.rs

1use core::marker::PhantomData;
2
3use crate::{
4    DecodeError, DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput,
5    NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput,
6};
7
8/// Empty structure with an empty bytes representation. Equivalent to `false`, `0` or `[u8; 0]`, but more explicit.
9///
10/// Note: the unit type `()` would have naturally fit this role, but we decided to make the unit type multi-value only.
11
12impl<T> TopEncode for PhantomData<T> {
13    #[inline]
14    fn top_encode_or_handle_err<O, H>(&self, output: O, _h: H) -> Result<(), H::HandledErr>
15    where
16        O: TopEncodeOutput,
17        H: EncodeErrorHandler,
18    {
19        output.set_slice_u8(&[]);
20        Ok(())
21    }
22}
23
24impl<T> TopDecode for PhantomData<T> {
25    fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
26    where
27        I: TopDecodeInput,
28        H: DecodeErrorHandler,
29    {
30        if input.byte_len() == 0 {
31            Ok(PhantomData)
32        } else {
33            Err(h.handle_error(DecodeError::INPUT_TOO_LONG))
34        }
35    }
36}
37
38impl<T> NestedEncode for PhantomData<T> {
39    #[inline]
40    fn dep_encode_or_handle_err<O, H>(&self, _dest: &mut O, _h: H) -> Result<(), H::HandledErr>
41    where
42        O: NestedEncodeOutput,
43        H: EncodeErrorHandler,
44    {
45        Ok(())
46    }
47}
48
49impl<T> NestedDecode for PhantomData<T> {
50    #[inline]
51    fn dep_decode_or_handle_err<I, H>(_input: &mut I, _h: H) -> Result<Self, H::HandledErr>
52    where
53        I: NestedDecodeInput,
54        H: DecodeErrorHandler,
55    {
56        Ok(PhantomData)
57    }
58}
59
60#[cfg(test)]
61pub mod tests {
62    use crate as elrond_codec;
63    use crate::test_util::{check_dep_encode_decode, check_top_encode_decode};
64    use core::marker::PhantomData;
65    use elrond_codec_derive::{NestedDecode, NestedEncode, TopDecode, TopEncode};
66
67    #[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, PartialEq, Eq, Clone, Debug)]
68    pub struct TestStructWithPhantom<M> {
69        x: u32,
70        y: u64,
71        _phantom: PhantomData<M>,
72    }
73
74    #[test]
75    fn test_dep_unit() {
76        check_dep_encode_decode(PhantomData::<u32>, &[]);
77    }
78
79    #[test]
80    fn test_top_unit() {
81        check_top_encode_decode(PhantomData::<u32>, &[]);
82    }
83
84    #[test]
85    fn test_dep_struc() {
86        check_dep_encode_decode(
87            TestStructWithPhantom::<u64> {
88                x: 42,
89                y: 42,
90                _phantom: PhantomData::<u64>,
91            },
92            &[0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 42],
93        );
94    }
95
96    #[test]
97    fn test_top_struc() {
98        check_top_encode_decode(
99            TestStructWithPhantom::<u64> {
100                x: 42,
101                y: 42,
102                _phantom: PhantomData::<u64>,
103            },
104            &[0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 42],
105        );
106    }
107}