Skip to content

Commit 3f339de

Browse files
committed
Relocate cfg attrs into seq_impl and map_impl
1 parent 215c2b7 commit 3f339de

File tree

2 files changed

+76
-38
lines changed

2 files changed

+76
-38
lines changed

serde/src/de/impls.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -878,16 +878,17 @@ impl<'de, T: ?Sized> Deserialize<'de> for PhantomData<T> {
878878

879879
////////////////////////////////////////////////////////////////////////////////
880880

881-
#[cfg(any(feature = "std", feature = "alloc"))]
882881
macro_rules! seq_impl {
883882
(
883+
$(#[$attr:meta])*
884884
$ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
885885
$access:ident,
886886
$clear:expr,
887887
$with_capacity:expr,
888888
$reserve:expr,
889889
$insert:expr
890890
) => {
891+
$(#[$attr])*
891892
impl<'de, T $(, $typaram)*> Deserialize<'de> for $ty<T $(, $typaram)*>
892893
where
893894
T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*,
@@ -975,8 +976,8 @@ macro_rules! seq_impl {
975976
#[cfg(any(feature = "std", feature = "alloc"))]
976977
fn nop_reserve<T>(_seq: T, _n: usize) {}
977978

978-
#[cfg(any(feature = "std", feature = "alloc"))]
979979
seq_impl!(
980+
#[cfg(any(feature = "std", feature = "alloc"))]
980981
BinaryHeap<T: Ord>,
981982
seq,
982983
BinaryHeap::clear,
@@ -985,8 +986,8 @@ seq_impl!(
985986
BinaryHeap::push
986987
);
987988

988-
#[cfg(any(feature = "std", feature = "alloc"))]
989989
seq_impl!(
990+
#[cfg(any(feature = "std", feature = "alloc"))]
990991
BTreeSet<T: Eq + Ord>,
991992
seq,
992993
BTreeSet::clear,
@@ -995,8 +996,8 @@ seq_impl!(
995996
BTreeSet::insert
996997
);
997998

998-
#[cfg(any(feature = "std", feature = "alloc"))]
999999
seq_impl!(
1000+
#[cfg(any(feature = "std", feature = "alloc"))]
10001001
LinkedList<T>,
10011002
seq,
10021003
LinkedList::clear,
@@ -1005,8 +1006,8 @@ seq_impl!(
10051006
LinkedList::push_back
10061007
);
10071008

1008-
#[cfg(feature = "std")]
10091009
seq_impl!(
1010+
#[cfg(feature = "std")]
10101011
HashSet<T: Eq + Hash, S: BuildHasher + Default>,
10111012
seq,
10121013
HashSet::clear,
@@ -1015,8 +1016,8 @@ seq_impl!(
10151016
HashSet::insert
10161017
);
10171018

1018-
#[cfg(any(feature = "std", feature = "alloc"))]
10191019
seq_impl!(
1020+
#[cfg(any(feature = "std", feature = "alloc"))]
10201021
VecDeque<T>,
10211022
seq,
10221023
VecDeque::clear,
@@ -1373,13 +1374,14 @@ tuple_impls! {
13731374

13741375
////////////////////////////////////////////////////////////////////////////////
13751376

1376-
#[cfg(any(feature = "std", feature = "alloc"))]
13771377
macro_rules! map_impl {
13781378
(
1379+
$(#[$attr:meta])*
13791380
$ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
13801381
$access:ident,
1381-
$with_capacity:expr
1382+
$with_capacity:expr,
13821383
) => {
1384+
$(#[$attr])*
13831385
impl<'de, K, V $(, $typaram)*> Deserialize<'de> for $ty<K, V $(, $typaram)*>
13841386
where
13851387
K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*,
@@ -1428,15 +1430,19 @@ macro_rules! map_impl {
14281430
}
14291431
}
14301432

1431-
#[cfg(any(feature = "std", feature = "alloc"))]
1432-
map_impl!(BTreeMap<K: Ord, V>, map, BTreeMap::new());
1433+
map_impl! {
1434+
#[cfg(any(feature = "std", feature = "alloc"))]
1435+
BTreeMap<K: Ord, V>,
1436+
map,
1437+
BTreeMap::new(),
1438+
}
14331439

1434-
#[cfg(feature = "std")]
1435-
map_impl!(
1440+
map_impl! {
1441+
#[cfg(feature = "std")]
14361442
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
14371443
map,
1438-
HashMap::with_capacity_and_hasher(size_hint::cautious::<(K, V)>(map.size_hint()), S::default())
1439-
);
1444+
HashMap::with_capacity_and_hasher(size_hint::cautious::<(K, V)>(map.size_hint()), S::default()),
1445+
}
14401446

14411447
////////////////////////////////////////////////////////////////////////////////
14421448

serde/src/ser/impls.rs

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,13 @@ where
179179
}
180180
}
181181

182-
#[cfg(all(any(feature = "std", feature = "alloc"), not(no_relaxed_trait_bounds)))]
182+
#[cfg(not(no_relaxed_trait_bounds))]
183183
macro_rules! seq_impl {
184-
($ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>) => {
184+
(
185+
$(#[$attr:meta])*
186+
$ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>
187+
) => {
188+
$(#[$attr])*
185189
impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
186190
where
187191
T: Serialize,
@@ -197,9 +201,13 @@ macro_rules! seq_impl {
197201
}
198202
}
199203

200-
#[cfg(all(any(feature = "std", feature = "alloc"), no_relaxed_trait_bounds))]
204+
#[cfg(no_relaxed_trait_bounds)]
201205
macro_rules! seq_impl {
202-
($ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>) => {
206+
(
207+
$(#[$attr:meta])*
208+
$ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>
209+
) => {
210+
$(#[$attr])*
203211
impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
204212
where
205213
T: Serialize $(+ $tbound1 $(+ $tbound2)*)*,
@@ -216,23 +224,35 @@ macro_rules! seq_impl {
216224
}
217225
}
218226

219-
#[cfg(any(feature = "std", feature = "alloc"))]
220-
seq_impl!(BinaryHeap<T: Ord>);
227+
seq_impl! {
228+
#[cfg(any(feature = "std", feature = "alloc"))]
229+
BinaryHeap<T: Ord>
230+
}
221231

222-
#[cfg(any(feature = "std", feature = "alloc"))]
223-
seq_impl!(BTreeSet<T: Ord>);
232+
seq_impl! {
233+
#[cfg(any(feature = "std", feature = "alloc"))]
234+
BTreeSet<T: Ord>
235+
}
224236

225-
#[cfg(feature = "std")]
226-
seq_impl!(HashSet<T: Eq + Hash, H: BuildHasher>);
237+
seq_impl! {
238+
#[cfg(feature = "std")]
239+
HashSet<T: Eq + Hash, H: BuildHasher>
240+
}
227241

228-
#[cfg(any(feature = "std", feature = "alloc"))]
229-
seq_impl!(LinkedList<T>);
242+
seq_impl! {
243+
#[cfg(any(feature = "std", feature = "alloc"))]
244+
LinkedList<T>
245+
}
230246

231-
#[cfg(any(feature = "std", feature = "alloc"))]
232-
seq_impl!(Vec<T>);
247+
seq_impl! {
248+
#[cfg(any(feature = "std", feature = "alloc"))]
249+
Vec<T>
250+
}
233251

234-
#[cfg(any(feature = "std", feature = "alloc"))]
235-
seq_impl!(VecDeque<T>);
252+
seq_impl! {
253+
#[cfg(any(feature = "std", feature = "alloc"))]
254+
VecDeque<T>
255+
}
236256

237257
////////////////////////////////////////////////////////////////////////////////
238258

@@ -394,9 +414,13 @@ tuple_impls! {
394414

395415
////////////////////////////////////////////////////////////////////////////////
396416

397-
#[cfg(all(any(feature = "std", feature = "alloc"), not(no_relaxed_trait_bounds)))]
417+
#[cfg(not(no_relaxed_trait_bounds))]
398418
macro_rules! map_impl {
399-
($ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>) => {
419+
(
420+
$(#[$attr:meta])*
421+
$ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>
422+
) => {
423+
$(#[$attr])*
400424
impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
401425
where
402426
K: Serialize,
@@ -413,9 +437,13 @@ macro_rules! map_impl {
413437
}
414438
}
415439

416-
#[cfg(all(any(feature = "std", feature = "alloc"), no_relaxed_trait_bounds))]
440+
#[cfg(no_relaxed_trait_bounds)]
417441
macro_rules! map_impl {
418-
($ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>) => {
442+
(
443+
$(#[$attr:meta])*
444+
$ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>
445+
) => {
446+
$(#[$attr])*
419447
impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
420448
where
421449
K: Serialize $(+ $kbound1 $(+ $kbound2)*)*,
@@ -433,11 +461,15 @@ macro_rules! map_impl {
433461
}
434462
}
435463

436-
#[cfg(any(feature = "std", feature = "alloc"))]
437-
map_impl!(BTreeMap<K: Ord, V>);
464+
map_impl! {
465+
#[cfg(any(feature = "std", feature = "alloc"))]
466+
BTreeMap<K: Ord, V>
467+
}
438468

439-
#[cfg(feature = "std")]
440-
map_impl!(HashMap<K: Eq + Hash, V, H: BuildHasher>);
469+
map_impl! {
470+
#[cfg(feature = "std")]
471+
HashMap<K: Eq + Hash, V, H: BuildHasher>
472+
}
441473

442474
////////////////////////////////////////////////////////////////////////////////
443475

0 commit comments

Comments
 (0)