Because constant generics are not totally ready yet I have been using the excellent typenum crate. However I have run into a situation I don't know how to handle. I have a struct that is generic, struct Foo<N: typenum::Unsigned> {...}
. Foo
is like an allocator, where N
represents the size of the fixed sized chunks the allocator will give out. Now I would like to instantiate a Foo that stores chunks that are big enough to store some given T
:
fn create_allocator_for<T>() -> Foo< std::mem::size_of<T>() > {
// ...
}
With const generics I believe this would just work, but I am not sure how to translate it to use typenum
. I think I need a trait on T
that has an associated type that is the typenum type corresponding to the size, so I could do something like:
fn create_allocator_for<T: CanGetTypenumSize>() -> Foo< T::typenum_size > {
// ...
}
But I have no idea how to go about defining such a trait, because it seems like it would require constant generics (and maybe specialization?) itself.
I know I can settle for implementing CanGetTypenumSize
by hand with hardcoding sizes for specific types I care about, and asserting at run time that the sizes are still correct, but that seems like a maintenance nightmare.