trait_eval

Crates.io Build Status lines of code Minimum rustc 1.43

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.

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; }

asserteq!(::Result::show(), "1"); asserteq!(::Result::show(), "2"); asserteq!(::Result::show(), "Fizz"); asserteq!(::Result::show(), "4"); asserteq!(::Result::show(), "Buzz"); asserteq!(::Result::show(), "Fizz"); asserteq!(::Result::show(), "7"); asserteq!(::Result::show(), "8"); asserteq!(::Result::show(), "Fizz"); asserteq!(::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.