1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::{encoders, ComponentSection, ComponentSectionId, Module};

/// An encoder for the module section of WebAssembly components.
///
/// # Example
///
/// ```rust
/// use wasm_encoder::{Module, Component, ModuleSection};
///
/// let mut component = Component::new();
/// component.section(&ModuleSection(Module::new()));
///
/// let bytes = component.finish();
/// ```
#[derive(Clone, Debug, Default)]
pub struct ModuleSection(pub Module);

impl ComponentSection for ModuleSection {
    fn id(&self) -> u8 {
        ComponentSectionId::Module.into()
    }

    fn encode<S>(&self, sink: &mut S)
    where
        S: Extend<u8>,
    {
        sink.extend(
            encoders::u32(u32::try_from(self.0.bytes.len()).unwrap())
                .chain(self.0.bytes.iter().copied()),
        );
    }
}