https://github.com/aecsocket/const-exhaustive
Enumerate all values of a type at compile time
https://github.com/aecsocket/const-exhaustive
Last synced: about 1 month ago
JSON representation
Enumerate all values of a type at compile time
- Host: GitHub
- URL: https://github.com/aecsocket/const-exhaustive
- Owner: aecsocket
- License: apache-2.0
- Created: 2024-09-25T13:17:10.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-28T22:01:13.000Z (over 1 year ago)
- Last Synced: 2024-12-28T20:04:16.623Z (about 1 year ago)
- Language: Rust
- Size: 61.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# `const-exhaustive`
[](https://crates.io/crates/const-exhaustive)
[](https://docs.rs/const-exhaustive)
Enumerate all values of a type at compile time.
## Features
- **All values of `T: Exhaustive` are stored in a [`GenericArray`]** - allowing you to access all
values at compile time, and in a const context.
- **Composable with `core` types** - supports `[T; N]`, tuples up to arity 16, `Option`, and
other types in `core`.
- **`#[derive(Exhaustive)]`** - to implement it on your own types.
- **`#![no_std]` and no `alloc`** - you can use it anywhere.
[`GenericArray`]: https://docs.rs/generic-array/
## Examples
```rust
use const_exhaustive::Exhaustive;
// there is 1 value of `()`
assert_eq!([()], <()>::ALL.as_slice());
// there are 2 values of `bool`
assert_eq!([false, true], bool::ALL.as_slice());
// works on types with generics
assert_eq!(
[None, Some(false), Some(true)],
Option::::ALL.as_slice()
);
// write your own exhaustive types
#[derive(Debug, Clone, Copy, PartialEq, Exhaustive)]
enum Direction {
North,
South,
East,
West,
}
assert_eq!(
[
Direction::North,
Direction::South,
Direction::East,
Direction::West,
],
Direction::ALL.as_slice()
);
// works on arbitrarily complex types
#[derive(Debug, Clone, Copy, PartialEq, Exhaustive)]
enum Complex {
Direction(Direction),
More {
foo: Option,
bar: (Result),
},
}
```
## Testing
Run unit and doc tests:
```bash
cargo test
```
Run miri tests:
```bash
cargo +nightly miri test
```
Test generating docs:
```bash
RUSTDOCFLAGS="--cfg docsrs_dep" cargo +nightly doc --workspace --all-features
```