parity_wasm/builder/
data.rs1use super::invoke::{Identity, Invoke};
2use crate::elements;
3use alloc::vec::Vec;
4
5pub struct DataSegmentBuilder<F = Identity> {
7 callback: F,
8 mem_index: u32,
10 offset: elements::InitExpr,
11 value: Vec<u8>,
12}
13
14impl DataSegmentBuilder {
15 pub fn new() -> Self {
17 DataSegmentBuilder::with_callback(Identity)
18 }
19}
20
21impl Default for DataSegmentBuilder {
22 fn default() -> Self {
23 Self::new()
24 }
25}
26
27impl<F> DataSegmentBuilder<F> {
28 pub fn with_callback(callback: F) -> Self {
30 DataSegmentBuilder {
31 callback,
32 mem_index: 0,
33 offset: elements::InitExpr::empty(),
34 value: Vec::new(),
35 }
36 }
37
38 pub fn offset(mut self, instruction: elements::Instruction) -> Self {
40 self.offset = elements::InitExpr::new(vec![instruction, elements::Instruction::End]);
41 self
42 }
43
44 pub fn value(mut self, value: Vec<u8>) -> Self {
46 self.value = value;
47 self
48 }
49}
50
51impl<F> DataSegmentBuilder<F>
52where
53 F: Invoke<elements::DataSegment>,
54{
55 pub fn build(self) -> F::Result {
57 self.callback.invoke(elements::DataSegment::new(
58 self.mem_index,
59 Some(self.offset),
60 self.value,
61 ))
62 }
63}