/rust/registry/src/index.crates.io-6f17d22bba15001f/wasm-encoder-0.4.1/src/functions.rs
| Line | Count | Source | 
| 1 |  | use super::*; | 
| 2 |  |  | 
| 3 |  | /// An encoder for the function section. | 
| 4 |  | /// | 
| 5 |  | /// # Example | 
| 6 |  | /// | 
| 7 |  | /// ``` | 
| 8 |  | /// use wasm_encoder::{Module, FunctionSection, ValType}; | 
| 9 |  | /// | 
| 10 |  | /// let mut functions = FunctionSection::new(); | 
| 11 |  | /// let type_index = 0; | 
| 12 |  | /// functions.function(type_index); | 
| 13 |  | /// | 
| 14 |  | /// let mut module = Module::new(); | 
| 15 |  | /// module.section(&functions); | 
| 16 |  | /// | 
| 17 |  | /// // Note: this will generate an invalid module because we didn't generate a | 
| 18 |  | /// // code section containing the function body. See the documentation for | 
| 19 |  | /// // `CodeSection` for details. | 
| 20 |  | /// | 
| 21 |  | /// let wasm_bytes = module.finish(); | 
| 22 |  | /// ``` | 
| 23 |  | #[derive(Clone, Debug)] | 
| 24 |  | pub struct FunctionSection { | 
| 25 |  |     bytes: Vec<u8>, | 
| 26 |  |     num_added: u32, | 
| 27 |  | } | 
| 28 |  |  | 
| 29 |  | impl FunctionSection { | 
| 30 |  |     /// Construct a new function section encoder. | 
| 31 | 16.2k |     pub fn new() -> FunctionSection { | 
| 32 | 16.2k |         FunctionSection { | 
| 33 | 16.2k |             bytes: vec![], | 
| 34 | 16.2k |             num_added: 0, | 
| 35 | 16.2k |         } | 
| 36 | 16.2k |     } <wasm_encoder::functions::FunctionSection>::new| Line | Count | Source |  | 31 | 169 |     pub fn new() -> FunctionSection { |  | 32 | 169 |         FunctionSection { |  | 33 | 169 |             bytes: vec![], |  | 34 | 169 |             num_added: 0, |  | 35 | 169 |         } |  | 36 | 169 |     } | 
Unexecuted instantiation: <wasm_encoder::functions::FunctionSection>::newUnexecuted instantiation: <wasm_encoder::functions::FunctionSection>::new<wasm_encoder::functions::FunctionSection>::new| Line | Count | Source |  | 31 | 11.8k |     pub fn new() -> FunctionSection { |  | 32 | 11.8k |         FunctionSection { |  | 33 | 11.8k |             bytes: vec![], |  | 34 | 11.8k |             num_added: 0, |  | 35 | 11.8k |         } |  | 36 | 11.8k |     } | 
<wasm_encoder::functions::FunctionSection>::new| Line | Count | Source |  | 31 | 4.27k |     pub fn new() -> FunctionSection { |  | 32 | 4.27k |         FunctionSection { |  | 33 | 4.27k |             bytes: vec![], |  | 34 | 4.27k |             num_added: 0, |  | 35 | 4.27k |         } |  | 36 | 4.27k |     } | 
Unexecuted instantiation: <wasm_encoder::functions::FunctionSection>::newUnexecuted instantiation: <wasm_encoder::functions::FunctionSection>::new | 
| 37 |  |  | 
| 38 |  |     /// Define a function that uses the given type. | 
| 39 | 255k |     pub fn function(&mut self, type_index: u32) -> &mut Self { | 
| 40 | 255k |         self.bytes.extend(encoders::u32(type_index)); | 
| 41 | 255k |         self.num_added += 1; | 
| 42 | 255k |         self | 
| 43 | 255k |     } | 
| 44 |  | } | 
| 45 |  |  | 
| 46 |  | impl Section for FunctionSection { | 
| 47 | 16.2k |     fn id(&self) -> u8 { | 
| 48 | 16.2k |         SectionId::Function.into() | 
| 49 | 16.2k |     } <wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::id| Line | Count | Source |  | 47 | 169 |     fn id(&self) -> u8 { |  | 48 | 169 |         SectionId::Function.into() |  | 49 | 169 |     } | 
Unexecuted instantiation: <wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::idUnexecuted instantiation: <wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::id<wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::id| Line | Count | Source |  | 47 | 11.8k |     fn id(&self) -> u8 { |  | 48 | 11.8k |         SectionId::Function.into() |  | 49 | 11.8k |     } | 
<wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::id| Line | Count | Source |  | 47 | 4.27k |     fn id(&self) -> u8 { |  | 48 | 4.27k |         SectionId::Function.into() |  | 49 | 4.27k |     } | 
Unexecuted instantiation: <wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::idUnexecuted instantiation: <wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::id | 
| 50 |  |  | 
| 51 | 16.2k |     fn encode<S>(&self, sink: &mut S) | 
| 52 | 16.2k |     where | 
| 53 | 16.2k |         S: Extend<u8>, | 
| 54 | 16.2k |     { | 
| 55 | 16.2k |         let num_added = encoders::u32(self.num_added); | 
| 56 | 16.2k |         let n = num_added.len(); | 
| 57 | 16.2k |         sink.extend( | 
| 58 | 16.2k |             encoders::u32(u32::try_from(n + self.bytes.len()).unwrap()) | 
| 59 | 16.2k |                 .chain(num_added) | 
| 60 | 16.2k |                 .chain(self.bytes.iter().copied()), | 
| 61 | 16.2k |         ); | 
| 62 | 16.2k |     } <wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>>| Line | Count | Source |  | 51 | 169 |     fn encode<S>(&self, sink: &mut S) |  | 52 | 169 |     where |  | 53 | 169 |         S: Extend<u8>, |  | 54 | 169 |     { |  | 55 | 169 |         let num_added = encoders::u32(self.num_added); |  | 56 | 169 |         let n = num_added.len(); |  | 57 | 169 |         sink.extend( |  | 58 | 169 |             encoders::u32(u32::try_from(n + self.bytes.len()).unwrap()) |  | 59 | 169 |                 .chain(num_added) |  | 60 | 169 |                 .chain(self.bytes.iter().copied()), |  | 61 | 169 |         ); |  | 62 | 169 |     } | 
Unexecuted instantiation: <wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>>Unexecuted instantiation: <wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::encode::<_><wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>>| Line | Count | Source |  | 51 | 11.8k |     fn encode<S>(&self, sink: &mut S) |  | 52 | 11.8k |     where |  | 53 | 11.8k |         S: Extend<u8>, |  | 54 | 11.8k |     { |  | 55 | 11.8k |         let num_added = encoders::u32(self.num_added); |  | 56 | 11.8k |         let n = num_added.len(); |  | 57 | 11.8k |         sink.extend( |  | 58 | 11.8k |             encoders::u32(u32::try_from(n + self.bytes.len()).unwrap()) |  | 59 | 11.8k |                 .chain(num_added) |  | 60 | 11.8k |                 .chain(self.bytes.iter().copied()), |  | 61 | 11.8k |         ); |  | 62 | 11.8k |     } | 
<wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>>| Line | Count | Source |  | 51 | 4.27k |     fn encode<S>(&self, sink: &mut S) |  | 52 | 4.27k |     where |  | 53 | 4.27k |         S: Extend<u8>, |  | 54 | 4.27k |     { |  | 55 | 4.27k |         let num_added = encoders::u32(self.num_added); |  | 56 | 4.27k |         let n = num_added.len(); |  | 57 | 4.27k |         sink.extend( |  | 58 | 4.27k |             encoders::u32(u32::try_from(n + self.bytes.len()).unwrap()) |  | 59 | 4.27k |                 .chain(num_added) |  | 60 | 4.27k |                 .chain(self.bytes.iter().copied()), |  | 61 | 4.27k |         ); |  | 62 | 4.27k |     } | 
Unexecuted instantiation: <wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>>Unexecuted instantiation: <wasm_encoder::functions::FunctionSection as wasm_encoder::Section>::encode::<alloc::vec::Vec<u8>> | 
| 63 |  | } |