2 unstable releases

0.2.0 Sep 29, 2024
0.1.0 Sep 29, 2024

#647 in Rust patterns

Download history 152/week @ 2024-09-23 130/week @ 2024-09-30 14/week @ 2024-10-07 12/week @ 2024-10-14

308 downloads per month

Custom license

6KB
104 lines

kinds

Higher-Kinded Types in Rust by GAT

Example

use kinds::kind::*;

enum Nat<'a, K: Kind<'a> + 'a> {
    Zero,
    Next(K::F<Nat<'a, K>>),
}

type BoxedNats = Box<Nat<'static, kinds::std::boxed::Box>>;

fn sum_boxed_nats(n: &BoxedNats) -> (usize, usize) {
    let n = n.as_ref();
    match n {
        Nat::Zero => (0, 0),
        Nat::Next(n) => {
            let (sum, n) = sum_boxed_nats(n);
            (sum + n + 1, n + 1)
        }
    }
}

#[test]
fn test_sum_nats() {
    let n: BoxedNats = Box::new(Nat::Next(Box::new(Nat::Next(Box::new(Nat::Next(
        Box::new(Nat::Zero),
    ))))));
    let sum = sum_boxed_nats(&n).0;
    assert_eq!(sum, 6);
}

No runtime deps