You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -9,99 +9,150 @@ Enum-centric data structures for Rust.
9
9
10
10
## Usage
11
11
12
-
### EnumMap
12
+
A map of enum variants to values. EnumMap is a fixed-size map, where each variant of the enum
13
+
is mapped to a value. This implementation EnumMap is a a zero-cost abstraction over an array (const-sized), where the index of the array corresponds to the position of the variant in the enum.
13
14
14
-
EnumMap is a special case of a Hash Map, with better **computational complexity** guarantees and overall **performance**. Can differentiante between a missing (`Option::None`)
15
-
and set (`Option::Some`) value.
15
+
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.
16
16
17
-
Abstracts away the need to handle [Option] on insert/remove operations.
18
-
It marginally is faster to initialize than `EnumTable`, because `Default` value needn't be cloned for each field.
17
+
- Indexed by enum variants.
18
+
- IndexMut by enum variants.
19
+
- Debug if the enum is Debug.
20
+
- PartialEq if the value is PartialEq. Same for Eq.
19
21
20
-
Using `get`and `insert` functions.
22
+
Debug and Eq are optional features. They are enabled by default.
21
23
22
24
```rust
23
-
useenum_collections::{EnumMap, Enumerated};
24
-
#[derive(Enumerated)]
25
-
enumLetter {
26
-
A,
27
-
B,
28
-
}
29
-
assert_eq!(Letter::VARIANTS.len(), 2); // VARIANTS provided by this crate
EnumTable is a special case of a Hash Map, with better **computational complexity** guarantees and overall **performance**. Initialized with default values, can NOT differentiate between missing values
56
-
and values actually set.
81
+
Iterate over enum variants
57
82
58
-
Using Index and IndexMut syntactic sugar.
59
83
60
84
```rust
61
-
useenum_collections::{EnumTable, Enumerated};
62
-
#[derive(Enumerated)]
63
-
enumLetter {
64
-
A,
65
-
B,
66
-
}
67
-
assert_eq!(Letter::VARIANTS.len(), 2); // VARIANTS provided by this crate
Portions of functionality are feature-flagged, but enabled by default. This is to allow turning this functionality off when not needed, e.g. `Debug` and `Eq` implementations.
78
-
79
100
See [docs.rs](https://docs.rs/crate/enum-collections/latest/features) for details.
80
101
81
102
## Benchmarks
82
103
83
-
There are single-threaded benchmarks for the `get`, `insert` and `remove` operations in [enum-collections/benches](enum-collections/benches/). Invoke `cargo bench` to run them.
104
+
Invoke `cargo bench` to run benchmarks. While `EnumMap` operates in pico-seconds, `std::collections::HashMap` in > 10 nanoseconds.
84
105
85
-
### EnumMap
86
-
```
87
-
NAME lower bound | est | upper bound
88
-
EnumMap get time: [635.02 ps 635.52 ps 636.06 ps] est ~22x faster
89
-
std::collections::HashMap get time: [13.971 ns 13.986 ns 14.002 ns]
0 commit comments