Stream: t-compiler/const-eval

Topic: Const generic specialisations for recursive enums


Zachary Freed (Feb 17 2020 at 00:33, on Zulip):

I was playing around with const generics and I tried making a recursive type that was recursive over a const parameter. This should work, so long as there was a way to specialize for a base case.
As an example, I think this would work for an enum to represent a buddy allocator

pub enum Block<const Level: usize> {
    Allocated,
    Free,
    Split(Block<{ Level - 1 }>, Block<{ Level - 1 }>),
}

pub enum Block<0> {
    Allocated,
    Free,
}

I think this would work as it has a fully known size, and so it shouldn't need a pointer type for the recursion, which is quite useful for embedded stuff.

Zachary Freed (Feb 18 2020 at 03:20, on Zulip):

I think having GADTs would also allow this?

Zachary Freed (Feb 18 2020 at 07:48, on Zulip):

I think having GADTs would also allow this?

Last update: Apr 03 2020 at 18:25UTC