the same update but for DecoderOpt
This commit is contained in:
parent
a6bfa194b9
commit
0c04e466af
4 changed files with 7 additions and 13 deletions
|
|
@ -11,7 +11,7 @@ pub trait Decoder: Sized {
|
|||
|
||||
pub trait DecoderCtx<DCtx, ECtx>: Sized {
|
||||
fn decode(raw: &Raw, offset: usize, size: usize, ctx: DCtx) -> Result<Self>;
|
||||
fn encode(&self, ctx: ECtx) -> Result<Raw>;
|
||||
fn encode(&self, wd: &mut WriteStream, ctx: ECtx) -> Result<()>;
|
||||
fn get_enc_size(&self) -> usize;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -51,8 +51,7 @@ impl DecoderCtx<&mut EntityList, &EntityList> for Entity {
|
|||
})
|
||||
}
|
||||
|
||||
fn encode(&self, ctx: &EntityList) -> Result<Raw> {
|
||||
let mut wd = WriteStream::new(self.get_enc_size());
|
||||
fn encode(&self, wd: &mut WriteStream, ctx: &EntityList) -> Result<()> {
|
||||
match ctx.get_entity_encoding() {
|
||||
EntityEncoding::File => {
|
||||
wd.write(ctx.get_type_name(self.type_idx))?;
|
||||
|
|
@ -67,7 +66,7 @@ impl DecoderCtx<&mut EntityList, &EntityList> for Entity {
|
|||
}
|
||||
}
|
||||
}
|
||||
Ok(wd.into_raw(0, 0))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_enc_size(&self) -> usize {
|
||||
|
|
|
|||
|
|
@ -129,8 +129,7 @@ impl DecoderCtx<EntityEncoding, EntityEncoding> for EntityList {
|
|||
})
|
||||
}
|
||||
|
||||
fn encode(&self, ctx: EntityEncoding) -> Result<Raw> {
|
||||
let mut wd = WriteStream::new(self.get_enc_size());
|
||||
fn encode(&self, wd: &mut WriteStream, ctx: EntityEncoding) -> Result<()> {
|
||||
match ctx {
|
||||
EntityEncoding::File => {
|
||||
for ent in self.ents.iter() {
|
||||
|
|
@ -156,7 +155,7 @@ impl DecoderCtx<EntityEncoding, EntityEncoding> for EntityList {
|
|||
}
|
||||
}
|
||||
|
||||
Ok(wd.into_raw(0, 0))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_enc_size(&self) -> usize {
|
||||
|
|
|
|||
|
|
@ -125,16 +125,12 @@ impl WriteStream {
|
|||
val: &T,
|
||||
ctx: ECtx,
|
||||
) -> Result<()> {
|
||||
let mut raw = val.encode(ctx)?;
|
||||
self.skip(raw.mem.len());
|
||||
self.buf.get_mut().append(&mut raw.mem);
|
||||
self.reserve(val.get_enc_size());
|
||||
val.encode(self, ctx)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write<T: Decoder>(&mut self, val: &T) -> Result<()> {
|
||||
//let mut raw = val.encode()?;
|
||||
//self.skip(raw.mem.len());
|
||||
//self.buf.get_mut().append(&mut raw.mem);
|
||||
self.reserve(val.get_enc_size());
|
||||
val.encode(self)?;
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue