Does anyone have any ideas how to create a recursive constant structure, where each next level is a brand new constants?
That is, inside the structure there is a constant array and a pointer to the next structure, but with different constant size / bounds, etc.
For clarity, here is an example of the desired code, but unfortunately it does not work (Rust Playground):
pub trait Zeros {
fn zeros() -> Self;
}
impl<T: Default + Copy, const N: usize> Zeros for [T; N] {
fn zeros() -> Self {
[T::default(); N]
}
}
pub trait Num: Sized {
const VAL: usize;
}
pub struct Const<const R: usize>;
impl<const T: usize> Num for Const<T> {
const VAL: usize = T;
}
pub struct Layer<R: Num, C: Num>
where
[(); R::VAL]: Sized,
[(); C::VAL]: Sized,
{
array: Box<[[f32; R::VAL]; C::VAL]>,
next: Option<Box<Layer<R, C>>>,
}
fn main() {
// This work
let layer: Layer<Const<3>, Const<3>> = Layer {
array: Box::new(Zeros::zeros()),
next: Some(Box::new(Layer::<Const<3>, Const<3>> {
array: Box::new(Zeros::zeros()),
next: None,
})),
};
// But this one doesn't work
//
// let layer: Layer<Const<3>, Const<3>> = Layer {
// array: Box::new(Zeros::zeros()),
// next: Some(Box::new(Layer::<Const<2>, Const<2>> {
// array: Box::new(Zeros::zeros()),
// next: None,
// })),
// };
}