Skip to content

Commit f93dacb

Browse files
committed
doc: library-level documentation
1 parent 9f16434 commit f93dacb

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

enum-collections/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ documentation = "https://docs.rs/enum-collections"
1414

1515
[features]
1616
default = ["debug", "eq"]
17+
# `std::fmt::Debug` implementation for EnumMap. Requires keys to be `Debug` as well.
1718
debug = ["variants"] # Array of all variants must be available at runtime to generate keys in the Debug output
19+
# `std::cmp::Eq` implementation for EnumMap. Requires keys to be `Eq` as well.
1820
eq = []
21+
# Generates a static array of all variants for the EnumMap. This is required for the `Debug` implementation.
1922
variants = ["enum-collections-macros/variants"]
2023

2124
[dependencies]

enum-collections/src/enummap.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ mod macro_test {
101101
/// # Examples
102102
///
103103
/// ```
104-
/// use enum_collections::{EnumMap, Enumerated};
104+
/// use enum_collections::{EnumMap, Enumerated, em_default, em};
105105
///
106106
/// #[derive(Enumerated)]
107107
/// pub enum Letter {
@@ -116,6 +116,22 @@ mod macro_test {
116116
/// enum_map[Letter::A] = 42;
117117
/// assert_eq!(42, enum_map[Letter::A]);
118118
///
119+
/// // Construction using macros
120+
/// // (Key type, Value type, Key=>Value pairs)
121+
/// let enum_map = em!(Letter, i32, A=>42, B=>24); // All values set explicitly
122+
/// assert_eq!(42, enum_map[Letter::A]);
123+
/// assert_eq!(24, enum_map[Letter::B]);
124+
///
125+
/// // (Key type, Value type, optional Key=>Value pairs)
126+
/// let enum_map = em_default!(Letter, i32, A => 42); // Default used for missing values
127+
/// assert_eq!(42, enum_map[Letter::A]);
128+
/// assert_eq!(i32::default(), enum_map[Letter::B]);
129+
///
130+
/// let enum_map = em_default!(Letter, i32,); // All default
131+
/// assert_eq!(i32::default(), enum_map[Letter::A]);
132+
/// assert_eq!(i32::default(), enum_map[Letter::B]);
133+
///
134+
///
119135
/// // Constructor with default values
120136
/// let enum_map_default = EnumMap::<Letter, i32, { Letter::SIZE }>::new_default();
121137
/// assert_eq!(0, enum_map_default[Letter::A]);

enum-collections/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
//!# Enum collections
2+
//!
3+
//! See [EnumMap] for usage details.
4+
//!
5+
//! A map of enum variants to values. EnumMap is a fixed-size map, where each variant of the enum is mapped to a value.
6+
//! This implementation of EnumMap uses **safe Rust** only and is a a zero-cost abstraction over an array (**const-sized**),
7+
//! where the index of the array corresponds to the position of the variant in the enum.
8+
//!
9+
//! Because it is a thin wrapper over an array, it is stack-allocated by default. Simply `std::boxed::Box`ing it will move it to the heap, at the caller's discretion.
10+
//!- Indexed by enum variants.
11+
//!- IndexMut by enum variants.
12+
//!- Debug if the enum is Debug.
13+
//!- PartialEq if the value is PartialEq. Same for Eq.
14+
//!
15+
//!Debug and Eq are optional features. They are enabled by default.
16+
//!
17+
//!
118
mod enumerated;
219
mod enummap;
320

0 commit comments

Comments
 (0)