Merge pull request #64 from NikVolf/builder-docs

Some builder examples in doc tests
This commit is contained in:
Nikolay Volf 2017-06-28 12:55:45 +03:00 committed by GitHub
commit f5a232caa9
2 changed files with 63 additions and 1 deletions

View File

@ -301,6 +301,20 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
} }
/// Import entry builder /// Import entry builder
/// # Examples
/// ```
/// use parity_wasm::builder::module;
///
/// let module = module()
/// .import()
/// .module("env")
/// .field("memory")
/// .external().memory(256, Some(256))
/// .build()
/// .build();
///
/// assert_eq!(module.import_section().expect("import section to exist").entries().len(), 1);
/// ```
pub fn import(self) -> import::ImportBuilder<Self> { pub fn import(self) -> import::ImportBuilder<Self> {
import::ImportBuilder::with_callback(self) import::ImportBuilder::with_callback(self)
} }
@ -318,11 +332,43 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
} }
/// Export entry builder /// Export entry builder
/// # Examples
/// ```
/// use parity_wasm::builder::module;
/// use parity_wasm::elements::Opcode::*;
///
/// let module = module()
/// .global()
/// .value_type().i32()
/// .init_expr(I32Const(0))
/// .build()
/// .export()
/// .field("_zero")
/// .internal().global(0)
/// .build()
/// .build();
///
/// assert_eq!(module.export_section().expect("export section to exist").entries().len(), 1);
/// ```
pub fn export(self) -> export::ExportBuilder<Self> { pub fn export(self) -> export::ExportBuilder<Self> {
export::ExportBuilder::with_callback(self) export::ExportBuilder::with_callback(self)
} }
/// Glboal entry builder /// Glboal entry builder
/// # Examples
/// ```
/// use parity_wasm::builder::module;
/// use parity_wasm::elements::Opcode::*;
///
/// let module = module()
/// .global()
/// .value_type().i32()
/// .init_expr(I32Const(0))
/// .build()
/// .build();
///
/// assert_eq!(module.global_section().expect("global section to exist").entries().len(), 1);
/// ```
pub fn global(self) -> global::GlobalBuilder<Self> { pub fn global(self) -> global::GlobalBuilder<Self> {
global::GlobalBuilder::with_callback(self) global::GlobalBuilder::with_callback(self)
} }
@ -442,6 +488,22 @@ impl<F> Invoke<elements::DataSegment> for ModuleBuilder<F>
} }
/// Start new module builder /// Start new module builder
/// # Examples
///
/// ```
/// use parity_wasm::builder;
///
/// let module = builder::module()
/// .function()
/// .signature().param().i32().build()
/// .body().build()
/// .build()
/// .build();
///
/// assert_eq!(module.type_section().expect("type section to exist").types().len(), 1);
/// assert_eq!(module.function_section().expect("function section to exist").entries().len(), 1);
/// assert_eq!(module.code_section().expect("code section to exist").bodies().len(), 1);
/// ```
pub fn module() -> ModuleBuilder { pub fn module() -> ModuleBuilder {
ModuleBuilder::new() ModuleBuilder::new()
} }