remaining sections

This commit is contained in:
NikVolf
2017-04-03 22:58:00 +03:00
parent 4dd2229243
commit 4e1f1f2d86
3 changed files with 113 additions and 4 deletions

View File

@ -98,6 +98,29 @@ impl Deserialize for Section {
}
}
impl Serialize for Section {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
match self {
Section::Custom(custom_section) => {
VarUint7::from(0x00).serialize(writer)?;
writer.write_all(&custom_section[..])?;
},
Section::Unparsed { id, payload } => {
VarUint7::from(id).serialize(writer)?;
writer.write_all(&payload[..])?;
},
Section::Type(type_section) => {
VarUint7::from(0x01).serialize(writer)?;
type_section.serialize(writer)?;
},
_ => unreachable!()
}
Ok(())
}
}
pub struct TypeSection(Vec<Type>);
impl TypeSection {
@ -117,6 +140,22 @@ impl Deserialize for TypeSection {
}
}
impl Serialize for TypeSection {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
let mut counted_writer = CountedWriter::new(writer);
let data = self.0;
let counted_list = CountedListWriter::<Type, _>(
data.len(),
data.into_iter().map(Into::into),
);
counted_list.serialize(&mut counted_writer)?;
counted_writer.done()?;
Ok(())
}
}
pub struct ImportSection(Vec<ImportEntry>);
impl ImportSection {
@ -136,6 +175,22 @@ impl Deserialize for ImportSection {
}
}
impl Serialize for ImportSection {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
let mut counted_writer = CountedWriter::new(writer);
let data = self.0;
let counted_list = CountedListWriter::<ImportEntry, _>(
data.len(),
data.into_iter().map(Into::into),
);
counted_list.serialize(&mut counted_writer)?;
counted_writer.done()?;
Ok(())
}
}
pub struct FunctionsSection(Vec<Func>);
impl FunctionsSection {
@ -159,6 +214,22 @@ impl Deserialize for FunctionsSection {
}
}
impl Serialize for FunctionsSection {
type Error = Error;
fn serialize<W: io::Write>(self, writer: &mut W) -> Result<(), Self::Error> {
let mut counted_writer = CountedWriter::new(writer);
let data = self.0;
let counted_list = CountedListWriter::<VarUint32, _>(
data.len(),
data.into_iter().map(|func| func.type_ref().into())
);
counted_list.serialize(&mut counted_writer)?;
counted_writer.done()?;
Ok(())
}
}
pub struct TableSection(Vec<TableType>);
impl TableSection {