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 #[inline]
26 fn top_decode_or_handle_err<I, H>(input: I, h: H) -> Result<Self, H::HandledErr>
27 where
28 I: TopDecodeInput,
29 H: DecodeErrorHandler,
30 {
31 if input.byte_len() == 0 {
32 Ok(Empty)
33 } else {
34 Err(h.handle_error(DecodeError::INPUT_TOO_LONG))
35 }
36 }
37}
38
39impl NestedEncode for Empty {
40 #[inline]
41 fn dep_encode_or_handle_err<O, H>(&self, _dest: &mut O, _h: H) -> Result<(), H::HandledErr>
42 where
43 O: NestedEncodeOutput,
44 H: EncodeErrorHandler,
45 {
46 Ok(())
47 }
48}
49
50impl NestedDecode for Empty {
51 #[inline]
52 fn dep_decode_or_handle_err<I, H>(_input: &mut I, _h: H) -> Result<Self, H::HandledErr>
53 where
54 I: NestedDecodeInput,
55 H: DecodeErrorHandler,
56 {
57 Ok(Empty)
58 }
59}
60
61#[cfg(test)]
62pub mod tests {
63 use crate::test_util::{check_dep_encode_decode, check_top_encode_decode};
64 use alloc::vec::Vec;
65
66 #[test]
67 fn test_top_empty() {
68 check_top_encode_decode(super::Empty, &[]);
69 }
70
71 #[test]
72 fn test_dep_empty() {
73 check_dep_encode_decode(super::Empty, &[]);
74 }
75
76 #[test]
77 fn test_dep_unit() {
78 check_dep_encode_decode((), &[]);
79 }
80
81 #[test]
82 fn test_empty_vec_compacted() {
83 check_top_encode_decode(Vec::<u8>::new(), &[]);
84 }
85}