Skip to content

Commit 8ff3168

Browse files
authored
Merge pull request #256 from rjsberry/feature-flag-docs
Improve feature flag documentation
2 parents 1f308f1 + 8f26bbc commit 8ff3168

File tree

12 files changed

+55
-46
lines changed

12 files changed

+55
-46
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ tag-name = "{{version}}"
5454

5555
[package.metadata.docs.rs]
5656
features = ["arbitrary", "quickcheck", "serde", "rayon"]
57+
rustdoc-args = ["--cfg", "docsrs"]
5758

5859
[workspace]
5960
members = ["test-nostd", "test-serde"]

RELEASES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
- The `hashbrown` dependency has been updated to version 0.13.
3030

31+
- The `serde_seq` module has been moved from the crate root to below the
32+
`map` module.
33+
3134
- 1.9.2
3235

3336
- `IndexMap` and `IndexSet` both implement `arbitrary::Arbitrary<'_>` and

src/arbitrary.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#[cfg(feature = "arbitrary")]
2+
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
23
mod impl_arbitrary {
34
use crate::{IndexMap, IndexSet};
45
use arbitrary::{Arbitrary, Result, Unstructured};
@@ -35,6 +36,7 @@ mod impl_arbitrary {
3536
}
3637

3738
#[cfg(feature = "quickcheck")]
39+
#[cfg_attr(docsrs, doc(cfg(feature = "quickcheck")))]
3840
mod impl_quickcheck {
3941
use crate::{IndexMap, IndexSet};
4042
use alloc::boxed::Box;

src/lib.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! [`IndexSet`]: set/struct.IndexSet.html
1515
//!
1616
//!
17-
//! ### Feature Highlights
17+
//! ### Highlights
1818
//!
1919
//! [`IndexMap`] and [`IndexSet`] are drop-in compatible with the std `HashMap`
2020
//! and `HashSet`, but they also have some features of note:
@@ -26,6 +26,34 @@
2626
//! - The [`MutableKeys`][map::MutableKeys] trait, which gives opt-in mutable
2727
//! access to hash map keys.
2828
//!
29+
//! ### Feature Flags
30+
//!
31+
//! To reduce the amount of compiled code in the crate by default, certain
32+
//! features are gated behind [feature flags]. These allow you to opt in to (or
33+
//! out of) functionality. Below is a list of the features available in this
34+
//! crate.
35+
//!
36+
//! * `std`: Enables features which require the Rust standard library. For more
37+
//! information see the section on [`no_std`].
38+
//! * `rayon`: Enables parallel iteration and other parallel methods.
39+
//! * `serde`: Adds implementations for [`Serialize`] and [`Deserialize`]
40+
//! to [`IndexMap`] and [`IndexSet`]. Alternative implementations for
41+
//! (de)serializing [`IndexMap`] as an ordered sequence are available in the
42+
//! [`map::serde_seq`] module.
43+
//! * `arbitrary`: Adds implementations for the [`arbitrary::Arbitrary`] trait
44+
//! to [`IndexMap`] and [`IndexSet`].
45+
//! * `quickcheck`: Adds implementations for the [`quickcheck::Arbitrary`] trait
46+
//! to [`IndexMap`] and [`IndexSet`].
47+
//!
48+
//! _Note: only the `std` feature is enabled by default._
49+
//!
50+
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
51+
//! [`no_std`]: #no-standard-library-targets
52+
//! [`Serialize`]: `::serde::Serialize`
53+
//! [`Deserialize`]: `::serde::Deserialize`
54+
//! [`arbitrary::Arbitrary`]: `::arbitrary::Arbitrary`
55+
//! [`quickcheck::Arbitrary`]: `::quickcheck::Arbitrary`
56+
//!
2957
//! ### Alternate Hashers
3058
//!
3159
//! [`IndexMap`] and [`IndexSet`] have a default hasher type `S = RandomState`,
@@ -76,6 +104,8 @@
76104
//!
77105
//! [def]: map/struct.IndexMap.html#impl-Default
78106
107+
#![cfg_attr(docsrs, feature(doc_cfg))]
108+
79109
extern crate alloc;
80110

81111
#[cfg(feature = "std")]
@@ -90,9 +120,8 @@ mod macros;
90120
mod equivalent;
91121
mod mutable_keys;
92122
#[cfg(feature = "serde")]
123+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
93124
mod serde;
94-
#[cfg(feature = "serde")]
95-
pub mod serde_seq;
96125
mod util;
97126

98127
pub mod map;
@@ -101,6 +130,7 @@ pub mod set;
101130
// Placed after `map` and `set` so new `rayon` methods on the types
102131
// are documented after the "normal" methods.
103132
#[cfg(feature = "rayon")]
133+
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))]
104134
mod rayon;
105135

