wasm_encoder/core/
start.rs

1use crate::{encoding_size, Encode, Section, SectionId};
2use alloc::vec::Vec;
3
4/// An encoder for the start section of WebAssembly modules.
5///
6/// # Example
7///
8/// Note: this doesn't actually define the function at index 0, its type, or its
9/// code body, so the resulting Wasm module will be invalid. See `TypeSection`,
10/// `FunctionSection`, and `CodeSection` for details on how to generate those
11/// things.
12///
13/// ```
14/// use wasm_encoder::{Module, StartSection};
15///
16/// let start = StartSection { function_index: 0 };
17///
18/// let mut module = Module::new();
19/// module.section(&start);
20///
21/// let wasm_bytes = module.finish();
22/// ```
23#[derive(Clone, Copy, Debug)]
24pub struct StartSection {
25    /// The index of the start function.
26    pub function_index: u32,
27}
28
29impl Encode for StartSection {
30    fn encode(&self, sink: &mut Vec<u8>) {
31        encoding_size(self.function_index).encode(sink);
32        self.function_index.encode(sink);
33    }
34}
35
36impl Section for StartSection {
37    fn id(&self) -> u8 {
38        SectionId::Start.into()
39    }
40}