Skip to content

Commit

Permalink
Relocate cfg attrs into seq_impl and map_impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 6, 2023
1 parent 215c2b7 commit 3f339de
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 38 deletions.
34 changes: 20 additions & 14 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,16 +878,17 @@ impl<'de, T: ?Sized> Deserialize<'de> for PhantomData<T> {

////////////////////////////////////////////////////////////////////////////////

#[cfg(any(feature = "std", feature = "alloc"))]
macro_rules! seq_impl {
(
$(#[$attr:meta])*
$ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
$access:ident,
$clear:expr,
$with_capacity:expr,
$reserve:expr,
$insert:expr
) => {
$(#[$attr])*
impl<'de, T $(, $typaram)*> Deserialize<'de> for $ty<T $(, $typaram)*>
where
T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*,
Expand Down Expand Up @@ -975,8 +976,8 @@ macro_rules! seq_impl {
#[cfg(any(feature = "std", feature = "alloc"))]
fn nop_reserve<T>(_seq: T, _n: usize) {}

#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(
#[cfg(any(feature = "std", feature = "alloc"))]
BinaryHeap<T: Ord>,
seq,
BinaryHeap::clear,
Expand All @@ -985,8 +986,8 @@ seq_impl!(
BinaryHeap::push
);

#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(
#[cfg(any(feature = "std", feature = "alloc"))]
BTreeSet<T: Eq + Ord>,
seq,
BTreeSet::clear,
Expand All @@ -995,8 +996,8 @@ seq_impl!(
BTreeSet::insert
);

#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(
#[cfg(any(feature = "std", feature = "alloc"))]
LinkedList<T>,
seq,
LinkedList::clear,
Expand All @@ -1005,8 +1006,8 @@ seq_impl!(
LinkedList::push_back
);

#[cfg(feature = "std")]
seq_impl!(
#[cfg(feature = "std")]
HashSet<T: Eq + Hash, S: BuildHasher + Default>,
seq,
HashSet::clear,
Expand All @@ -1015,8 +1016,8 @@ seq_impl!(
HashSet::insert
);

#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(
#[cfg(any(feature = "std", feature = "alloc"))]
VecDeque<T>,
seq,
VecDeque::clear,
Expand Down Expand Up @@ -1373,13 +1374,14 @@ tuple_impls! {

////////////////////////////////////////////////////////////////////////////////

#[cfg(any(feature = "std", feature = "alloc"))]
macro_rules! map_impl {
(
$(#[$attr:meta])*
$ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
$access:ident,
$with_capacity:expr
$with_capacity:expr,
) => {
$(#[$attr])*
impl<'de, K, V $(, $typaram)*> Deserialize<'de> for $ty<K, V $(, $typaram)*>
where
K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*,
Expand Down Expand Up @@ -1428,15 +1430,19 @@ macro_rules! map_impl {
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
map_impl!(BTreeMap<K: Ord, V>, map, BTreeMap::new());
map_impl! {
#[cfg(any(feature = "std", feature = "alloc"))]
BTreeMap<K: Ord, V>,
map,
BTreeMap::new(),
}

#[cfg(feature = "std")]
map_impl!(
map_impl! {
#[cfg(feature = "std")]
HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
map,
HashMap::with_capacity_and_hasher(size_hint::cautious::<(K, V)>(map.size_hint()), S::default())
);
HashMap::with_capacity_and_hasher(size_hint::cautious::<(K, V)>(map.size_hint()), S::default()),
}

////////////////////////////////////////////////////////////////////////////////

Expand Down
80 changes: 56 additions & 24 deletions serde/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,13 @@ where
}
}

#[cfg(all(any(feature = "std", feature = "alloc"), not(no_relaxed_trait_bounds)))]
#[cfg(not(no_relaxed_trait_bounds))]
macro_rules! seq_impl {
($ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>) => {
(
$(#[$attr:meta])*
$ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>
) => {
$(#[$attr])*
impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
where
T: Serialize,
Expand All @@ -197,9 +201,13 @@ macro_rules! seq_impl {
}
}

#[cfg(all(any(feature = "std", feature = "alloc"), no_relaxed_trait_bounds))]
#[cfg(no_relaxed_trait_bounds)]
macro_rules! seq_impl {
($ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>) => {
(
$(#[$attr:meta])*
$ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound:ident)*>
) => {
$(#[$attr])*
impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
where
T: Serialize $(+ $tbound1 $(+ $tbound2)*)*,
Expand All @@ -216,23 +224,35 @@ macro_rules! seq_impl {
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(BinaryHeap<T: Ord>);
seq_impl! {
#[cfg(any(feature = "std", feature = "alloc"))]
BinaryHeap<T: Ord>
}

#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(BTreeSet<T: Ord>);
seq_impl! {
#[cfg(any(feature = "std", feature = "alloc"))]
BTreeSet<T: Ord>
}

#[cfg(feature = "std")]
seq_impl!(HashSet<T: Eq + Hash, H: BuildHasher>);
seq_impl! {
#[cfg(feature = "std")]
HashSet<T: Eq + Hash, H: BuildHasher>
}

#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(LinkedList<T>);
seq_impl! {
#[cfg(any(feature = "std", feature = "alloc"))]
LinkedList<T>
}

#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(Vec<T>);
seq_impl! {
#[cfg(any(feature = "std", feature = "alloc"))]
Vec<T>
}

#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!(VecDeque<T>);
seq_impl! {
#[cfg(any(feature = "std", feature = "alloc"))]
VecDeque<T>
}

////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -394,9 +414,13 @@ tuple_impls! {

////////////////////////////////////////////////////////////////////////////////

#[cfg(all(any(feature = "std", feature = "alloc"), not(no_relaxed_trait_bounds)))]
#[cfg(not(no_relaxed_trait_bounds))]
macro_rules! map_impl {
($ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>) => {
(
$(#[$attr:meta])*
$ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>
) => {
$(#[$attr])*
impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
where
K: Serialize,
Expand All @@ -413,9 +437,13 @@ macro_rules! map_impl {
}
}

#[cfg(all(any(feature = "std", feature = "alloc"), no_relaxed_trait_bounds))]
#[cfg(no_relaxed_trait_bounds)]
macro_rules! map_impl {
($ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>) => {
(
$(#[$attr:meta])*
$ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound:ident)*>
) => {
$(#[$attr])*
impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
where
K: Serialize $(+ $kbound1 $(+ $kbound2)*)*,
Expand All @@ -433,11 +461,15 @@ macro_rules! map_impl {
}
}

#[cfg(any(feature = "std", feature = "alloc"))]
map_impl!(BTreeMap<K: Ord, V>);
map_impl! {
#[cfg(any(feature = "std", feature = "alloc"))]
BTreeMap<K: Ord, V>
}

#[cfg(feature = "std")]
map_impl!(HashMap<K: Eq + Hash, V, H: BuildHasher>);
map_impl! {
#[cfg(feature = "std")]
HashMap<K: Eq + Hash, V, H: BuildHasher>
}

////////////////////////////////////////////////////////////////////////////////

Expand Down

0 comments on commit 3f339de

Please sign in to comment.