Support for factory in decoder/encoder
There are use cases where objects need to be constructed through a factory rather than directly using new. One example is interned strings or identifiers. This is currently not possible to implement easily with Decoder since decoder is not extensible and rust does not support inheritance.
One potential way to implement this is allowing custom object to be added to Decoder when constructing. This custom object can contain the factory which can then be extracted and used in decode method.
Roughly:
pub struct Decoder<'b, Ctx=()> {
buf: &'b [u8],
pos: usize,
ctx: &'b Ctx
}
impl<'b, Ctx> Decoder<'b, Ctx> {
...
fn new_with_ctx(..., ctx: &'b Ctx) -> Decoder<'b, Ctx> {
...
}
...
fn ctx(&self) -> &Ctx {
self.ctx
}
}
There might be other ways to do this (i am new to rust, so would love to hear if it is already possible).