PFaas
November 25, 2024, 5:14pm
1
I would like to be able to use (but not have to name) a const
generic in a generic type, such as:
struct Generic<T>(::core::marker::PhantomData<T>);
trait NameConst<const N: usize> {}
impl<T: NameConst<N>, const N: usize> Generic<T> {}
The compiler error is pretty clear about the issue, but it is disappointing:
"proving the result of expressions other than the parameter are unique is not supported"
And here's a playground link, in case I'm missing something.
2e71828
November 25, 2024, 5:40pm
2
Unless I’m misunderstanding what you’re trying to do, you’d need to write it more like this, which also isn’t supported— likely because the only current way for a type to satisfy the bound would be ~264 explicit impls:
impl<T> Generic<T> where T: for<const N:usize> NameConst<N> {}
PFaas
November 25, 2024, 7:07pm
3
Well, yeah, more or less. To start with I do have a simple variety:
struct L<const N: usize>;
impl<const N: usize> NameConst<N> for L<N> {}
But also, more complex versions:
struct Complex<const N: usize, const A: bool, /* ... */, const F: bool>;
impl<const N: usize, const A: bool, /* ... */, const F: bool>
NameConst<N> for Complex<N, A, B, C, D, E, F>
where
// Lots of bounds to map bools to N
{}
And I don't want to have to do:
impl<const N: usize> Generic<L<N>> {}
impl<const N: usize, const A: bool, /* ... */, const F: bool>
Generic<Complex<N, A, B, C, D, E, F>>
where
// So many bounds
{}
2e71828
November 25, 2024, 10:07pm
4
What about:
trait NameConst {
const N:usize;
}
impl<T:NameConst> Generic for T {
// T::N can be used here…
}
PFaas
November 26, 2024, 10:22am
5
Oh, there's the issue. The const
of the trait doesn't guarantee it's implemented for an equivalently unique type:
struct NoConst;
trait ConstTrait<const N: usize> {}
impl<const N: usize> ConstTrait<N> for NoConst {}
PFaas
November 28, 2024, 9:48pm
6
And that gave a clue to the solution! [playground ]
Just need to name it as an associated type, not a generic:
impl<T: NameType<Assoc = L<N>>, const N: usize> Final for T {}