Consider this code:
use std::io::Write;
struct Writer<W: Write> {
inner: W,
}
impl<W: Write> Writer<W> {
fn new(inner: W) -> Self {
Writer { inner }
}
fn write_something(&mut self) {
self.inner.write(b"something").unwrap();
}
}
fn main() {
let buf = Vec::new();
let writer = Writer::new(buf);
takes_a_writer(writer);
}
fn takes_a_writer(mut writer: Writer<Vec<u8>>) {
writer.write_something();
}
My question is about function takes_a_writer()
. To make this function take a Writer
as an argument, I have to specify the actual inner
type of that Writer
(Vec<u8>
), even though it would make no difference. (Or would it?)
In this example it's easy to just add a type parameter to takes_a_writer()
, but if Writer
contains many such inner types, that means every function that takes such a struct has to have all those type parameters as well. Is there a better way?