Relocate cfg attrs into seq_impl and map_impl

This commit is contained in:
David Tolnay 2023-11-06 08:39:31 -08:00
parent 215c2b71ef
commit 3f339de36a
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 76 additions and 38 deletions

View File

@ -878,9 +878,9 @@ impl<'de, T: ?Sized> Deserialize<'de> for PhantomData<T> {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#[cfg(any(feature = "std", feature = "alloc"))]
macro_rules! seq_impl { macro_rules! seq_impl {
( (
$(#[$attr:meta])*
$ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>, $ty:ident <T $(: $tbound1:ident $(+ $tbound2:ident)*)* $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
$access:ident, $access:ident,
$clear:expr, $clear:expr,
@ -888,6 +888,7 @@ macro_rules! seq_impl {
$reserve:expr, $reserve:expr,
$insert:expr $insert:expr
) => { ) => {
$(#[$attr])*
impl<'de, T $(, $typaram)*> Deserialize<'de> for $ty<T $(, $typaram)*> impl<'de, T $(, $typaram)*> Deserialize<'de> for $ty<T $(, $typaram)*>
where where
T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*, T: Deserialize<'de> $(+ $tbound1 $(+ $tbound2)*)*,
@ -975,8 +976,8 @@ macro_rules! seq_impl {
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
fn nop_reserve<T>(_seq: T, _n: usize) {} fn nop_reserve<T>(_seq: T, _n: usize) {}
#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!( seq_impl!(
#[cfg(any(feature = "std", feature = "alloc"))]
BinaryHeap<T: Ord>, BinaryHeap<T: Ord>,
seq, seq,
BinaryHeap::clear, BinaryHeap::clear,
@ -985,8 +986,8 @@ seq_impl!(
BinaryHeap::push BinaryHeap::push
); );
#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!( seq_impl!(
#[cfg(any(feature = "std", feature = "alloc"))]
BTreeSet<T: Eq + Ord>, BTreeSet<T: Eq + Ord>,
seq, seq,
BTreeSet::clear, BTreeSet::clear,
@ -995,8 +996,8 @@ seq_impl!(
BTreeSet::insert BTreeSet::insert
); );
#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!( seq_impl!(
#[cfg(any(feature = "std", feature = "alloc"))]
LinkedList<T>, LinkedList<T>,
seq, seq,
LinkedList::clear, LinkedList::clear,
@ -1005,8 +1006,8 @@ seq_impl!(
LinkedList::push_back LinkedList::push_back
); );
#[cfg(feature = "std")]
seq_impl!( seq_impl!(
#[cfg(feature = "std")]
HashSet<T: Eq + Hash, S: BuildHasher + Default>, HashSet<T: Eq + Hash, S: BuildHasher + Default>,
seq, seq,
HashSet::clear, HashSet::clear,
@ -1015,8 +1016,8 @@ seq_impl!(
HashSet::insert HashSet::insert
); );
#[cfg(any(feature = "std", feature = "alloc"))]
seq_impl!( seq_impl!(
#[cfg(any(feature = "std", feature = "alloc"))]
VecDeque<T>, VecDeque<T>,
seq, seq,
VecDeque::clear, VecDeque::clear,
@ -1373,13 +1374,14 @@ tuple_impls! {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#[cfg(any(feature = "std", feature = "alloc"))]
macro_rules! map_impl { macro_rules! map_impl {
( (
$(#[$attr:meta])*
$ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>, $ty:ident <K $(: $kbound1:ident $(+ $kbound2:ident)*)*, V $(, $typaram:ident : $bound1:ident $(+ $bound2:ident)*)*>,
$access:ident, $access:ident,
$with_capacity:expr $with_capacity:expr,
) => { ) => {
$(#[$attr])*
impl<'de, K, V $(, $typaram)*> Deserialize<'de> for $ty<K, V $(, $typaram)*> impl<'de, K, V $(, $typaram)*> Deserialize<'de> for $ty<K, V $(, $typaram)*>
where where
K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*, K: Deserialize<'de> $(+ $kbound1 $(+ $kbound2)*)*,
@ -1428,15 +1430,19 @@ macro_rules! map_impl {
} }
} }
#[cfg(any(feature = "std", feature = "alloc"))] map_impl! {
map_impl!(BTreeMap<K: Ord, V>, map, BTreeMap::new()); #[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>, HashMap<K: Eq + Hash, V, S: BuildHasher + Default>,
map, 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()),
); }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -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 { 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)*> impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
where where
T: Serialize, T: Serialize,
@ -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 { 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)*> impl<T $(, $typaram)*> Serialize for $ty<T $(, $typaram)*>
where where
T: Serialize $(+ $tbound1 $(+ $tbound2)*)*, T: Serialize $(+ $tbound1 $(+ $tbound2)*)*,
@ -216,23 +224,35 @@ macro_rules! seq_impl {
} }
} }
#[cfg(any(feature = "std", feature = "alloc"))] seq_impl! {
seq_impl!(BinaryHeap<T: Ord>); #[cfg(any(feature = "std", feature = "alloc"))]
BinaryHeap<T: Ord>
}
#[cfg(any(feature = "std", feature = "alloc"))] seq_impl! {
seq_impl!(BTreeSet<T: Ord>); #[cfg(any(feature = "std", feature = "alloc"))]
BTreeSet<T: Ord>
}
#[cfg(feature = "std")] seq_impl! {
seq_impl!(HashSet<T: Eq + Hash, H: BuildHasher>); #[cfg(feature = "std")]
HashSet<T: Eq + Hash, H: BuildHasher>
}
#[cfg(any(feature = "std", feature = "alloc"))] seq_impl! {
seq_impl!(LinkedList<T>); #[cfg(any(feature = "std", feature = "alloc"))]
LinkedList<T>
}
#[cfg(any(feature = "std", feature = "alloc"))] seq_impl! {
seq_impl!(Vec<T>); #[cfg(any(feature = "std", feature = "alloc"))]
Vec<T>
}
#[cfg(any(feature = "std", feature = "alloc"))] seq_impl! {
seq_impl!(VecDeque<T>); #[cfg(any(feature = "std", feature = "alloc"))]
VecDeque<T>
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -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 { 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)*> impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
where where
K: Serialize, K: Serialize,
@ -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 { 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)*> impl<K, V $(, $typaram)*> Serialize for $ty<K, V $(, $typaram)*>
where where
K: Serialize $(+ $kbound1 $(+ $kbound2)*)*, K: Serialize $(+ $kbound1 $(+ $kbound2)*)*,
@ -433,11 +461,15 @@ macro_rules! map_impl {
} }
} }
#[cfg(any(feature = "std", feature = "alloc"))] map_impl! {
map_impl!(BTreeMap<K: Ord, V>); #[cfg(any(feature = "std", feature = "alloc"))]
BTreeMap<K: Ord, V>
}
#[cfg(feature = "std")] map_impl! {
map_impl!(HashMap<K: Eq + Hash, V, H: BuildHasher>); #[cfg(feature = "std")]
HashMap<K: Eq + Hash, V, H: BuildHasher>
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////