|
1 | | -use crate::coder::{Buffer, Decoder, Encoder, Result, View}; |
2 | | -use crate::derive::{Decode, Encode}; |
3 | | -use core::num::NonZeroUsize; |
| 1 | +use super::convert_from::ConvertFrom; |
4 | 2 | use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; |
5 | 3 |
|
6 | 4 | macro_rules! ipvx_addr { |
@@ -94,56 +92,3 @@ impl ConvertFrom<SocketAddrConversion> for SocketAddr { |
94 | 92 | } |
95 | 93 | } |
96 | 94 | } |
97 | | - |
98 | | -// Like [`From`] but we can implement it ourselves. |
99 | | -pub(crate) trait ConvertFrom<T>: Sized { |
100 | | - fn convert_from(value: T) -> Self; |
101 | | -} |
102 | | - |
103 | | -pub struct ConvertIntoEncoder<T: Encode>(T::Encoder); |
104 | | - |
105 | | -// Can't derive since it would bound T: Default. |
106 | | -impl<T: Encode> Default for ConvertIntoEncoder<T> { |
107 | | - fn default() -> Self { |
108 | | - Self(Default::default()) |
109 | | - } |
110 | | -} |
111 | | - |
112 | | -impl<D, T: Encode + for<'a> ConvertFrom<&'a D>> Encoder<D> for ConvertIntoEncoder<T> { |
113 | | - #[inline(always)] |
114 | | - fn encode(&mut self, t: &D) { |
115 | | - self.0.encode(&T::convert_from(t)); |
116 | | - } |
117 | | -} |
118 | | - |
119 | | -impl<T: Encode> Buffer for ConvertIntoEncoder<T> { |
120 | | - fn collect_into(&mut self, out: &mut Vec<u8>) { |
121 | | - self.0.collect_into(out); |
122 | | - } |
123 | | - fn reserve(&mut self, additional: NonZeroUsize) { |
124 | | - self.0.reserve(additional); |
125 | | - } |
126 | | -} |
127 | | - |
128 | | -/// Decodes a `T` and then converts it with [`ConvertFrom`]. |
129 | | -pub struct ConvertFromDecoder<'a, T: Decode<'a>>(T::Decoder); |
130 | | - |
131 | | -// Can't derive since it would bound T: Default. |
132 | | -impl<'a, T: Decode<'a>> Default for ConvertFromDecoder<'a, T> { |
133 | | - fn default() -> Self { |
134 | | - Self(Default::default()) |
135 | | - } |
136 | | -} |
137 | | - |
138 | | -impl<'a, T: Decode<'a>> View<'a> for ConvertFromDecoder<'a, T> { |
139 | | - fn populate(&mut self, input: &mut &'a [u8], length: usize) -> Result<()> { |
140 | | - self.0.populate(input, length) |
141 | | - } |
142 | | -} |
143 | | - |
144 | | -impl<'a, F: ConvertFrom<T>, T: Decode<'a>> Decoder<'a, F> for ConvertFromDecoder<'a, T> { |
145 | | - #[inline(always)] |
146 | | - fn decode(&mut self) -> F { |
147 | | - F::convert_from(self.0.decode()) |
148 | | - } |
149 | | -} |
0 commit comments