the same update but for DecoderOpt

This commit is contained in:
mykola2312 2023-09-07 15:43:40 +03:00
parent a6bfa194b9
commit 0c04e466af
4 changed files with 7 additions and 13 deletions

View file

@ -11,7 +11,7 @@ pub trait Decoder: Sized {
pub trait DecoderCtx<DCtx, ECtx>: Sized { pub trait DecoderCtx<DCtx, ECtx>: Sized {
fn decode(raw: &Raw, offset: usize, size: usize, ctx: DCtx) -> Result<Self>; 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; fn get_enc_size(&self) -> usize;
} }

View file

@ -51,8 +51,7 @@ impl DecoderCtx<&mut EntityList, &EntityList> for Entity {
}) })
} }
fn encode(&self, ctx: &EntityList) -> Result<Raw> { fn encode(&self, wd: &mut WriteStream, ctx: &EntityList) -> Result<()> {
let mut wd = WriteStream::new(self.get_enc_size());
match ctx.get_entity_encoding() { match ctx.get_entity_encoding() {
EntityEncoding::File => { EntityEncoding::File => {
wd.write(ctx.get_type_name(self.type_idx))?; 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 { fn get_enc_size(&self) -> usize {

View file

@ -129,8 +129,7 @@ impl DecoderCtx<EntityEncoding, EntityEncoding> for EntityList {
}) })
} }
fn encode(&self, ctx: EntityEncoding) -> Result<Raw> { fn encode(&self, wd: &mut WriteStream, ctx: EntityEncoding) -> Result<()> {
let mut wd = WriteStream::new(self.get_enc_size());
match ctx { match ctx {
EntityEncoding::File => { EntityEncoding::File => {
for ent in self.ents.iter() { 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 { fn get_enc_size(&self) -> usize {

View file

@ -125,16 +125,12 @@ impl WriteStream {
val: &T, val: &T,
ctx: ECtx, ctx: ECtx,
) -> Result<()> { ) -> Result<()> {
let mut raw = val.encode(ctx)?; self.reserve(val.get_enc_size());
self.skip(raw.mem.len()); val.encode(self, ctx)?;
self.buf.get_mut().append(&mut raw.mem);
Ok(()) Ok(())
} }
pub fn write<T: Decoder>(&mut self, val: &T) -> Result<()> { 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()); self.reserve(val.get_enc_size());
val.encode(self)?; val.encode(self)?;
Ok(()) Ok(())