multiversx_sc_codec/impl_for_types/
impl_empty.rs1use crate::{
2 DecodeError, DecodeErrorHandler, EncodeErrorHandler, NestedDecode, NestedDecodeInput,
3 NestedEncode, NestedEncodeOutput, TopDecode, TopDecodeInput, TopEncode, TopEncodeOutput,
4};
5
6#[derive(Debug, PartialEq, Eq)]
10pub struct Empty;
11
12impl TopEncode for Empty {
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 TopDecode for Empty {
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(Empty)
32 } else {
33 Err(h.handle_error(DecodeError::INPUT_TOO_LONG))
34 }
35 }
36}
37
38impl NestedEncode for Empty {
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 NestedDecode for Empty {
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(Empty)
57 }
58}
59
60#[cfg(test)]
61pub mod tests {
62 use crate::test_util::{check_dep_encode_decode, check_top_encode_decode};
63 use alloc::vec::Vec;
64
65 #[test]
66 fn test_top_empty() {
67 check_top_encode_decode(super::Empty, &[]);
68 }
69
70 #[test]
71 fn test_dep_empty() {
72 check_dep_encode_decode(super::Empty, &[]);
73 }
74
75 #[test]
76 fn test_dep_unit() {
77 check_dep_encode_decode((), &[]);
78 }
79
80 #[test]
81 fn test_empty_vec_compacted() {
82 check_top_encode_decode(Vec::<u8>::new(), &[]);
83 }
84}