1use core::fmt::Display;
2
3use crate::{
4 Error,
5 de::{CellDeserialize, CellDeserializeAs, CellParser, CellParserError},
6 ser::{CellBuilder, CellBuilderError, CellSerialize, CellSerializeAs},
7};
8
9pub use crate::bits::{FromInto, FromIntoRef, TryFromInto, TryFromIntoRef};
10
11impl<T, As> CellSerializeAs<T> for FromInto<As>
12where
13 T: Into<As> + Clone,
14 As: CellSerialize,
15{
16 type Args = As::Args;
17
18 #[inline]
19 fn store_as(
20 source: &T,
21 builder: &mut CellBuilder,
22 args: Self::Args,
23 ) -> Result<(), CellBuilderError> {
24 source.clone().into().store(builder, args)
25 }
26}
27
28impl<'de, T, As> CellDeserializeAs<'de, T> for FromInto<As>
29where
30 As: Into<T> + CellDeserialize<'de>,
31{
32 type Args = As::Args;
33
34 #[inline]
35 fn parse_as(parser: &mut CellParser<'de>, args: Self::Args) -> Result<T, CellParserError<'de>> {
36 As::parse(parser, args).map(Into::into)
37 }
38}
39
40impl<T, As> CellSerializeAs<T> for FromIntoRef<As>
41where
42 for<'a> &'a T: Into<As>,
43 As: CellSerialize,
44{
45 type Args = As::Args;
46
47 #[inline]
48 fn store_as(
49 source: &T,
50 builder: &mut CellBuilder,
51 args: Self::Args,
52 ) -> Result<(), CellBuilderError> {
53 source.into().store(builder, args)
54 }
55}
56
57impl<'de, T, As> CellDeserializeAs<'de, T> for FromIntoRef<As>
58where
59 As: Into<T> + CellDeserialize<'de>,
60{
61 type Args = As::Args;
62
63 #[inline]
64 fn parse_as(parser: &mut CellParser<'de>, args: Self::Args) -> Result<T, CellParserError<'de>> {
65 As::parse(parser, args).map(Into::into)
66 }
67}
68
69impl<T, As> CellSerializeAs<T> for TryFromInto<As>
70where
71 T: TryInto<As> + Clone,
72 <T as TryInto<As>>::Error: Display,
73 As: CellSerialize,
74{
75 type Args = As::Args;
76
77 #[inline]
78 fn store_as(
79 source: &T,
80 builder: &mut CellBuilder,
81 args: Self::Args,
82 ) -> Result<(), CellBuilderError> {
83 source
84 .clone()
85 .try_into()
86 .map_err(Error::custom)?
87 .store(builder, args)
88 }
89}
90
91impl<'de, T, As> CellDeserializeAs<'de, T> for TryFromInto<As>
92where
93 As: TryInto<T> + CellDeserialize<'de>,
94 <As as TryInto<T>>::Error: Display,
95{
96 type Args = As::Args;
97
98 #[inline]
99 fn parse_as(parser: &mut CellParser<'de>, args: Self::Args) -> Result<T, CellParserError<'de>> {
100 As::parse(parser, args)?.try_into().map_err(Error::custom)
101 }
102}
103
104impl<T, As> CellSerializeAs<T> for TryFromIntoRef<As>
105where
106 for<'a> &'a T: TryInto<As> + Clone,
107 for<'a> <&'a T as TryInto<As>>::Error: Display,
108 As: CellSerialize,
109{
110 type Args = As::Args;
111
112 #[inline]
113 fn store_as(
114 source: &T,
115 builder: &mut CellBuilder,
116 args: Self::Args,
117 ) -> Result<(), CellBuilderError> {
118 source
119 .clone()
120 .try_into()
121 .map_err(Error::custom)?
122 .store(builder, args)
123 }
124}
125
126impl<'de, T, As> CellDeserializeAs<'de, T> for TryFromIntoRef<As>
127where
128 As: TryInto<T> + CellDeserialize<'de>,
129 <As as TryInto<T>>::Error: Display,
130{
131 type Args = As::Args;
132
133 #[inline]
134 fn parse_as(parser: &mut CellParser<'de>, args: Self::Args) -> Result<T, CellParserError<'de>> {
135 As::parse(parser, args)?.try_into().map_err(Error::custom)
136 }
137}