| 1 | #![no_std] |
| 2 | |
| 3 | //! [`OrderMap`] is a hash table where the iteration order of the key-value |
| 4 | //! pairs is independent of the hash values of the keys. |
| 5 | //! |
| 6 | //! [`OrderSet`] is a corresponding hash set using the same implementation and |
| 7 | //! with similar properties. |
| 8 | //! |
| 9 | //! ### Highlights |
| 10 | //! |
| 11 | //! [`OrderMap`] and [`OrderSet`] are drop-in compatible with the std `HashMap` |
| 12 | //! and `HashSet`, but they also have some features of note: |
| 13 | //! |
| 14 | //! - The ordering semantics (see their documentation for details) |
| 15 | //! - Sorting methods and the [`.pop()`][OrderMap::pop] methods. |
| 16 | //! - The [`Equivalent`] trait, which offers more flexible equality definitions |
| 17 | //! between borrowed and owned versions of keys. |
| 18 | //! - The [`MutableKeys`][map::MutableKeys] trait, which gives opt-in mutable |
| 19 | //! access to map keys, and [`MutableValues`][set::MutableValues] for sets. |
| 20 | //! |
| 21 | //! ### Feature Flags |
| 22 | //! |
| 23 | //! To reduce the amount of compiled code in the crate by default, certain |
| 24 | //! features are gated behind [feature flags]. These allow you to opt in to (or |
| 25 | //! out of) functionality. Below is a list of the features available in this |
| 26 | //! crate. |
| 27 | //! |
| 28 | //! * `std`: Enables features which require the Rust standard library. For more |
| 29 | //! information see the section on [`no_std`]. |
| 30 | //! * `rayon`: Enables parallel iteration and other parallel methods. |
| 31 | //! * `serde`: Adds implementations for [`Serialize`] and [`Deserialize`] |
| 32 | //! to [`OrderMap`] and [`OrderSet`]. Alternative implementations for |
| 33 | //! (de)serializing [`OrderMap`] as an ordered sequence are available in the |
| 34 | //! [`map::serde_seq`] module. |
| 35 | //! * `borsh`: Adds implementations for [`BorshSerialize`] and [`BorshDeserialize`] |
| 36 | //! to [`OrderMap`] and [`OrderSet`]. |
| 37 | //! * `arbitrary`: Adds implementations for the [`arbitrary::Arbitrary`] trait |
| 38 | //! to [`OrderMap`] and [`OrderSet`]. |
| 39 | //! * `quickcheck`: Adds implementations for the [`quickcheck::Arbitrary`] trait |
| 40 | //! to [`OrderMap`] and [`OrderSet`]. |
| 41 | //! |
| 42 | //! _Note: only the `std` feature is enabled by default._ |
| 43 | //! |
| 44 | //! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section |
| 45 | //! [`no_std`]: #no-standard-library-targets |
| 46 | //! [`Serialize`]: `::serde_core::Serialize` |
| 47 | //! [`Deserialize`]: `::serde_core::Deserialize` |
| 48 | //! [`BorshSerialize`]: `::borsh::BorshSerialize` |
| 49 | //! [`BorshDeserialize`]: `::borsh::BorshDeserialize` |
| 50 | //! [`arbitrary::Arbitrary`]: `::arbitrary::Arbitrary` |
| 51 | //! [`quickcheck::Arbitrary`]: `::quickcheck::Arbitrary` |
| 52 | //! |
| 53 | //! ### Alternate Hashers |
| 54 | //! |
| 55 | //! [`OrderMap`] and [`OrderSet`] have a default hasher type |
| 56 | //! [`S = RandomState`][std::hash::RandomState], |
| 57 | //! just like the standard `HashMap` and `HashSet`, which is resistant to |
| 58 | //! HashDoS attacks but not the most performant. Type aliases can make it easier |
| 59 | //! to use alternate hashers: |
| 60 | //! |
| 61 | //! ``` |
| 62 | //! use fnv::FnvBuildHasher; |
| 63 | //! use ordermap::{OrderMap, OrderSet}; |
| 64 | //! |
| 65 | //! type FnvOrderMap<K, V> = OrderMap<K, V, FnvBuildHasher>; |
| 66 | //! type FnvOrderSet<T> = OrderSet<T, FnvBuildHasher>; |
| 67 | //! |
| 68 | //! let std: OrderSet<i32> = (0..100).collect(); |
| 69 | //! let fnv: FnvOrderSet<i32> = (0..100).collect(); |
| 70 | //! assert_eq!(std, fnv); |
| 71 | //! ``` |
| 72 | //! |
| 73 | //! ### Rust Version |
| 74 | //! |
| 75 | //! This version of ordermap requires Rust 1.85 or later. |
| 76 | //! |
| 77 | //! The ordermap 1.x release series will use a carefully considered version |
| 78 | //! upgrade policy, where in a later 1.x version, we will raise the minimum |
| 79 | //! required Rust version. |
| 80 | //! |
| 81 | //! ## No Standard Library Targets |
| 82 | //! |
| 83 | //! This crate supports being built without `std`, requiring `alloc` instead. |
| 84 | //! This is chosen by disabling the default "std" cargo feature, by adding |
| 85 | //! `default-features = false` to your dependency specification. |
| 86 | //! |
| 87 | //! - Creating maps and sets using [`new`][OrderMap::new] and |
| 88 | //! [`with_capacity`][OrderMap::with_capacity] is unavailable without `std`. |
| 89 | //! Use methods [`OrderMap::default`], [`with_hasher`][OrderMap::with_hasher], |
| 90 | //! [`with_capacity_and_hasher`][OrderMap::with_capacity_and_hasher] instead. |
| 91 | //! A no-std compatible hasher will be needed as well, for example |
| 92 | //! from the crate `twox-hash`. |
| 93 | //! - Macros [`ordermap!`] and [`orderset!`] are unavailable without `std`. Use |
| 94 | //! the macros [`ordermap_with_default!`] and [`orderset_with_default!`] instead. |
| 95 | |
| 96 | #![cfg_attr(docsrs, feature(doc_cfg))] |
| 97 | |
| 98 | extern crate alloc; |
| 99 | |
| 100 | #[cfg(feature = "std")] |
| 101 | #[cfg_attr(test, macro_use)] |
| 102 | extern crate std; |
| 103 | |
| 104 | mod arbitrary; |
| 105 | #[macro_use] |
| 106 | mod macros; |
| 107 | #[cfg(feature = "borsh")] |
| 108 | mod borsh; |
| 109 | #[cfg(feature = "serde")] |
| 110 | mod serde; |
| 111 | #[cfg(feature = "sval")] |
| 112 | mod sval; |
| 113 | |
| 114 | pub mod map; |
| 115 | pub mod set; |
| 116 | |
| 117 | pub use crate::map::OrderMap; |
| 118 | pub use crate::set::OrderSet; |
| 119 | pub use indexmap::{Equivalent, GetDisjointMutError, TryReserveError}; |