diff --git a/src/builder/code.rs b/src/builder/code.rs index ac65e61..d7e6776 100644 --- a/src/builder/code.rs +++ b/src/builder/code.rs @@ -167,7 +167,7 @@ impl Invoke for SignaturesBuilder { impl SignaturesBuilder where F: Invoke { pub fn build(self) -> F::Result { - let mut result = elements::FunctionsSection::new(); + let mut result = elements::FunctionsSection::default(); for f in self.section.into_iter() { if let Signature::TypeReference(type_ref) = f { result.entries_mut().push(elements::Func::new(type_ref)); diff --git a/src/elements/section.rs b/src/elements/section.rs index 908acec..b7102cc 100644 --- a/src/elements/section.rs +++ b/src/elements/section.rs @@ -183,6 +183,11 @@ impl Serialize for Section { pub struct TypeSection(Vec); impl TypeSection { + /// New type section with provided types + pub fn with_types(types: Vec) -> Self { + TypeSection(types) + } + /// List of type declarations pub fn types(&self) -> &[Type] { &self.0 @@ -226,6 +231,11 @@ impl Serialize for TypeSection { pub struct ImportSection(Vec); impl ImportSection { + /// New import section with provided types + pub fn with_entries(entries: Vec) -> Self { + ImportSection(entries) + } + /// List of import entries. pub fn entries(&self) -> &[ImportEntry] { &self.0 @@ -269,9 +279,9 @@ impl Serialize for ImportSection { pub struct FunctionsSection(Vec); impl FunctionsSection { - /// New functions section - pub fn new() -> FunctionsSection { - FunctionsSection(Vec::new()) + /// New function signatures section with provided entries + pub fn with_entries(entries: Vec) -> Self { + FunctionsSection(entries) } /// List of all functions in the section, mutable @@ -326,6 +336,11 @@ impl TableSection { &self.0 } + /// New table section with provided table entries + pub fn with_entries(entries: Vec) -> Self { + TableSection(entries) + } + /// Mutable table entries. pub fn entries_mut(&mut self) -> &mut Vec { &mut self.0 @@ -369,6 +384,11 @@ impl MemorySection { &self.0 } + /// New memory section with memory types + pub fn with_entries(entries: Vec) -> Self { + MemorySection(entries) + } + /// Mutable list of all memory entries in the section pub fn entries_mut(&mut self) -> &mut Vec { &mut self.0 @@ -412,6 +432,11 @@ impl GlobalSection { &self.0 } + /// New global section from list of global entries + pub fn with_entries(entries: Vec) -> Self { + GlobalSection(entries) + } + /// List of all global entries in the section (mutable) pub fn entries_mut(&mut self) -> &mut Vec { &mut self.0 @@ -455,6 +480,11 @@ impl ExportSection { &self.0 } + /// New export section from list of export entries + pub fn with_entries(entries: Vec) -> Self { + ExportSection(entries) + } + /// List of all export entries in the section (mutable) pub fn entries_mut(&mut self) -> &mut Vec { &mut self.0 @@ -493,8 +523,8 @@ impl Serialize for ExportSection { pub struct CodeSection(Vec); impl CodeSection { - /// New code section - pub fn new(bodies: Vec) -> Self { + /// New code section with specified function bodies + pub fn with_bodies(bodies: Vec) -> Self { CodeSection(bodies) } @@ -542,7 +572,7 @@ pub struct ElementSection(Vec); impl ElementSection { /// New elements section - pub fn new(entries: Vec) -> Self { + pub fn with_entries(entries: Vec) -> Self { ElementSection(entries) } @@ -590,8 +620,8 @@ pub struct DataSection(Vec); impl DataSection { /// New data section - pub fn new(segments: Vec) -> Self { - DataSection(segments) + pub fn with_entries(entries: Vec) -> Self { + DataSection(entries) } /// List of all data entries in the section @@ -868,7 +898,7 @@ mod tests { #[test] fn data_section_ser() { - let data_section = DataSection::new( + let data_section = DataSection::with_entries( vec![DataSegment::new(0u32, InitExpr::empty(), vec![0u8; 16])] ); @@ -902,7 +932,7 @@ mod tests { #[test] fn element_section_ser() { - let element_section = ElementSection::new( + let element_section = ElementSection::with_entries( vec![ElementSegment::new(0u32, InitExpr::empty(), vec![0u32; 4])] ); @@ -922,7 +952,7 @@ mod tests { fn code_section_ser() { use super::super::Opcode::*; - let code_section = CodeSection::new( + let code_section = CodeSection::with_bodies( vec![ FuncBody::new( vec![Local::new(1, ValueType::I32)],