106136
#[cfg(feature = "rustc-rayon")]
@@ -245,4 +275,5 @@ impl core::fmt::Display for TryReserveError {
245275
}
246276

247277
#[cfg(feature = "std")]
278+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
248279
impl std::error::Error for TryReserveError {}

src/macros.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#[cfg(feature = "std")]
2+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
23
#[macro_export]
34
/// Create an `IndexMap` from a list of key-value pairs
45
///
@@ -35,6 +36,7 @@ macro_rules! indexmap {
3536
}
3637

3738
#[cfg(feature = "std")]
39+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
3840
#[macro_export]
3941
/// Create an `IndexSet` from a list of values
4042
///

src/map.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ mod core;
55
mod iter;
66
mod slice;
77

8+
#[cfg(feature = "serde")]
9+
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
10+
pub mod serde_seq;
11+
812
#[cfg(test)]
913
mod tests;
1014

@@ -148,6 +152,7 @@ where
148152
}
149153

150154
#[cfg(feature = "std")]
155+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
151156
impl<K, V> IndexMap<K, V> {
152157
/// Create a new map. (Does not allocate.)
153158
#[inline]
@@ -1112,6 +1117,7 @@ where
11121117
}
11131118

11141119
#[cfg(feature = "std")]
1120+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
11151121
impl<K, V, const N: usize> From<[(K, V); N]> for IndexMap<K, V, RandomState>
11161122
where
11171123
K: Hash + Eq,

src/serde_seq.rs renamed to src/map/serde_seq.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
//! # use serde_derive::{Deserialize, Serialize};
1313
//! #[derive(Deserialize, Serialize)]
1414
//! struct Data {
15-
//! #[serde(with = "indexmap::serde_seq")]
15+
//! #[serde(with = "indexmap::map::serde_seq")]
1616
//! map: IndexMap<i32, u64>,
1717
//! // ...
1818
//! }
1919
//! ```
20-
//!
21-
//! Requires crate feature `"serde"`
2220
2321
use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
2422
use serde::ser::{Serialize, Serializer};
@@ -33,10 +31,8 @@ use crate::IndexMap;
3331

3432
/// Serializes a `map::Slice` as an ordered sequence.
3533
///
36-
/// This behaves like [`crate::serde_seq`] for `IndexMap`, serializing a sequence
34+
/// This behaves like [`crate::map::serde_seq`] for `IndexMap`, serializing a sequence
3735
/// of `(key, value)` pairs, rather than as a map that might not preserve order.
38-
///
39-
/// Requires crate feature `"serde"`
4036
impl<K, V> Serialize for MapSlice<K, V>
4137
where
4238
K: Serialize,
@@ -51,8 +47,6 @@ where
5147
}
5248

