Skip to content

Commit 195318d

Browse files
authored
Refactor. (#44)
1 parent 73c73b0 commit 195318d

File tree

4 files changed

+66
-64
lines changed

4 files changed

+66
-64
lines changed

src/derive/convert.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@ use crate::coder::{Buffer, Decoder, Encoder, Result, View};
22
use crate::derive::{Decode, Encode};
33
use core::num::NonZeroUsize;
44

5+
#[allow(unused)]
6+
macro_rules! impl_convert {
7+
($want: path, $have: ty) => {
8+
impl Encode for $want {
9+
type Encoder = crate::derive::convert::ConvertIntoEncoder<$have>;
10+
}
11+
impl<'a> Decode<'a> for $want {
12+
type Decoder = crate::derive::convert::ConvertFromDecoder<'a, $have>;
13+
}
14+
};
15+
}
16+
17+
#[allow(unused)]
18+
pub(crate) use impl_convert;
19+
520
// Like [`From`] but we can implement it ourselves.
621
pub(crate) trait ConvertFrom<T>: Sized {
722
fn convert_from(value: T) -> Self;

src/derive/impls.rs

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ use core::marker::PhantomData;
1818
use core::mem::MaybeUninit;
1919
use core::num::*;
2020

21-
#[cfg(feature = "std")]
22-
use core::hash::{BuildHasher, Hash};
23-
#[cfg(feature = "std")]
24-
use std::collections::{HashMap, HashSet};
25-
2621
macro_rules! impl_both {
2722
($t:ty, $encoder:ident, $decoder:ident) => {
2823
impl Encode for $t {
@@ -33,6 +28,7 @@ macro_rules! impl_both {
3328
}
3429
};
3530
}
31+
pub(crate) use impl_both;
3632
impl_both!(bool, BoolEncoder, BoolDecoder);
3733
impl_both!(f32, F32Encoder, F32Decoder);
3834
impl_both!(String, StrEncoder, StrDecoder);
@@ -150,31 +146,13 @@ impl<T: Encode> Encode for BTreeSet<T> {
150146
impl<'a, T: Decode<'a> + Ord> Decode<'a> for BTreeSet<T> {
151147
type Decoder = VecDecoder<'a, T>;
152148
}
153-
#[cfg(feature = "std")]
154-
impl<T: Encode, S> Encode for HashSet<T, S> {
155-
type Encoder = VecEncoder<T>;
156-
}
157-
#[cfg(feature = "std")]
158-
impl<'a, T: Decode<'a> + Eq + Hash, S: BuildHasher + Default> Decode<'a> for HashSet<T, S> {
159-
type Decoder = VecDecoder<'a, T>;
160-
}
161149

162150
impl<K: Encode, V: Encode> Encode for BTreeMap<K, V> {
163151
type Encoder = MapEncoder<K, V>;
164152
}
165153
impl<'a, K: Decode<'a> + Ord, V: Decode<'a>> Decode<'a> for BTreeMap<K, V> {
166154
type Decoder = MapDecoder<'a, K, V>;
167155
}
168-
#[cfg(feature = "std")]
169-
impl<K: Encode, V: Encode, S> Encode for HashMap<K, V, S> {
170-
type Encoder = MapEncoder<K, V>;
171-
}
172-
#[cfg(feature = "std")]
173-
impl<'a, K: Decode<'a> + Eq + Hash, V: Decode<'a>, S: BuildHasher + Default> Decode<'a>
174-
for HashMap<K, V, S>
175-
{
176-
type Decoder = MapDecoder<'a, K, V>;
177-
}
178156

179157
impl<T: Encode, E: Encode> Encode for core::result::Result<T, E> {
180158
type Encoder = ResultEncoder<T, E>;
@@ -184,42 +162,49 @@ impl<'a, T: Decode<'a>, E: Decode<'a>> Decode<'a> for core::result::Result<T, E>
184162
}
185163

186164
#[cfg(feature = "std")]
187-
macro_rules! impl_convert {
188-
($want: path, $have: ty) => {
189-
impl Encode for $want {
190-
type Encoder = super::convert::ConvertIntoEncoder<$have>;
191-
}
192-
impl<'a> Decode<'a> for $want {
193-
type Decoder = super::convert::ConvertFromDecoder<'a, $have>;
194-
}
195-
};
196-
}
165+
mod with_std {
166+
use super::*;
167+
use crate::derive::convert::impl_convert;
168+
use core::hash::{BuildHasher, Hash};
169+
use std::collections::{HashMap, HashSet};
170+
171+
impl<T: Encode, S> Encode for HashSet<T, S> {
172+
type Encoder = VecEncoder<T>;
173+
}
174+
impl<'a, T: Decode<'a> + Eq + Hash, S: BuildHasher + Default> Decode<'a> for HashSet<T, S> {
175+
type Decoder = VecDecoder<'a, T>;
176+
}
177+
impl<K: Encode, V: Encode, S> Encode for HashMap<K, V, S> {
178+
type Encoder = MapEncoder<K, V>;
179+
}
180+
impl<'a, K: Decode<'a> + Eq + Hash, V: Decode<'a>, S: BuildHasher + Default> Decode<'a>
181+
for HashMap<K, V, S>
182+
{
183+
type Decoder = MapDecoder<'a, K, V>;
184+
}
197185

198-
#[cfg(feature = "std")]
199-
macro_rules! impl_ipvx_addr {
200-
($addr: ident, $repr: ident) => {
201-
impl_convert!(std::net::$addr, $repr);
202-
};
203-
}
186+
macro_rules! impl_ipvx_addr {
187+
($addr: ident, $repr: ident) => {
188+
impl_convert!(std::net::$addr, $repr);
189+
};
190+
}
204191

205-
#[cfg(feature = "std")]
206-
impl_ipvx_addr!(Ipv4Addr, u32);
207-
#[cfg(feature = "std")]
208-
impl_ipvx_addr!(Ipv6Addr, u128);
209-
#[cfg(feature = "std")]
210-
impl_convert!(std::net::IpAddr, super::ip_addr::IpAddrConversion);
211-
#[cfg(feature = "std")]
212-
impl_convert!(
213-
std::net::SocketAddrV4,
214-
super::ip_addr::SocketAddrV4Conversion
215-
);
216-
#[cfg(feature = "std")]
217-
impl_convert!(
218-
std::net::SocketAddrV6,
219-
super::ip_addr::SocketAddrV6Conversion
220-
);
221-
#[cfg(feature = "std")]
222-
impl_convert!(std::net::SocketAddr, super::ip_addr::SocketAddrConversion);
192+
impl_ipvx_addr!(Ipv4Addr, u32);
193+
impl_ipvx_addr!(Ipv6Addr, u128);
194+
impl_convert!(std::net::IpAddr, crate::derive::ip_addr::IpAddrConversion);
195+
impl_convert!(
196+
std::net::SocketAddrV4,
197+
crate::derive::ip_addr::SocketAddrV4Conversion
198+
);
199+
impl_convert!(
200+
std::net::SocketAddrV6,
201+
crate::derive::ip_addr::SocketAddrV6Conversion
202+
);
203+
impl_convert!(
204+
std::net::SocketAddr,
205+
crate::derive::ip_addr::SocketAddrConversion
206+
);
207+
}
223208

224209
impl<T> Encode for PhantomData<T> {
225210
type Encoder = EmptyCoder;

src/derive/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use alloc::vec::Vec;
55
use core::num::NonZeroUsize;
66

77
mod array;
8-
mod convert;
8+
pub(crate) mod convert;
99
mod duration;
1010
mod empty;
1111
mod impls;

src/serde/de.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,23 @@ macro_rules! specify {
117117
_ => {
118118
// Either create the correct decoder if unspecified or diverge via panic/error.
119119
#[cold]
120+
#[rustfmt::skip]
120121
fn cold<'de>(decoder: &mut SerdeDecoder<'de>, input: &mut &'de [u8]) -> Result<()> {
121122
let &mut SerdeDecoder::Unspecified { length } = decoder else {
122-
type_changed!()
123-
};
123+
type_changed!()
124+
};
124125
*decoder = SerdeDecoder::$variant(Default::default());
125126
decoder.populate(input, length)
126127
}
127128
cold(&mut *$self.decoder, &mut *$self.input)?;
128129
}
129130
}
131+
#[rustfmt::skip]
130132
let SerdeDecoder::$variant(d) = &mut *$self.decoder else {
131-
// Safety: `cold` gets called when decoder isn't the correct decoder. `cold` either
132-
// errors or sets lazy to the correct decoder.
133-
unsafe { core::hint::unreachable_unchecked() };
134-
};
133+
// Safety: `cold` gets called when decoder isn't the correct decoder. `cold` either
134+
// errors or sets lazy to the correct decoder.
135+
unsafe { core::hint::unreachable_unchecked() };
136+
};
135137
d
136138
}};
137139
}

0 commit comments

Comments
 (0)