I have the following problem (it is simplified a bit).
There is a trait which supplies a set of functions which do not use self
:
pub trait ImageFormat {
fn write(data: Vec<[u8; 3]>, path: &str, ...) -> io::Result<()>;
...
}
with several implementations.
There also is a struct which uses functions from that trait:
pub struct Images<T: ImageFormat> {
path: String,
...
}
impl<T> Images<T> where T: ImageFormat {
pub fn setup(path: &str, ...) -> Images<T> {
Images {
path: String::from(path),
...
}
}
fn write(&mut self, data: Vec<[u8; 3]>, ...) -> io::Result<()> {
T::write(data, &self.path[..], ...)
}
...
}
This does not compile, because the struct does not have a field of type T. It works if I do this, but it feels like a hack:
pub struct Images<T: ImageFormat> {
_image_format: Option<T>,
path: String,
...
}
impl<T> Images<T> where T: ImageFormat {
pub fn setup(path: &str, ...) -> Images<T> {
Images {
_image_format: None,
path: String::from(path),
...
}
}
...
}
Is there any idiomatic way of doing this?