1use alloc::boxed::Box;
2use alloc::collections::{BTreeMap, VecDeque};
3use alloc::vec::Vec;
4
5use crate::cowstr::CowStr;
6use crate::{DecodeError, ESExpr, ESExprCodec, ESExprDictCodec, ESExprEncodedEq, ESExprOptionalFieldCodec, ESExprTagSet, ESExprVarArgCodec};
7
8impl<A: ESExprEncodedEq> ESExprEncodedEq for Box<A> {
9 fn is_encoded_eq(&self, other: &Self) -> bool {
10 A::is_encoded_eq(self, other)
11 }
12}
13
14impl<'a, A: ESExprCodec<'a>> ESExprCodec<'a> for Box<A> {
15 const TAGS: ESExprTagSet = A::TAGS;
16
17 fn encode_esexpr(&'a self) -> ESExpr<'a> {
18 A::encode_esexpr(&**self)
19 }
20
21 fn decode_esexpr(expr: ESExpr<'a>) -> Result<Self, DecodeError> {
22 A::decode_esexpr(expr).map(Box::new)
23 }
24}
25
26impl<'a, F: ESExprOptionalFieldCodec<'a>> ESExprOptionalFieldCodec<'a> for Box<F> {
27 type Element = F::Element;
28
29
30 fn encode_optional_field(&'a self) -> Option<ESExpr<'a>> {
31 (**self).encode_optional_field()
32 }
33
34 fn decode_optional_field(value: Option<ESExpr<'a>>) -> Result<Self, DecodeError> {
35 F::decode_optional_field(value).map(Box::new)
36 }
37}
38
39impl<'a, F: ESExprVarArgCodec<'a>> ESExprVarArgCodec<'a> for Box<F> {
40 type Element = F::Element;
41
42 fn encode_vararg_element(&'a self, args: &mut Vec<ESExpr<'a>>) {
43 (**self).encode_vararg_element(args);
44 }
45
46 fn decode_vararg_element(
47 args: &mut VecDeque<ESExpr<'a>>,
48 constructor_name: &str,
49 start_index: &mut usize,
50 ) -> Result<Self, DecodeError> {
51 F::decode_vararg_element(args, constructor_name, start_index).map(Box::new)
52 }
53}
54
55impl<'a, F: ESExprDictCodec<'a>> ESExprDictCodec<'a> for Box<F> {
56 type Element = F::Element;
57
58 fn encode_dict_element(&'a self, kwargs: &mut BTreeMap<CowStr<'a>, ESExpr<'a>>) {
59 (**self).encode_dict_element(kwargs);
60 }
61
62 fn decode_dict_element(
63 kwargs: &mut BTreeMap<CowStr<'a>, ESExpr<'a>>,
64 constructor_name: &str,
65 ) -> Result<Self, DecodeError> {
66 F::decode_dict_element(kwargs, constructor_name).map(Box::new)
67 }
68}