5349
/// Serializes a `set::Slice` as an ordered sequence.
54-
///
55-
/// Requires crate feature `"serde"`
5650
impl<T> Serialize for SetSlice<T>
5751
where
5852
T: Serialize,
@@ -74,13 +68,11 @@ where
7468
/// # use serde_derive::Serialize;
7569
/// #[derive(Serialize)]
7670
/// struct Data {
77-
/// #[serde(serialize_with = "indexmap::serde_seq::serialize")]
71+
/// #[serde(serialize_with = "indexmap::map::serde_seq::serialize")]
7872
/// map: IndexMap<i32, u64>,
7973
/// // ...
8074
/// }
8175
/// ```
82-
///
83-
/// Requires crate feature `"serde"`
8476
pub fn serialize<K, V, S, T>(map: &IndexMap<K, V, S>, serializer: T) -> Result<T::Ok, T::Error>
8577
where
8678
K: Serialize + Hash + Eq,
@@ -130,13 +122,11 @@ where
130122
/// # use serde_derive::Deserialize;
131123
/// #[derive(Deserialize)]
132124
/// struct Data {
133-
/// #[serde(deserialize_with = "indexmap::serde_seq::deserialize")]
125+
/// #[serde(deserialize_with = "indexmap::map::serde_seq::deserialize")]
134126
/// map: IndexMap<i32, u64>,
135127
/// // ...
136128
/// }
137129
/// ```
138-
///
139-
/// Requires crate feature `"serde"`
140130
pub fn deserialize<'de, D, K, V, S>(deserializer: D) -> Result<IndexMap<K, V, S>, D::Error>
141131
where
142132
D: Deserializer<'de>,

src/rayon/map.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//!
33
//! You will rarely need to interact with this module directly unless you need to name one of the
44
//! iterator types.
5-
//!
6-
//! Requires crate feature `"rayon"`
75
86
use super::collect;
97
use rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer};
@@ -21,7 +19,6 @@ use crate::Bucket;
2119
use crate::Entries;
2220
use crate::IndexMap;
2321

24-
/// Requires crate feature `"rayon"`.
2522
impl<K, V, S> IntoParallelIterator for IndexMap<K, V, S>
2623
where
2724
K: Send,
@@ -37,7 +34,6 @@ where
3734
}
3835
}
3936

40-
/// Requires crate feature `"rayon"`.
4137
impl<K, V> IntoParallelIterator for Box<Slice<K, V>>
4238
where
4339
K: Send,
@@ -81,7 +77,6 @@ impl<K: Send, V: Send> IndexedParallelIterator for IntoParIter<K, V> {
8177
indexed_parallel_iterator_methods!(Bucket::key_value);
8278
}
8379

84-
/// Requires crate feature `"rayon"`.
8580
impl<'a, K, V, S> IntoParallelIterator for &'a IndexMap<K, V, S>
8681
where
8782
K: Sync,
@@ -97,7 +92,6 @@ where
9792
}
9893
}
9994

100-
/// Requires crate feature `"rayon"`.
10195
impl<'a, K, V> IntoParallelIterator for &'a Slice<K, V>
10296
where
10397
K: Sync,
@@ -147,7 +141,6 @@ impl<K: Sync, V: Sync> IndexedParallelIterator for ParIter<'_, K, V> {
147141
indexed_parallel_iterator_methods!(Bucket::refs);
148142
}
149143

150-
/// Requires crate feature `"rayon"`.
151144
impl<'a, K, V, S> IntoParallelIterator for &'a mut IndexMap<K, V, S>
152145
where
153146
K: Sync + Send,
@@ -163,7 +156,6 @@ where
163156
}
164157
}
165158

166-
/// Requires crate feature `"rayon"`.
167159
impl<'a, K, V> IntoParallelIterator for &'a mut Slice<K, V>
168160
where
169161
K: Sync + Send,
@@ -207,7 +199,6 @@ impl<K: Sync + Send, V: Send> IndexedParallelIterator for ParIterMut<'_, K, V> {
207199
indexed_parallel_iterator_methods!(Bucket::ref_mut);
208200
}
209201

210-
/// Requires crate feature `"rayon"`.
211202
impl<'a, K, V, S> ParallelDrainRange<usize> for &'a mut IndexMap<K, V, S>
212203
where
213204
K: Send,
@@ -395,7 +386,6 @@ impl<K: Sync, V: Sync> IndexedParallelIterator for ParValues<'_, K, V> {
395386
indexed_parallel_iterator_methods!(Bucket::value_ref);
396387
}
397388

398-
/// Requires crate feature `"rayon"`.
399389
impl<K, V, S> IndexMap<K, V, S>
400390
where
401391
K: Send,
@@ -412,7 +402,6 @@ where
412402
}
413403
}
414404

