https://github.com/prostomarkeloff/enum-to-types
Macro for generating pseudo-enums for type-level programming.
https://github.com/prostomarkeloff/enum-to-types
enums rust type-level
Last synced: 11 months ago
JSON representation
Macro for generating pseudo-enums for type-level programming.
- Host: GitHub
- URL: https://github.com/prostomarkeloff/enum-to-types
- Owner: prostomarkeloff
- License: mit
- Created: 2020-07-21T16:43:56.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-21T16:48:51.000Z (almost 6 years ago)
- Last Synced: 2025-06-14T03:38:19.743Z (about 1 year ago)
- Topics: enums, rust, type-level
- Language: Rust
- Homepage:
- Size: 2.93 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Enum-to-types
Macro for generating pseudo-enums for type-level programming.
This is somewhat like https://github.com/fmease/tylift but implemented with `macro_rules!` syntax.
## Install
```toml
[dependencies]
enum-to-types = "0.1.0"
```
## Example
```rust
use enum_to_types::enum_to_types;
use std::marker::PhantomData;
enum_to_types!(AccessLevel; User, Admin);
struct DataStorage(i32, PhantomData);
impl DataStorage {
fn new(i: i32) -> Self {
Self(i, PhantomData)
}
}
trait ReadStorage {
fn read(&self) -> i32;
}
impl ReadStorage for DataStorage {
fn read(&self) -> i32 {
self.0
}
}
impl ReadStorage for DataStorage {
fn read(&self) -> i32 {
self.0
}
}
impl ReadStorage for DataStorage {
fn read(&self) -> i32 {
self.0
}
}
impl ReadStorage for DataStorage {
fn read(&self) -> i32 {
panic!("You have no rights to read this");
}
}
fn main() {
let storage = DataStorage::::new(1);
assert_eq!( as ReadStorage>::read(&storage), 1);
let storage = DataStorage::::new(5);
assert_eq!( as ReadStorage>::read(&storage), 5);
// reading storage with `AccessLevel::Admin` by user will cause panic
}
```
This may look very verbose, but it gives a lot of flexibility.
Also, other examples can look less verbose.