/rust/registry/src/index.crates.io-6f17d22bba15001f/wasm-encoder-0.4.1/src/memories.rs
Line | Count | Source |
1 | | use super::*; |
2 | | |
3 | | /// An encoder for the memory section. |
4 | | /// |
5 | | /// # Example |
6 | | /// |
7 | | /// ``` |
8 | | /// use wasm_encoder::{Module, MemorySection, MemoryType, Limits}; |
9 | | /// |
10 | | /// let mut memories = MemorySection::new(); |
11 | | /// memories.memory(MemoryType { |
12 | | /// limits: Limits { |
13 | | /// min: 1, |
14 | | /// max: None, |
15 | | /// }, |
16 | | /// }); |
17 | | /// |
18 | | /// let mut module = Module::new(); |
19 | | /// module.section(&memories); |
20 | | /// |
21 | | /// let wasm_bytes = module.finish(); |
22 | | /// ``` |
23 | | #[derive(Clone, Debug)] |
24 | | pub struct MemorySection { |
25 | | bytes: Vec<u8>, |
26 | | num_added: u32, |
27 | | } |
28 | | |
29 | | impl MemorySection { |
30 | | /// Create a new memory section encoder. |
31 | 7.07k | pub fn new() -> MemorySection { |
32 | 7.07k | MemorySection { |
33 | 7.07k | bytes: vec![], |
34 | 7.07k | num_added: 0, |
35 | 7.07k | } |
36 | 7.07k | } <wasm_encoder::memories::MemorySection>::new Line | Count | Source | 31 | 174 | pub fn new() -> MemorySection { | 32 | 174 | MemorySection { | 33 | 174 | bytes: vec![], | 34 | 174 | num_added: 0, | 35 | 174 | } | 36 | 174 | } |
Unexecuted instantiation: <wasm_encoder::memories::MemorySection>::new Unexecuted instantiation: <wasm_encoder::memories::MemorySection>::new <wasm_encoder::memories::MemorySection>::new Line | Count | Source | 31 | 4.39k | pub fn new() -> MemorySection { | 32 | 4.39k | MemorySection { | 33 | 4.39k | bytes: vec![], | 34 | 4.39k | num_added: 0, | 35 | 4.39k | } | 36 | 4.39k | } |
<wasm_encoder::memories::MemorySection>::new Line | Count | Source | 31 | 2.50k | pub fn new() -> MemorySection { | 32 | 2.50k | MemorySection { | 33 | 2.50k | bytes: vec![], | 34 | 2.50k | num_added: 0, | 35 | 2.50k | } | 36 | 2.50k | } |
Unexecuted instantiation: <wasm_encoder::memories::MemorySection>::new Unexecuted instantiation: <wasm_encoder::memories::MemorySection>::new |
37 | | |
38 | | /// Define a memory. |
39 | 7.07k | pub fn memory(&mut self, memory_type: MemoryType) -> &mut Self { |
40 | 7.07k | memory_type.encode(&mut self.bytes); |
41 | 7.07k | self.num_added += 1; |
42 | 7.07k | self |
43 | 7.07k | } |
44 | | } |
45 | | |
46 | | impl Section for MemorySection { |
47 | 7.07k | fn id(&self) -> u8 { |
48 | 7.07k | SectionId::Memory.into() |
49 | 7.07k | } <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::id Line | Count | Source | 47 | 174 | fn id(&self) -> u8 { | 48 | 174 | SectionId::Memory.into() | 49 | 174 | } |
Unexecuted instantiation: <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::id Unexecuted instantiation: <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::id <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::id Line | Count | Source | 47 | 4.39k | fn id(&self) -> u8 { | 48 | 4.39k | SectionId::Memory.into() | 49 | 4.39k | } |
<wasm_encoder::memories::MemorySection as wasm_encoder::Section>::id Line | Count | Source | 47 | 2.50k | fn id(&self) -> u8 { | 48 | 2.50k | SectionId::Memory.into() | 49 | 2.50k | } |
Unexecuted instantiation: <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::id Unexecuted instantiation: <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::id |
50 | | |
51 | 7.07k | fn encode<S>(&self, sink: &mut S) |
52 | 7.07k | where |
53 | 7.07k | S: Extend<u8>, |
54 | 7.07k | { |
55 | 7.07k | let num_added = encoders::u32(self.num_added); |
56 | 7.07k | let n = num_added.len(); |
57 | 7.07k | sink.extend( |
58 | 7.07k | encoders::u32(u32::try_from(n + self.bytes.len()).unwrap()) |
59 | 7.07k | .chain(num_added) |
60 | 7.07k | .chain(self.bytes.iter().copied()), |
61 | 7.07k | ); |
62 | 7.07k | } <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>> Line | Count | Source | 51 | 174 | fn encode<S>(&self, sink: &mut S) | 52 | 174 | where | 53 | 174 | S: Extend<u8>, | 54 | 174 | { | 55 | 174 | let num_added = encoders::u32(self.num_added); | 56 | 174 | let n = num_added.len(); | 57 | 174 | sink.extend( | 58 | 174 | encoders::u32(u32::try_from(n + self.bytes.len()).unwrap()) | 59 | 174 | .chain(num_added) | 60 | 174 | .chain(self.bytes.iter().copied()), | 61 | 174 | ); | 62 | 174 | } |
Unexecuted instantiation: <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::encode::<_> <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>> Line | Count | Source | 51 | 4.39k | fn encode<S>(&self, sink: &mut S) | 52 | 4.39k | where | 53 | 4.39k | S: Extend<u8>, | 54 | 4.39k | { | 55 | 4.39k | let num_added = encoders::u32(self.num_added); | 56 | 4.39k | let n = num_added.len(); | 57 | 4.39k | sink.extend( | 58 | 4.39k | encoders::u32(u32::try_from(n + self.bytes.len()).unwrap()) | 59 | 4.39k | .chain(num_added) | 60 | 4.39k | .chain(self.bytes.iter().copied()), | 61 | 4.39k | ); | 62 | 4.39k | } |
<wasm_encoder::memories::MemorySection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>> Line | Count | Source | 51 | 2.50k | fn encode<S>(&self, sink: &mut S) | 52 | 2.50k | where | 53 | 2.50k | S: Extend<u8>, | 54 | 2.50k | { | 55 | 2.50k | let num_added = encoders::u32(self.num_added); | 56 | 2.50k | let n = num_added.len(); | 57 | 2.50k | sink.extend( | 58 | 2.50k | encoders::u32(u32::try_from(n + self.bytes.len()).unwrap()) | 59 | 2.50k | .chain(num_added) | 60 | 2.50k | .chain(self.bytes.iter().copied()), | 61 | 2.50k | ); | 62 | 2.50k | } |
Unexecuted instantiation: <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>> Unexecuted instantiation: <wasm_encoder::memories::MemorySection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>> |
63 | | } |
64 | | |
65 | | /// A memory's type. |
66 | | #[derive(Clone, Copy, Debug)] |
67 | | pub struct MemoryType { |
68 | | /// This memory's limits (in units of pages). |
69 | | pub limits: Limits, |
70 | | } |
71 | | |
72 | | impl MemoryType { |
73 | 7.07k | pub(crate) fn encode(&self, bytes: &mut Vec<u8>) { |
74 | 7.07k | self.limits.encode(bytes); |
75 | 7.07k | } |
76 | | } |