https://github.com/doctorn/trait-eval
We all know Rust's trait system is Turing complete, so tell me, why aren't we exploiting this???
https://github.com/doctorn/trait-eval
compile-time engineering-at-its-best evaluator
Last synced: 11 months ago
JSON representation
We all know Rust's trait system is Turing complete, so tell me, why aren't we exploiting this???
- Host: GitHub
- URL: https://github.com/doctorn/trait-eval
- Owner: doctorn
- License: mit
- Created: 2020-05-23T22:52:58.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-01-31T22:09:34.000Z (about 5 years ago)
- Last Synced: 2025-04-22T03:06:51.446Z (11 months ago)
- Topics: compile-time, engineering-at-its-best, evaluator
- Language: Rust
- Homepage:
- Size: 24.4 KB
- Stars: 367
- Watchers: 7
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-list - trait-eval
README
# `trait_eval`
[](https://crates.io/crates/trait_eval)
[](https://travis-ci.org/doctorn/trait-eval)
[](https://github.com/Aaronepower/tokei)

We all know Rust's trait system is Turing complete, so tell me, why aren't we exploiting this??? Who needs `const-fn` when we've got a crate like this?!
Honestly, I was too preoccupied with the fact that I could to stop to think whether I actually should.
Believe it or not, [I even wrote docs for this](https://docs.rs/trait_eval/).
## Example
Here's an eminently readable example where we play FizzBuzz at compile-time!
```rust
trait FizzBuzzType {
fn show() -> String; // Don't worry about this -- it's just so we can print the result
}
struct Fizz;
impl FizzBuzzType for Fizz {
fn show() -> String {
"Fizz".to_string()
}
}
struct Buzz;
impl FizzBuzzType for Buzz {
fn show() -> String {
"Buzz".to_string()
}
}
struct FizzBuzz;
impl FizzBuzzType for FizzBuzz {
fn show() -> String {
"FizzBuzz".to_string()
}
}
impl FizzBuzzType for T
where
T: Eval,
::Output: Display,
{
fn show() -> String {
format!("{}", T::eval())
}
}
trait FizzBuzzEval: Nat {
type Result: FizzBuzzType;
}
impl FizzBuzzEval for T
where
T: Mod + Mod,
Mod3: Equals,
Mod5: Equals,
ShouldFizz: AndAlso,
(Fizz, T): If,
(Buzz, DidFizz): If,
(FizzBuzz, DidBuzz): If,
{
type Result = DidFizzBuzz;
}
assert_eq!(::Result::show(), "1");
assert_eq!(::Result::show(), "2");
assert_eq!(::Result::show(), "Fizz");
assert_eq!(::Result::show(), "4");
assert_eq!(::Result::show(), "Buzz");
assert_eq!(::Result::show(), "Fizz");
assert_eq!(::Result::show(), "7");
assert_eq!(::Result::show(), "8");
assert_eq!(::Result::show(), "Fizz");
assert_eq!(::Result::show(), "Buzz");
type Fifteen = >::Result;
assert_eq!(::Result::show(), "FizzBuzz"); // !!!
```
## Contributing
Please, for the love of God, don't use this crate. If you must contribute, open a PR.
## Projects using this crate
Some people never listen, huh?
- [fortraith](https://github.com/Ashymad/fortraith) - Forth implemented in the Rust trait system