Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wyatt-herkamp/enum_helper
https://github.com/wyatt-herkamp/enum_helper
Last synced: 22 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/wyatt-herkamp/enum_helper
- Owner: wyatt-herkamp
- Created: 2023-08-08T02:16:00.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2023-10-10T13:51:08.000Z (about 1 year ago)
- Last Synced: 2024-04-14T03:08:00.572Z (7 months ago)
- Language: Rust
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Enum Helpers
### Enum Of Keys
Generates a Second Enum of just the variant names of the first Enum
```rust
#[derive(EnumOfKeys)]
#[enum_of_keys(TestEnumKeys)]
#[enum_attr(derive(Debug, Clone, PartialEq, Eq))]
#[non_exhaustive]
pub enum TestEnum {
A(String),
B,
C{
a: String,
b: u32,
},
#[enum_of_keys(default)]
Default(String),
}
```
Will generate
```rust
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum TestEnumKeys {
A,
B,
C,
Default(String),
}
impl enum_helper::KeyEnum for TestEnumKeys {}
impl enum_helper::HasKeyEnum for TestEnum {
type KeyEnum<'a> = TestEnumKeys where Self: 'static;
fn get_key(&self) -> Self::KeyEnum<'static> {
match self {
TestEnum::A(..) => TestEnumKeys::A,
TestEnum::B => TestEnumKeys::B,
TestEnum::C { .. } => TestEnumKeys::C,
TestEnum::Default(value) => TestEnumKeys::Default(value.clone())
}
}
fn get_key_borrowed(&self) -> Self::KeyEnum<'static> {
match self {
TestEnum::A(..) => TestEnumKeys::A,
TestEnum::B => TestEnumKeys::B,
TestEnum::C { .. } => TestEnumKeys::C,
TestEnum::Default(value) => TestEnumKeys::Default(value.clone())
}
}
}
impl ::core::cmp::PartialEq for TestEnum {
fn eq(&self, other: &TestEnumKeys) -> bool {
match (self, other) {
(TestEnum::A(..), TestEnumKeys::A) => true,
(TestEnum::B, TestEnumKeys::B) => true,
(TestEnum::C { .. }, TestEnumKeys::C) => true,
(TestEnum::Default(a), TestEnumKeys::Default(b)) => a == b,
_ => false
}
}
}
impl ::core::cmp::PartialEq for TestEnumKeys {
fn eq(&self, other: &TestEnum) -> bool {
match (other, self) {
(TestEnum::A(..), TestEnumKeys::A) => true,
(TestEnum::B, TestEnumKeys::B) => true,
(TestEnum::C { .. }, TestEnumKeys::C) => true,
(TestEnum::Default(a), TestEnumKeys::Default(b)) => a == b,
_ => false
}
}
}
```