I have a const generic struct Foo
:
struct Foo<const B1: bool, const B2: bool, const B3: bool> {...}
And I want to add additional fields for certain types:
struct Foo<const B1: bool, const B2: bool, const B3: bool> {
pub f1: Vec<Foo<false, B2, B3>>, // only exitst in Foo<true, B2, B3>
pub f2: String, // only exitst in Foo<false, B2, B3>
pub id: usize, // exist in all types (Foo<B1, B2, B3>)
}
Can this be done? Currently, I have to warp f1
, f2
with option (or union if I care the memory usage), which is not very elegant, and it's dangerous if others use f1
and f2
wrong.
P.S. In the example, the solution maybe this:
struct Foo<const B2:i32, const B3: i32> {
pub id: usize
}
struct<const B2:i32, const B3: i32> TrueFoo(Foo<B2, B3>);
struct<const B2:i32, const B3: i32> FalseFoo(Foo<B2, B3>);
But the real case is more complicated, and it's hard to explain why I want to do this.
So is there a solution which I can add fields by using the const generic feature only? Unstable features (or even unfinished RFC) is acceptable.