A trait parameter type that the struct does not care about

Hi,

So I'm still trying to find my way around Rust's design, and I already like it a lot - thanks everyone for your work, both on the compiler, the library, the crates, and the community!

Right, now for the question. So a trait, let's call it Output, has a <W: Write> parameter. A struct wants to hold an object that implements the Output trait, but the struct itself does not really care at all about the writer itself - it is more or less an internal implementation detail of the Output structure, although, of course, I know it matters to the compiler :slight_smile: But I could not figure out how to put W in the definition of the struct that wants to have <O: Output> as a parameter - the compiler kept saying W was unused, so in the end I had to resort to what feels like a hack with PhantomData.

Here's the code in question:

And sure, I could box the object, but that felt like an even weirder hack - I can't help but feel that there ought to be a way to express this using parameters, but I cannot figure out how.

Thanks in advance for the inevitable insight-inducing cluebat :slight_smile: And keep up the great work!

G'luck,
Peter

For the record, yes, I realize that changing the Output trait to turn the writer type from a parameter into an associated type could work. Are there other ways to do it?

G'luck,
Peter

...and now I feel stupid :slight_smile: Two minutes after posting this question I realized that the Output trait does not need the W parameter at all, so in this particular case the solution was to remove it: The Output trait does not need the Write parameter at all. (70a9497f) · Commits · Peter Pentchev / hd-realtime · GitLab :slight_smile:

Still, if people feel that the question might actually have some relevance in a slightly different situation, I'd be thankful for any additional insight.

G'luck,
Peter

See Rust complains about unused type parameter which is only used to parameterize another · Issue #23246 · rust-lang/rust (github.com).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.