Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mrgvsv/discrimenum
Derive `Hash` and `PartialEq` on enums where only the discriminant matters.
https://github.com/mrgvsv/discrimenum
Last synced: 13 days ago
JSON representation
Derive `Hash` and `PartialEq` on enums where only the discriminant matters.
- Host: GitHub
- URL: https://github.com/mrgvsv/discrimenum
- Owner: MrGVSV
- License: other
- Created: 2022-04-06T07:48:02.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-06T08:02:16.000Z (almost 3 years ago)
- Last Synced: 2024-12-13T02:28:17.390Z (about 1 month ago)
- Language: Rust
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# discrimenum
Derive `Hash` and `PartialEq` on enums where only the discriminant matters.
### Why?
There are times where you have an enum where its variants might contain some data. However, you don't necessarily care
what that data is when comparing or hashing, just that the discriminant matches. This can be done with a super simple
implβit's really only five lines of code. This crate simply does it for you as a convenience.### Usage
By default, discrimenum's derives take the name of the respective trait. Which means you only need to add the following
line for things to work:```rust
use discrimenum::{Hash, PartialEq};
```> Keep in mind that this will shadow the existing derives automatically imported in the prelude, preventing non-discrimenum usage for the current scope.
>
> If this is undesired, either...
> * Qualify it: `#[derive(discrimenum::Hash)]`
> * Rename it: `use discrimenum::Hash as DHash;`
> * Scope it:
>
> ```rust
> // ...
> {
> use discrimenum::Hash;
> #[derive(Hash)]
> enum Foo {
> // ...
> }
> }
> // ...
> ```Then apply it to your enum:
```rust
use discrimenum::{Hash, PartialEq};#[derive(Hash, PartialEq, Debug)]
enum MyEnum {
A(usize),
B(usize)
}assert_eq!(MyEnum::A(123), MyEnum::A(321));
assert_ne!(MyEnum::A(123), MyEnum::B(123));
```#### Generics
This also applies to generic enums. In fact, since we only care about the discriminants, none of the generics need to be
bound by `Hash` or `PartialEq`:```rust
use discrimenum::{Hash, PartialEq};#[derive(Hash, PartialEq)]
enum MyGenericEnum {
A(T),
B(T)
}// Notice we can put `T` instead of `T: Hash + PartialEq`
```### Difference from similar crates?
None. Just felt like adding another π