I am trying to create a generic wrapper type to simplify so branching logic however the compiler complains that the Write trait is not implemented for the specific type being used in that function.
Not pictured here is the logic of someFunction requires the inner type to do some instructions specific to itself to finish it's task. Seeing as I can't implement a trait to do so on the third-party struct, I am creating a wrapper so that I can implement a new trait that will serve as a cleanup step at the end of someFunction.
pub struct someWrapper<T>
where T: Write
{
inner: someInnerStuct<T>
}
impl<T: Write> someWrapper<T>
where T: Write
{
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}
}
fn buildStruct<T>(input: T) -> someWrapper<T>
where T: Write
{
someWrapper{
inner: someInnerStuct::new(input)
}
}
fn someFunction<T: 'static>(
func: fn(fs::File) -> T
) -> io::Result<()>
where T: Write
{
...
}
fn main()
{
someFunction(buildStruct);
}
I have no idea how to fix this because as far as I can see that trait bound has been satisfied for any type that implements Write.
Implementing the Write trait explicitly solves the problem but defeats the purpose of making it generic no?