[generic_const_exprs] Support for basic field access for structs and tuples

The following situation happens often:

  • I have a wrapper type around a multidimensional array.
  • I want to make the type generic over the specific dimensions.
  • I want to pack all dimensions into one parameter, so that large generic trees don't have excessively long impl<> notations.

The obvious solution is:

#![feature(generic_const_exprs)]

#[derive(std::marker::ConstParamTy, PartialEq, Eq)]
pub struct BoardSize {
  pub width: usize,
  pub height: usize,
}

pub struct Board<const SIZE: BoardSize> {
  board: [[i32; SIZE.width]; SIZE.height] // or [i32; (SIZE.width * SIZE.height)],
  // it doesn't particularly matter
}

However, this does not compile, and neither does the (less readable, but presumably more checkable) variant of:

#![feature(generic_const_exprs)]

pub struct Board<const SIZE: (usize, usize)> {
  board: [[i32; SIZE.0]; SIZE.1] // ditto
}

I believe this should be added to generic_const_exprs, as it seems like a fairly simple feature (especially for tuples, which can be trivially reduced to const SIZE_0: usize, const SIZE_1: usize, the advantage here being less typing and less opportunities to accidentally swap the axes).

As I understand it generic_const_exprs is getting replaced with generic_const_args. And generic_const_exprs is not supported by the next trait solver. generic_const_args currently doesn't allow any expressions on const arguments at all though.

2 Likes