Hi! I'm trying to make a data structure that stores an array of items that implement, for example, std::fmt::Display
after calling .into()
on them. In rust code, I would describe it like so:
struct Smth<T: Into<impl std::fmt::Display>, const N: usize>(pub [T; N]);
However, this is invalid syntax since impl
isn't allowed in such cases. So, the other idea is to write:
struct Smth<T: Into<D>, D: std::fmt::Display, const N: usize>(pub [T; N]);
But this gives the following error:
error[E0392]: parameter `D` is never used
--> src/lib.rs:1:25
|
1 | struct Smth<T: Into<D>, D: std::fmt::Display, const N: usize>(pub [T; N]);
| ^ unused parameter
|
= help: consider removing `D`, referring to it in a field, or using a marker such as `PhantomData`
= help: if you intended `D` to be a const parameter, use `const D: usize` instead
For more information about this error, try `rustc --explain E0392`.
Following the compiler's recommendation, I can make it work by storing std::marker::PhantomData
inside struct:
struct Smth<T: Into<D>, D: std::fmt::Display, const N: usize>(pub [T; N], pub std::marker::PhantomData<D>);
However, this approach requires me to provide PhantomData when constructing the struct. But I wanted to make it sort of DSL, to be used like so:
Smth([
smth_else,
Text("Hello, World!"),
Smth([
Text("etc")
])
])
And recommended approach is going to ruin this aesthetic.
I'll go with dyn std::fmt::Display
anyway, because of other reasons, but I still wonder if there is a more beautiful way to solve this issue.