Debian Codegraph @

third_party/rust/wasm-encoder/src/core/start.rs

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