parity_wasm/builder/
data.rs

1use super::invoke::{Identity, Invoke};
2use crate::elements;
3use alloc::vec::Vec;
4
5/// Data segment builder
6pub struct DataSegmentBuilder<F = Identity> {
7	callback: F,
8	// todo: add mapper once multiple memory refs possible
9	mem_index: u32,
10	offset: elements::InitExpr,
11	value: Vec<u8>,
12}
13
14impl DataSegmentBuilder {
15	/// New data segment builder
16	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	/// New data segment builder inside the chain context
29	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	/// Set offset initialization instruction. `End` instruction will be added automatically.
39	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	/// Set the bytes value of the segment
45	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	/// Finish current builder, spawning resulting struct
56	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}