Hi all -- I'm very new to Rust and still trying to wrap my brain around The Rust Way, so if there's a better approach, please let me know.
I have a set of objects that can be written to anything with the Write trait, so I've defined a Writeable trait that requires them to expose a write_bytes() function.
trait Writeable {
fn write_bytes<T: Write>(&self, out: &mut T) -> Result<usize>;
}
For some of these objects--the simpler ones--it's possible to provide a default implementation of write_bytes(), so I've created a subtrait for those simple objects:
trait BatchOneByteOption: Writeable {
const ID: BatchOptionId;
fn id() -> BatchOptionId {
Self::ID
}
fn val(&self) -> u8;
fn write_bytes<T: Write>(&self, out: &mut T) -> Result<usize> {
out.write(&[Self::id() as u8, self.val()])
}
}
but the default implementation in BatchOneByteOption doesn't seem to satisfy the requirement Writeable imposes. This:
struct BatchEndOption {}
impl BatchOneByteOption for BatchEndOption {
const ID: BatchOptionId = BatchOptionId::BatchEnd;
}
yields this error:
the trait bound `BatchEndOption: Writeable` is not satisfied
the trait `Writeable` is not implemented for `BatchEndOption`rustc(E0277)
What am I doing wrong, or is there a better way?