415-
/// Requires crate feature `"rayon"`.
416405
impl<K, V> Slice<K, V>
417406
where
418407
K: Send,
@@ -546,7 +535,6 @@ impl<K: Send, V: Send> IndexedParallelIterator for ParValuesMut<'_, K, V> {
546535
indexed_parallel_iterator_methods!(Bucket::value_mut);
547536
}
548537

549-
/// Requires crate feature `"rayon"`.
550538
impl<K, V, S> FromParallelIterator<(K, V)> for IndexMap<K, V, S>
551539
where
552540
K: Eq + Hash + Send,
@@ -567,7 +555,6 @@ where
567555
}
568556
}
569557

570-
/// Requires crate feature `"rayon"`.
571558
impl<K, V, S> ParallelExtend<(K, V)> for IndexMap<K, V, S>
572559
where
573560
K: Eq + Hash + Send,
@@ -584,7 +571,6 @@ where
584571
}
585572
}
586573

587-
/// Requires crate feature `"rayon"`.
588574
impl<'a, K: 'a, V: 'a, S> ParallelExtend<(&'a K, &'a V)> for IndexMap<K, V, S>
589575
where
590576
K: Copy + Eq + Hash + Send + Sync,

src/rayon/set.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//!
33
//! You will rarely need to interact with this module directly unless you need to name one of the
44
//! iterator types.
5-
//!
6-
//! Requires crate feature `"rayon"`.
75
86
use super::collect;
97
use rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer};
@@ -22,7 +20,6 @@ use crate::IndexSet;
2220

2321
type Bucket<T> = crate::Bucket<T, ()>;
2422

25-
/// Requires crate feature `"rayon"`.
2623
impl<T, S> IntoParallelIterator for IndexSet<T, S>
2724
where
2825
T: Send,
@@ -37,7 +34,6 @@ where
3734
}
3835
}
3936

40-
/// Requires crate feature `"rayon"`.
4137
impl<T> IntoParallelIterator for Box<Slice<T>>
4238
where
4339
T: Send,
@@ -80,7 +76,6 @@ impl<T: Send> IndexedParallelIterator for IntoParIter<T> {
8076
indexed_parallel_iterator_methods!(Bucket::key);
8177
}
8278

83-
/// Requires crate feature `"rayon"`.
8479
impl<'a, T, S> IntoParallelIterator for &'a IndexSet<T, S>
8580
where
8681
T: Sync,
@@ -95,7 +90,6 @@ where
9590
}
9691
}
9792

98-
/// Requires crate feature `"rayon"`.
9993
impl<'a, T> IntoParallelIterator for &'a Slice<T>
10094
where
10195
T: Sync,
@@ -144,7 +138,6 @@ impl<T: Sync> IndexedParallelIterator for ParIter<'_, T> {
144138
indexed_parallel_iterator_methods!(Bucket::key_ref);
145139
}
146140

147-
/// Requires crate feature `"rayon"`.
148141
impl<'a, T, S> ParallelDrainRange<usize> for &'a mut IndexSet<T, S>
149142
where
150143
T: Send,
@@ -585,7 +578,6 @@ where
585578
}
586579
}
587580

588-
/// Requires crate feature `"rayon"`.
589581
impl<T, S> FromParallelIterator<T> for IndexSet<T, S>
590582
where
591583
T: Eq + Hash + Send,
@@ -605,7 +597,6 @@ where
605597
}
606598
}
607599

608-
/// Requires crate feature `"rayon"`.
609600
impl<T, S> ParallelExtend<T> for IndexSet<T, S>
610601
where
611602
T: Eq + Hash + Send,
@@ -621,7 +612,6 @@ where
621612
}
622613
}
623614

624-
/// Requires crate feature `"rayon"`.
625615
impl<'a, T: 'a, S> ParallelExtend<&'a T> for IndexSet<T, S>
626616
where
627617
T: Copy + Eq + Hash + Send + Sync,

0 commit comments

Comments
 (0)