/__w/smoldot/smoldot/repo/lib/src/util.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Smoldot |
2 | | // Copyright (C) 2019-2022 Parity Technologies (UK) Ltd. |
3 | | // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
4 | | |
5 | | // This program is free software: you can redistribute it and/or modify |
6 | | // it under the terms of the GNU General Public License as published by |
7 | | // the Free Software Foundation, either version 3 of the License, or |
8 | | // (at your option) any later version. |
9 | | |
10 | | // This program is distributed in the hope that it will be useful, |
11 | | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | // GNU General Public License for more details. |
14 | | |
15 | | // You should have received a copy of the GNU General Public License |
16 | | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | | |
18 | | //! Internal module. Contains functions that aren't Substrate/Polkadot-specific and should ideally |
19 | | //! be found in third party libraries, but that aren't worth a third-party library. |
20 | | |
21 | | use core::{cmp, iter, marker, str}; |
22 | | |
23 | | pub(crate) mod leb128; |
24 | | pub(crate) mod protobuf; |
25 | | |
26 | | /// Implementation of the `BuildHasher` trait for the sip hasher. |
27 | | /// |
28 | | /// Contrary to the one in the standard library, a seed is explicitly passed here, making the |
29 | | /// hashing predictable. This is a good thing for tests and no-std compatibility. |
30 | | pub struct SipHasherBuild([u8; 16]); |
31 | | |
32 | | impl SipHasherBuild { |
33 | 153 | pub fn new(seed: [u8; 16]) -> SipHasherBuild { |
34 | 153 | SipHasherBuild(seed) |
35 | 153 | } _RNvMNtCsN16ciHI6Qf_7smoldot4utilNtB2_14SipHasherBuild3new Line | Count | Source | 33 | 69 | pub fn new(seed: [u8; 16]) -> SipHasherBuild { | 34 | 69 | SipHasherBuild(seed) | 35 | 69 | } |
_RNvMNtCseuYC0Zibziv_7smoldot4utilNtB2_14SipHasherBuild3new Line | Count | Source | 33 | 84 | pub fn new(seed: [u8; 16]) -> SipHasherBuild { | 34 | 84 | SipHasherBuild(seed) | 35 | 84 | } |
|
36 | | } |
37 | | |
38 | | impl core::hash::BuildHasher for SipHasherBuild { |
39 | | type Hasher = siphasher::sip::SipHasher13; |
40 | | |
41 | 2.41k | fn build_hasher(&self) -> Self::Hasher { |
42 | 2.41k | siphasher::sip::SipHasher13::new_with_key(&self.0) |
43 | 2.41k | } _RNvXs_NtCsN16ciHI6Qf_7smoldot4utilNtB4_14SipHasherBuildNtNtCsaYZPK01V26L_4core4hash11BuildHasher12build_hasher Line | Count | Source | 41 | 2.41k | fn build_hasher(&self) -> Self::Hasher { | 42 | 2.41k | siphasher::sip::SipHasher13::new_with_key(&self.0) | 43 | 2.41k | } |
Unexecuted instantiation: _RNvXs_NtCseuYC0Zibziv_7smoldot4utilNtB4_14SipHasherBuildNtNtCsaYZPK01V26L_4core4hash11BuildHasher12build_hasher |
44 | | } |
45 | | |
46 | | /// Returns an iterator that yields the content of `container`. |
47 | 1.42M | pub(crate) fn as_ref_iter<T: Clone>( |
48 | 1.42M | container: impl AsRef<[T]>, |
49 | 1.42M | ) -> impl ExactSizeIterator<Item = T> + iter::FusedIterator { |
50 | 1.42M | struct Iter<C, T>(C, usize, marker::PhantomData<T>); |
51 | 1.42M | |
52 | 1.42M | impl<T: Clone, C: AsRef<[T]>> Iterator for Iter<C, T> { |
53 | 1.42M | type Item = T; |
54 | 1.42M | |
55 | 3.96M | fn next(&mut self) -> Option<Self::Item> { |
56 | 3.96M | let as_ref = self.0.as_ref(); |
57 | 3.96M | |
58 | 3.96M | if self.1 == as_ref.len() { |
59 | 1.42M | return None; |
60 | 2.53M | } |
61 | 2.53M | |
62 | 2.53M | let item = as_ref[self.1].clone(); |
63 | 2.53M | self.1 += 1; |
64 | 2.53M | Some(item) |
65 | 3.96M | } Unexecuted instantiation: _RNvXNvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1q_EEB1q_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextB6_ _RNvXNvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBV_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1u_EEB1Z_EB1u_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextB6_ Line | Count | Source | 55 | 3.96M | fn next(&mut self) -> Option<Self::Item> { | 56 | 3.96M | let as_ref = self.0.as_ref(); | 57 | 3.96M | | 58 | 3.96M | if self.1 == as_ref.len() { | 59 | 1.42M | return None; | 60 | 2.53M | } | 61 | 2.53M | | 62 | 2.53M | let item = as_ref[self.1].clone(); | 63 | 2.53M | self.1 += 1; | 64 | 2.53M | Some(item) | 65 | 3.96M | } |
_RNvXNvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBV_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1F_11Interpreter11read_memory12AccessOffsetB1t_EEB1t_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextB6_ Line | Count | Source | 55 | 431 | fn next(&mut self) -> Option<Self::Item> { | 56 | 431 | let as_ref = self.0.as_ref(); | 57 | 431 | | 58 | 431 | if self.1 == as_ref.len() { | 59 | 15 | return None; | 60 | 416 | } | 61 | 416 | | 62 | 416 | let item = as_ref[self.1].clone(); | 63 | 416 | self.1 += 1; | 64 | 416 | Some(item) | 65 | 431 | } |
_RNvXNvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1B_11Interpreter11read_memory12AccessOffsetB1p_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextB6_ Line | Count | Source | 55 | 33 | fn next(&mut self) -> Option<Self::Item> { | 56 | 33 | let as_ref = self.0.as_ref(); | 57 | 33 | | 58 | 33 | if self.1 == as_ref.len() { | 59 | 1 | return None; | 60 | 32 | } | 61 | 32 | | 62 | 32 | let item = as_ref[self.1].clone(); | 63 | 32 | self.1 += 1; | 64 | 32 | Some(item) | 65 | 33 | } |
Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsDDUKWWCHAU_18smoldot_light_wasm Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsDDUKWWCHAU_18smoldot_light_wasm Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsDDUKWWCHAU_18smoldot_light_wasm Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsDDUKWWCHAU_18smoldot_light_wasm Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextB6_ Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextB6_ Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextB6_ Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextB6_ Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsiUjFBJteJ7x_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsiUjFBJteJ7x_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsiUjFBJteJ7x_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsiUjFBJteJ7x_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCscDgN54JpMGG_6author Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCscDgN54JpMGG_6author Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCscDgN54JpMGG_6author Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCscDgN54JpMGG_6author Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsibGXYHQB8Ea_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsibGXYHQB8Ea_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsibGXYHQB8Ea_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator4nextCsibGXYHQB8Ea_25json_rpc_general_requests |
66 | 1.42M | |
67 | 1.42M | fn size_hint(&self) -> (usize, Option<usize>) { |
68 | 396k | let as_ref = self.0.as_ref(); |
69 | 396k | let len = as_ref.len() - self.1; |
70 | 396k | (len, Some(len)) |
71 | 396k | } _RNvXNvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBV_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1u_EEB1Z_EB1u_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintB6_ Line | Count | Source | 67 | 396k | fn size_hint(&self) -> (usize, Option<usize>) { | 68 | 396k | let as_ref = self.0.as_ref(); | 69 | 396k | let len = as_ref.len() - self.1; | 70 | 396k | (len, Some(len)) | 71 | 396k | } |
_RNvXNvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBV_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1F_11Interpreter11read_memory12AccessOffsetB1t_EEB1t_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintB6_ Line | Count | Source | 67 | 13 | fn size_hint(&self) -> (usize, Option<usize>) { | 68 | 13 | let as_ref = self.0.as_ref(); | 69 | 13 | let len = as_ref.len() - self.1; | 70 | 13 | (len, Some(len)) | 71 | 13 | } |
_RNvXNvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1B_11Interpreter11read_memory12AccessOffsetB1p_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintB6_ Line | Count | Source | 67 | 1 | fn size_hint(&self) -> (usize, Option<usize>) { | 68 | 1 | let as_ref = self.0.as_ref(); | 69 | 1 | let len = as_ref.len() - self.1; | 70 | 1 | (len, Some(len)) | 71 | 1 | } |
Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintB6_ Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintB6_ Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintB6_ Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintB6_ Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsiUjFBJteJ7x_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsiUjFBJteJ7x_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsiUjFBJteJ7x_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsiUjFBJteJ7x_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCscDgN54JpMGG_6author Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCscDgN54JpMGG_6author Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCscDgN54JpMGG_6author Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCscDgN54JpMGG_6author Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsibGXYHQB8Ea_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsdZExvAaxgia_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsibGXYHQB8Ea_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsibGXYHQB8Ea_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterINtB2_4IterINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCsaYZPK01V26L_4core4iter6traits8iterator8Iterator9size_hintCsibGXYHQB8Ea_25json_rpc_general_requests |
72 | 1.42M | } |
73 | 1.42M | |
74 | 1.42M | impl<T: Clone, C: AsRef<[T]>> ExactSizeIterator for Iter<C, T> {} |
75 | 1.42M | impl<T: Clone, C: AsRef<[T]>> iter::FusedIterator for Iter<C, T> {} |
76 | 1.42M | |
77 | 1.42M | Iter(container, 0, marker::PhantomData::<T>) |
78 | 1.42M | } Unexecuted instantiation: _RINvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherABH_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBH_EEEB4_ _RINvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherIB1a_ABH_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBH_EEB1Q_EEB4_ Line | Count | Source | 47 | 1.42M | pub(crate) fn as_ref_iter<T: Clone>( | 48 | 1.42M | container: impl AsRef<[T]>, | 49 | 1.42M | ) -> impl ExactSizeIterator<Item = T> + iter::FusedIterator { | 50 | 1.42M | struct Iter<C, T>(C, usize, marker::PhantomData<T>); | 51 | 1.42M | | 52 | 1.42M | impl<T: Clone, C: AsRef<[T]>> Iterator for Iter<C, T> { | 53 | 1.42M | type Item = T; | 54 | 1.42M | | 55 | 1.42M | fn next(&mut self) -> Option<Self::Item> { | 56 | 1.42M | let as_ref = self.0.as_ref(); | 57 | 1.42M | | 58 | 1.42M | if self.1 == as_ref.len() { | 59 | 1.42M | return None; | 60 | 1.42M | } | 61 | 1.42M | | 62 | 1.42M | let item = as_ref[self.1].clone(); | 63 | 1.42M | self.1 += 1; | 64 | 1.42M | Some(item) | 65 | 1.42M | } | 66 | 1.42M | | 67 | 1.42M | fn size_hint(&self) -> (usize, Option<usize>) { | 68 | 1.42M | let as_ref = self.0.as_ref(); | 69 | 1.42M | let len = as_ref.len() - self.1; | 70 | 1.42M | (len, Some(len)) | 71 | 1.42M | } | 72 | 1.42M | } | 73 | 1.42M | | 74 | 1.42M | impl<T: Clone, C: AsRef<[T]>> ExactSizeIterator for Iter<C, T> {} | 75 | 1.42M | impl<T: Clone, C: AsRef<[T]>> iter::FusedIterator for Iter<C, T> {} | 76 | 1.42M | | 77 | 1.42M | Iter(container, 0, marker::PhantomData::<T>) | 78 | 1.42M | } |
_RINvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterhINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB4_8executor2vm11interpreterNtB1p_11Interpreter11read_memory12AccessOffsetB1d_EEEB4_ Line | Count | Source | 47 | 1 | pub(crate) fn as_ref_iter<T: Clone>( | 48 | 1 | container: impl AsRef<[T]>, | 49 | 1 | ) -> impl ExactSizeIterator<Item = T> + iter::FusedIterator { | 50 | 1 | struct Iter<C, T>(C, usize, marker::PhantomData<T>); | 51 | 1 | | 52 | 1 | impl<T: Clone, C: AsRef<[T]>> Iterator for Iter<C, T> { | 53 | 1 | type Item = T; | 54 | 1 | | 55 | 1 | fn next(&mut self) -> Option<Self::Item> { | 56 | 1 | let as_ref = self.0.as_ref(); | 57 | 1 | | 58 | 1 | if self.1 == as_ref.len() { | 59 | 1 | return None; | 60 | 1 | } | 61 | 1 | | 62 | 1 | let item = as_ref[self.1].clone(); | 63 | 1 | self.1 += 1; | 64 | 1 | Some(item) | 65 | 1 | } | 66 | 1 | | 67 | 1 | fn size_hint(&self) -> (usize, Option<usize>) { | 68 | 1 | let as_ref = self.0.as_ref(); | 69 | 1 | let len = as_ref.len() - self.1; | 70 | 1 | (len, Some(len)) | 71 | 1 | } | 72 | 1 | } | 73 | 1 | | 74 | 1 | impl<T: Clone, C: AsRef<[T]>> ExactSizeIterator for Iter<C, T> {} | 75 | 1 | impl<T: Clone, C: AsRef<[T]>> iter::FusedIterator for Iter<C, T> {} | 76 | 1 | | 77 | 1 | Iter(container, 0, marker::PhantomData::<T>) | 78 | 1 | } |
_RINvNtCsN16ciHI6Qf_7smoldot4util11as_ref_iterhINtCs1qmLyiTSqYF_6either6EitherIBJ_RShINtNvMs5_NtNtNtB4_8executor2vm11interpreterNtB1t_11Interpreter11read_memory12AccessOffsetB1h_EEB1h_EEB4_ Line | Count | Source | 47 | 15 | pub(crate) fn as_ref_iter<T: Clone>( | 48 | 15 | container: impl AsRef<[T]>, | 49 | 15 | ) -> impl ExactSizeIterator<Item = T> + iter::FusedIterator { | 50 | 15 | struct Iter<C, T>(C, usize, marker::PhantomData<T>); | 51 | 15 | | 52 | 15 | impl<T: Clone, C: AsRef<[T]>> Iterator for Iter<C, T> { | 53 | 15 | type Item = T; | 54 | 15 | | 55 | 15 | fn next(&mut self) -> Option<Self::Item> { | 56 | 15 | let as_ref = self.0.as_ref(); | 57 | 15 | | 58 | 15 | if self.1 == as_ref.len() { | 59 | 15 | return None; | 60 | 15 | } | 61 | 15 | | 62 | 15 | let item = as_ref[self.1].clone(); | 63 | 15 | self.1 += 1; | 64 | 15 | Some(item) | 65 | 15 | } | 66 | 15 | | 67 | 15 | fn size_hint(&self) -> (usize, Option<usize>) { | 68 | 15 | let as_ref = self.0.as_ref(); | 69 | 15 | let len = as_ref.len() - self.1; | 70 | 15 | (len, Some(len)) | 71 | 15 | } | 72 | 15 | } | 73 | 15 | | 74 | 15 | impl<T: Clone, C: AsRef<[T]>> ExactSizeIterator for Iter<C, T> {} | 75 | 15 | impl<T: Clone, C: AsRef<[T]>> iter::FusedIterator for Iter<C, T> {} | 76 | 15 | | 77 | 15 | Iter(container, 0, marker::PhantomData::<T>) | 78 | 15 | } |
Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEECsDDUKWWCHAU_18smoldot_light_wasm Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherIB1b_ABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEB1R_EECsDDUKWWCHAU_18smoldot_light_wasm Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterhINtCs1qmLyiTSqYF_6either6EitherRShINtNvMs5_NtNtNtB4_8executor2vm11interpreterNtB1q_11Interpreter11read_memory12AccessOffsetB1e_EEEB4_ Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterhINtCs1qmLyiTSqYF_6either6EitherIBK_RShINtNvMs5_NtNtNtB4_8executor2vm11interpreterNtB1u_11Interpreter11read_memory12AccessOffsetB1i_EEB1i_EEB4_ Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEEB4_ Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherIB1b_ABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEB1R_EEB4_ Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEECsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherIB1b_ABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEB1R_EECsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEECsiUjFBJteJ7x_17smoldot_full_node Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherIB1b_ABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEB1R_EECsiUjFBJteJ7x_17smoldot_full_node Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEECscDgN54JpMGG_6author Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherIB1b_ABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEB1R_EECscDgN54JpMGG_6author Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEECsibGXYHQB8Ea_25json_rpc_general_requests Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCs1qmLyiTSqYF_6either6EitherIB1b_ABI_j1_RINtNtCsdZExvAaxgia_5alloc3vec3VecBI_EEB1R_EECsibGXYHQB8Ea_25json_rpc_general_requests |
79 | | |
80 | | /// Returns a parser that decodes a SCALE-encoded `Option`. |
81 | | /// |
82 | | /// > **Note**: When using this function outside of a `nom` "context", you might have to explicit |
83 | | /// > the type of `E`. Use `nom::Err<nom::error::Error>`. |
84 | 10 | pub(crate) fn nom_option_decode<'a, O, E: nom::error::ParseError<&'a [u8]>>( |
85 | 10 | inner_decode: impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], O, E>, |
86 | 10 | ) -> impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], Option<O>, E> { |
87 | 10 | nom::branch::alt(( |
88 | 10 | nom::combinator::map(nom::bytes::streaming::tag(&[0]), |_| None2 ), Unexecuted instantiation: _RNCINvNtCsN16ciHI6Qf_7smoldot4util17nom_option_decodeNtNtNtB6_4sync4para26PersistedValidationDataRefINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShENCINvNtB1F_10combinator3mapB29_TB29_yB29_mEBP_B1A_NCINvNtB1F_8sequence5tupleB29_B2I_B1A_TINvB4_16nom_bytes_decodeB1A_ENCINvB2i_7map_optB29_B29_yB1A_NCINvNtNtB1F_5bytes9streaming4takejB29_B1A_E0NCINvB4_29nom_varsize_number_decode_u64B1A_E0E0NCIB4E_mB29_B1A_E0INvNtNtB1F_6number9streaming6le_u32B29_B1A_EEE0NCINvBR_25persisted_validation_dataB1A_E0E0E0B6_ _RNCINvNtCsN16ciHI6Qf_7smoldot4util17nom_option_decodemINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEINvNtNtBV_6number9streaming6le_u32B1p_BQ_EE0B6_ Line | Count | Source | 88 | 2 | nom::combinator::map(nom::bytes::streaming::tag(&[0]), |_| None), |
Unexecuted instantiation: _RNCINvNtCsN16ciHI6Qf_7smoldot4util17nom_option_decodemTRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEINvNtNtBY_6number9streaming6le_u32BR_BQ_EE0B6_ Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util17nom_option_decodeNtNtNtB6_4sync4para26PersistedValidationDataRefINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShENCINvNtB1G_10combinator3mapB2a_TB2a_yB2a_mEBQ_B1B_NCINvNtB1G_8sequence5tupleB2a_B2J_B1B_TINvB4_16nom_bytes_decodeB1B_ENCINvB2j_7map_optB2a_B2a_yB1B_NCINvNtNtB1G_5bytes9streaming4takejB2a_B1B_E0NCINvB4_29nom_varsize_number_decode_u64B1B_E0E0NCIB4F_mB2a_B1B_E0INvNtNtB1G_6number9streaming6le_u32B2a_B1B_EEE0NCINvBS_25persisted_validation_dataB1B_E0E0E0B6_ Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util17nom_option_decodemINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEINvNtNtBW_6number9streaming6le_u32B1q_BR_EE0B6_ Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util17nom_option_decodemTRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEINvNtNtBZ_6number9streaming6le_u32BS_BR_EE0B6_ |
89 | 10 | nom::combinator::map( |
90 | 10 | nom::sequence::preceded(nom::bytes::streaming::tag(&[1]), inner_decode), |
91 | 10 | Some, |
92 | 10 | ), |
93 | 10 | )) |
94 | 10 | } _RINvNtCsN16ciHI6Qf_7smoldot4util17nom_option_decodemTRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEINvNtNtBW_6number9streaming6le_u32BP_BO_EEB4_ Line | Count | Source | 84 | 5 | pub(crate) fn nom_option_decode<'a, O, E: nom::error::ParseError<&'a [u8]>>( | 85 | 5 | inner_decode: impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], O, E>, | 86 | 5 | ) -> impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], Option<O>, E> { | 87 | 5 | nom::branch::alt(( | 88 | 5 | nom::combinator::map(nom::bytes::streaming::tag(&[0]), |_| None), | 89 | 5 | nom::combinator::map( | 90 | 5 | nom::sequence::preceded(nom::bytes::streaming::tag(&[1]), inner_decode), | 91 | 5 | Some, | 92 | 5 | ), | 93 | 5 | )) | 94 | 5 | } |
_RINvNtCsN16ciHI6Qf_7smoldot4util17nom_option_decodemINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEINvNtNtBT_6number9streaming6le_u32B1n_BO_EEB4_ Line | Count | Source | 84 | 4 | pub(crate) fn nom_option_decode<'a, O, E: nom::error::ParseError<&'a [u8]>>( | 85 | 4 | inner_decode: impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], O, E>, | 86 | 4 | ) -> impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], Option<O>, E> { | 87 | 4 | nom::branch::alt(( | 88 | 4 | nom::combinator::map(nom::bytes::streaming::tag(&[0]), |_| None), | 89 | 4 | nom::combinator::map( | 90 | 4 | nom::sequence::preceded(nom::bytes::streaming::tag(&[1]), inner_decode), | 91 | 4 | Some, | 92 | 4 | ), | 93 | 4 | )) | 94 | 4 | } |
_RINvNtCsN16ciHI6Qf_7smoldot4util17nom_option_decodeNtNtNtB4_4sync4para26PersistedValidationDataRefINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShENCINvNtB1D_10combinator3mapB27_TB27_yB27_mEBN_B1y_NCINvNtB1D_8sequence5tupleB27_B2G_B1y_TINvB2_16nom_bytes_decodeB1y_ENCINvB2g_7map_optB27_B27_yB1y_NCINvNtNtB1D_5bytes9streaming4takejB27_B1y_E0NCINvB2_29nom_varsize_number_decode_u64B1y_E0E0NCIB4C_mB27_B1y_E0INvNtNtB1D_6number9streaming6le_u32B27_B1y_EEE0NCINvBP_25persisted_validation_dataB1y_E0E0EB4_ Line | Count | Source | 84 | 1 | pub(crate) fn nom_option_decode<'a, O, E: nom::error::ParseError<&'a [u8]>>( | 85 | 1 | inner_decode: impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], O, E>, | 86 | 1 | ) -> impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], Option<O>, E> { | 87 | 1 | nom::branch::alt(( | 88 | 1 | nom::combinator::map(nom::bytes::streaming::tag(&[0]), |_| None), | 89 | 1 | nom::combinator::map( | 90 | 1 | nom::sequence::preceded(nom::bytes::streaming::tag(&[1]), inner_decode), | 91 | 1 | Some, | 92 | 1 | ), | 93 | 1 | )) | 94 | 1 | } |
Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util17nom_option_decodemTRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEINvNtNtBX_6number9streaming6le_u32BQ_BP_EEB4_ Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util17nom_option_decodemINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEINvNtNtBU_6number9streaming6le_u32B1o_BP_EEB4_ Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util17nom_option_decodeNtNtNtB4_4sync4para26PersistedValidationDataRefINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShENCINvNtB1E_10combinator3mapB28_TB28_yB28_mEBO_B1z_NCINvNtB1E_8sequence5tupleB28_B2H_B1z_TINvB2_16nom_bytes_decodeB1z_ENCINvB2h_7map_optB28_B28_yB1z_NCINvNtNtB1E_5bytes9streaming4takejB28_B1z_E0NCINvB2_29nom_varsize_number_decode_u64B1z_E0E0NCIB4D_mB28_B1z_E0INvNtNtB1E_6number9streaming6le_u32B28_B1z_EEE0NCINvBQ_25persisted_validation_dataB1z_E0E0EB4_ |
95 | | |
96 | | /// Decodes a SCALE-encoded vector of bytes. |
97 | 21.9k | pub(crate) fn nom_bytes_decode<'a, E: nom::error::ParseError<&'a [u8]>>( |
98 | 21.9k | bytes: &'a [u8], |
99 | 21.9k | ) -> nom::IResult<&'a [u8], &'a [u8], E> { |
100 | 21.9k | nom::multi::length_data(crate::util::nom_scale_compact_usize)(bytes) |
101 | 21.9k | } _RINvNtCsN16ciHI6Qf_7smoldot4util16nom_bytes_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ Line | Count | Source | 97 | 21.9k | pub(crate) fn nom_bytes_decode<'a, E: nom::error::ParseError<&'a [u8]>>( | 98 | 21.9k | bytes: &'a [u8], | 99 | 21.9k | ) -> nom::IResult<&'a [u8], &'a [u8], E> { | 100 | 21.9k | nom::multi::length_data(crate::util::nom_scale_compact_usize)(bytes) | 101 | 21.9k | } |
Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util16nom_bytes_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEECsDDUKWWCHAU_18smoldot_light_wasm Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util16nom_bytes_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util16nom_bytes_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEECsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util16nom_bytes_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEECscDgN54JpMGG_6author Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util16nom_bytes_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEECsibGXYHQB8Ea_25json_rpc_general_requests |
102 | | |
103 | | /// Decodes a SCALE-encoded string. |
104 | 766 | pub(crate) fn nom_string_decode< |
105 | 766 | 'a, |
106 | 766 | E: nom::error::ParseError<&'a [u8]> + nom::error::FromExternalError<&'a [u8], str::Utf8Error>, |
107 | 766 | >( |
108 | 766 | bytes: &'a [u8], |
109 | 766 | ) -> nom::IResult<&'a [u8], &'a str, E> { |
110 | 766 | nom::combinator::map_res( |
111 | 766 | nom::multi::length_data(crate::util::nom_scale_compact_usize), |
112 | 766 | str::from_utf8, |
113 | 766 | )(bytes) |
114 | 766 | } _RINvNtCsN16ciHI6Qf_7smoldot4util17nom_string_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ Line | Count | Source | 104 | 170 | pub(crate) fn nom_string_decode< | 105 | 170 | 'a, | 106 | 170 | E: nom::error::ParseError<&'a [u8]> + nom::error::FromExternalError<&'a [u8], str::Utf8Error>, | 107 | 170 | >( | 108 | 170 | bytes: &'a [u8], | 109 | 170 | ) -> nom::IResult<&'a [u8], &'a str, E> { | 110 | 170 | nom::combinator::map_res( | 111 | 170 | nom::multi::length_data(crate::util::nom_scale_compact_usize), | 112 | 170 | str::from_utf8, | 113 | 170 | )(bytes) | 114 | 170 | } |
_RINvNtCseuYC0Zibziv_7smoldot4util17nom_string_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ Line | Count | Source | 104 | 596 | pub(crate) fn nom_string_decode< | 105 | 596 | 'a, | 106 | 596 | E: nom::error::ParseError<&'a [u8]> + nom::error::FromExternalError<&'a [u8], str::Utf8Error>, | 107 | 596 | >( | 108 | 596 | bytes: &'a [u8], | 109 | 596 | ) -> nom::IResult<&'a [u8], &'a str, E> { | 110 | 596 | nom::combinator::map_res( | 111 | 596 | nom::multi::length_data(crate::util::nom_scale_compact_usize), | 112 | 596 | str::from_utf8, | 113 | 596 | )(bytes) | 114 | 596 | } |
|
115 | | |
116 | | /// Decodes a SCALE-encoded boolean. |
117 | 0 | pub(crate) fn nom_bool_decode<'a, E: nom::error::ParseError<&'a [u8]>>( |
118 | 0 | bytes: &'a [u8], |
119 | 0 | ) -> nom::IResult<&'a [u8], bool, E> { |
120 | 0 | nom::branch::alt(( |
121 | 0 | nom::combinator::map(nom::bytes::streaming::tag(&[0]), |_| false), Unexecuted instantiation: _RNCINvNtCsN16ciHI6Qf_7smoldot4util15nom_bool_decodepE0B6_ Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util15nom_bool_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0B6_ |
122 | 0 | nom::combinator::map(nom::bytes::streaming::tag(&[1]), |_| true), Unexecuted instantiation: _RNCINvNtCsN16ciHI6Qf_7smoldot4util15nom_bool_decodepEs_0B6_ Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util15nom_bool_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEs_0B6_ |
123 | 0 | ))(bytes) |
124 | 0 | } Unexecuted instantiation: _RINvNtCsN16ciHI6Qf_7smoldot4util15nom_bool_decodepEB4_ Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util15nom_bool_decodeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ |
125 | | |
126 | | /// Decodes into a `u64` a SCALE-encoded number whose number of bytes isn't known at compile-time. |
127 | | /// |
128 | | /// Returns an error if the decoded number doesn't fit into a `u64`. |
129 | 135 | pub(crate) fn nom_varsize_number_decode_u64<'a, E: nom::error::ParseError<&'a [u8]>>( |
130 | 135 | num_bytes: usize, |
131 | 135 | ) -> impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], u64, E> { |
132 | 135 | nom::combinator::map_opt( |
133 | 135 | nom::bytes::streaming::take(num_bytes), |
134 | 135 | move |slice: &[u8]| { |
135 | 19 | // `slice` contains the little endian block number. We extend the block |
136 | 19 | // number to 64bits if it is smaller, or return an error if it is larger |
137 | 19 | // than 64bits and doesn't fit in a u64. |
138 | 19 | let mut slice_out = [0; 8]; |
139 | 19 | let clamp = cmp::min(8, num_bytes); |
140 | 19 | if slice.iter().skip(clamp).any(|b| *b != 00 ) { Unexecuted instantiation: _RNCNCINvNtCsN16ciHI6Qf_7smoldot4util29nom_varsize_number_decode_u64INtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE00B8_ Unexecuted instantiation: _RNCNCINvNtCsN16ciHI6Qf_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEE00B8_ Unexecuted instantiation: _RNCNCINvNtCseuYC0Zibziv_7smoldot4util29nom_varsize_number_decode_u64INtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE00B8_ Unexecuted instantiation: _RNCNCINvNtCseuYC0Zibziv_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEE00B8_ |
141 | 0 | return None; |
142 | 19 | } |
143 | 19 | slice_out[..clamp].copy_from_slice(&slice[..clamp]); |
144 | 19 | Some(u64::from_le_bytes(slice_out)) |
145 | 135 | }19 , _RNCINvNtCsN16ciHI6Qf_7smoldot4util29nom_varsize_number_decode_u64INtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0B6_ Line | Count | Source | 134 | 16 | move |slice: &[u8]| { | 135 | 16 | // `slice` contains the little endian block number. We extend the block | 136 | 16 | // number to 64bits if it is smaller, or return an error if it is larger | 137 | 16 | // than 64bits and doesn't fit in a u64. | 138 | 16 | let mut slice_out = [0; 8]; | 139 | 16 | let clamp = cmp::min(8, num_bytes); | 140 | 16 | if slice.iter().skip(clamp).any(|b| *b != 0) { | 141 | 0 | return None; | 142 | 16 | } | 143 | 16 | slice_out[..clamp].copy_from_slice(&slice[..clamp]); | 144 | 16 | Some(u64::from_le_bytes(slice_out)) | 145 | 16 | }, |
_RNCINvNtCsN16ciHI6Qf_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEE0B6_ Line | Count | Source | 134 | 3 | move |slice: &[u8]| { | 135 | 3 | // `slice` contains the little endian block number. We extend the block | 136 | 3 | // number to 64bits if it is smaller, or return an error if it is larger | 137 | 3 | // than 64bits and doesn't fit in a u64. | 138 | 3 | let mut slice_out = [0; 8]; | 139 | 3 | let clamp = cmp::min(8, num_bytes); | 140 | 3 | if slice.iter().skip(clamp).any(|b| *b != 0) { | 141 | 0 | return None; | 142 | 3 | } | 143 | 3 | slice_out[..clamp].copy_from_slice(&slice[..clamp]); | 144 | 3 | Some(u64::from_le_bytes(slice_out)) | 145 | 3 | }, |
Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util29nom_varsize_number_decode_u64INtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0B6_ Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEE0B6_ |
146 | 135 | ) |
147 | 135 | } _RINvNtCsN16ciHI6Qf_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEEB4_ Line | Count | Source | 129 | 125 | pub(crate) fn nom_varsize_number_decode_u64<'a, E: nom::error::ParseError<&'a [u8]>>( | 130 | 125 | num_bytes: usize, | 131 | 125 | ) -> impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], u64, E> { | 132 | 125 | nom::combinator::map_opt( | 133 | 125 | nom::bytes::streaming::take(num_bytes), | 134 | 125 | move |slice: &[u8]| { | 135 | | // `slice` contains the little endian block number. We extend the block | 136 | | // number to 64bits if it is smaller, or return an error if it is larger | 137 | | // than 64bits and doesn't fit in a u64. | 138 | | let mut slice_out = [0; 8]; | 139 | | let clamp = cmp::min(8, num_bytes); | 140 | | if slice.iter().skip(clamp).any(|b| *b != 0) { | 141 | | return None; | 142 | | } | 143 | | slice_out[..clamp].copy_from_slice(&slice[..clamp]); | 144 | | Some(u64::from_le_bytes(slice_out)) | 145 | 125 | }, | 146 | 125 | ) | 147 | 125 | } |
_RINvNtCsN16ciHI6Qf_7smoldot4util29nom_varsize_number_decode_u64INtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ Line | Count | Source | 129 | 10 | pub(crate) fn nom_varsize_number_decode_u64<'a, E: nom::error::ParseError<&'a [u8]>>( | 130 | 10 | num_bytes: usize, | 131 | 10 | ) -> impl FnMut(&'a [u8]) -> nom::IResult<&'a [u8], u64, E> { | 132 | 10 | nom::combinator::map_opt( | 133 | 10 | nom::bytes::streaming::take(num_bytes), | 134 | 10 | move |slice: &[u8]| { | 135 | | // `slice` contains the little endian block number. We extend the block | 136 | | // number to 64bits if it is smaller, or return an error if it is larger | 137 | | // than 64bits and doesn't fit in a u64. | 138 | | let mut slice_out = [0; 8]; | 139 | | let clamp = cmp::min(8, num_bytes); | 140 | | if slice.iter().skip(clamp).any(|b| *b != 0) { | 141 | | return None; | 142 | | } | 143 | | slice_out[..clamp].copy_from_slice(&slice[..clamp]); | 144 | | Some(u64::from_le_bytes(slice_out)) | 145 | 10 | }, | 146 | 10 | ) | 147 | 10 | } |
Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEEB4_ Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util29nom_varsize_number_decode_u64INtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ |
148 | | |
149 | | macro_rules! decode_scale_compact { |
150 | | ($fn_name:ident, $num_ty:ty) => { |
151 | | /// Decodes a SCALE-compact-encoded integer. |
152 | | /// |
153 | | /// > **Note**: When using this function outside of a `nom` "context", you might have to |
154 | | /// > explicit the type of `E`. Use `nom::error::Error<&[u8]>`. |
155 | 700k | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( |
156 | 700k | bytes: &'a [u8], |
157 | 700k | ) -> nom::IResult<&'a [u8], $num_ty, E> { |
158 | 700k | if bytes.is_empty() { |
159 | 20 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); |
160 | 700k | } |
161 | 700k | |
162 | 700k | match bytes[0] & 0b11 { |
163 | | 0b00 => { |
164 | 690k | let value = bytes[0] >> 2; |
165 | 690k | Ok((&bytes[1..], <$num_ty>::from(value))) |
166 | | } |
167 | | 0b01 => { |
168 | 9.52k | if bytes.len() < 2 { |
169 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( |
170 | 0 | core::num::NonZeroUsize::new(2 - bytes.len()).unwrap(), |
171 | 0 | ))); |
172 | 9.52k | } |
173 | 9.52k | |
174 | 9.52k | let byte0 = u16::from(bytes[0] >> 2); |
175 | 9.52k | let byte1 = u16::from(bytes[1]); |
176 | 9.52k | |
177 | 9.52k | // Value is invalid if highest byte is 0. |
178 | 9.52k | if byte1 == 0 { |
179 | 0 | return Err(nom::Err::Error(nom::error::make_error( |
180 | 0 | bytes, |
181 | 0 | nom::error::ErrorKind::Satisfy, |
182 | 0 | ))); |
183 | 9.52k | } |
184 | 9.52k | |
185 | 9.52k | let value = (byte1 << 6) | byte0; |
186 | 9.52k | Ok((&bytes[2..], <$num_ty>::from(value))) |
187 | | } |
188 | | 0b10 => { |
189 | 29 | if bytes.len() < 4 { |
190 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( |
191 | 0 | core::num::NonZeroUsize::new(4 - bytes.len()).unwrap(), |
192 | 0 | ))); |
193 | 29 | } |
194 | 29 | |
195 | 29 | // The code below uses `checked_shl` because using plain `<<` sometimes panics |
196 | 29 | // with "attempt to shift left with overflow", even though it is |
197 | 29 | // mathematically impossible for this to happen. I strongly suspect a |
198 | 29 | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed |
199 | 29 | // to isolate the problem in a reproducible case. |
200 | 29 | let byte0 = u32::from(bytes[0] >> 2); |
201 | 29 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); |
202 | 29 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); |
203 | 29 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); |
204 | 29 | |
205 | 29 | // Value is invalid if value could have been encoded with 2 fewer bytes. |
206 | 29 | if byte2 == 0 && byte3 == 02 { |
207 | 2 | return Err(nom::Err::Error(nom::error::make_error( |
208 | 2 | bytes, |
209 | 2 | nom::error::ErrorKind::Satisfy, |
210 | 2 | ))); |
211 | 27 | } |
212 | 27 | |
213 | 27 | let value = byte3 | byte2 | byte1 | byte0; |
214 | 27 | let value = match <$num_ty>::try_from(value) { |
215 | 27 | Ok(v) => v, |
216 | | Err(_) => { |
217 | 0 | return Err(nom::Err::Error(nom::error::make_error( |
218 | 0 | bytes, |
219 | 0 | nom::error::ErrorKind::Satisfy, |
220 | 0 | ))) |
221 | | } |
222 | | }; |
223 | 27 | Ok((&bytes[4..], value)) |
224 | | } |
225 | | 0b11 => { |
226 | 13 | let num_bytes = usize::from(bytes[0] >> 2) + 4; |
227 | 13 | |
228 | 13 | if bytes.len() < num_bytes + 1 { |
229 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( |
230 | 0 | core::num::NonZeroUsize::new(num_bytes + 1 - bytes.len()).unwrap(), |
231 | 0 | ))); |
232 | 13 | } |
233 | 13 | |
234 | 13 | // Value is invalid if highest byte is 0. |
235 | 13 | if bytes[num_bytes] == 0 { |
236 | 4 | return Err(nom::Err::Error(nom::error::make_error( |
237 | 4 | bytes, |
238 | 4 | nom::error::ErrorKind::Satisfy, |
239 | 4 | ))); |
240 | 9 | } |
241 | 9 | |
242 | 9 | let mut out_value = 0; |
243 | 9 | let mut shift = 0u32; |
244 | 78 | for byte_index in 1..=num_bytes9 { |
245 | 78 | out_value |= match <$num_ty>::from(1u8) |
246 | 78 | .checked_shl(shift) |
247 | 78 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) |
248 | | { |
249 | 71 | Some(v) => v, |
250 | | None => { |
251 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. |
252 | 7 | return Err(nom::Err::Error(nom::error::make_error( |
253 | 7 | bytes, |
254 | 7 | nom::error::ErrorKind::Satisfy, |
255 | 7 | ))); |
256 | | } |
257 | | }; |
258 | | |
259 | | // Overflows aren't properly handled because `out_value` is expected to |
260 | | // overflow way sooner than `shift`. |
261 | 71 | shift += 8; |
262 | | } |
263 | | |
264 | 2 | Ok((&bytes[num_bytes + 1..], out_value)) |
265 | | } |
266 | 0 | _ => unreachable!(), |
267 | | } |
268 | 700k | } _RINvNtCsN16ciHI6Qf_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ Line | Count | Source | 155 | 697k | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( | 156 | 697k | bytes: &'a [u8], | 157 | 697k | ) -> nom::IResult<&'a [u8], $num_ty, E> { | 158 | 697k | if bytes.is_empty() { | 159 | 20 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); | 160 | 697k | } | 161 | 697k | | 162 | 697k | match bytes[0] & 0b11 { | 163 | | 0b00 => { | 164 | 687k | let value = bytes[0] >> 2; | 165 | 687k | Ok((&bytes[1..], <$num_ty>::from(value))) | 166 | | } | 167 | | 0b01 => { | 168 | 9.51k | if bytes.len() < 2 { | 169 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 170 | 0 | core::num::NonZeroUsize::new(2 - bytes.len()).unwrap(), | 171 | 0 | ))); | 172 | 9.51k | } | 173 | 9.51k | | 174 | 9.51k | let byte0 = u16::from(bytes[0] >> 2); | 175 | 9.51k | let byte1 = u16::from(bytes[1]); | 176 | 9.51k | | 177 | 9.51k | // Value is invalid if highest byte is 0. | 178 | 9.51k | if byte1 == 0 { | 179 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 180 | 0 | bytes, | 181 | 0 | nom::error::ErrorKind::Satisfy, | 182 | 0 | ))); | 183 | 9.51k | } | 184 | 9.51k | | 185 | 9.51k | let value = (byte1 << 6) | byte0; | 186 | 9.51k | Ok((&bytes[2..], <$num_ty>::from(value))) | 187 | | } | 188 | | 0b10 => { | 189 | 22 | if bytes.len() < 4 { | 190 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 191 | 0 | core::num::NonZeroUsize::new(4 - bytes.len()).unwrap(), | 192 | 0 | ))); | 193 | 22 | } | 194 | 22 | | 195 | 22 | // The code below uses `checked_shl` because using plain `<<` sometimes panics | 196 | 22 | // with "attempt to shift left with overflow", even though it is | 197 | 22 | // mathematically impossible for this to happen. I strongly suspect a | 198 | 22 | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed | 199 | 22 | // to isolate the problem in a reproducible case. | 200 | 22 | let byte0 = u32::from(bytes[0] >> 2); | 201 | 22 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); | 202 | 22 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); | 203 | 22 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); | 204 | 22 | | 205 | 22 | // Value is invalid if value could have been encoded with 2 fewer bytes. | 206 | 22 | if byte2 == 0 && byte3 == 02 { | 207 | 2 | return Err(nom::Err::Error(nom::error::make_error( | 208 | 2 | bytes, | 209 | 2 | nom::error::ErrorKind::Satisfy, | 210 | 2 | ))); | 211 | 20 | } | 212 | 20 | | 213 | 20 | let value = byte3 | byte2 | byte1 | byte0; | 214 | 20 | let value = match <$num_ty>::try_from(value) { | 215 | 20 | Ok(v) => v, | 216 | | Err(_) => { | 217 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 218 | 0 | bytes, | 219 | 0 | nom::error::ErrorKind::Satisfy, | 220 | 0 | ))) | 221 | | } | 222 | | }; | 223 | 20 | Ok((&bytes[4..], value)) | 224 | | } | 225 | | 0b11 => { | 226 | 13 | let num_bytes = usize::from(bytes[0] >> 2) + 4; | 227 | 13 | | 228 | 13 | if bytes.len() < num_bytes + 1 { | 229 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 230 | 0 | core::num::NonZeroUsize::new(num_bytes + 1 - bytes.len()).unwrap(), | 231 | 0 | ))); | 232 | 13 | } | 233 | 13 | | 234 | 13 | // Value is invalid if highest byte is 0. | 235 | 13 | if bytes[num_bytes] == 0 { | 236 | 4 | return Err(nom::Err::Error(nom::error::make_error( | 237 | 4 | bytes, | 238 | 4 | nom::error::ErrorKind::Satisfy, | 239 | 4 | ))); | 240 | 9 | } | 241 | 9 | | 242 | 9 | let mut out_value = 0; | 243 | 9 | let mut shift = 0u32; | 244 | 78 | for byte_index in 1..=num_bytes9 { | 245 | 78 | out_value |= match <$num_ty>::from(1u8) | 246 | 78 | .checked_shl(shift) | 247 | 78 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) | 248 | | { | 249 | 71 | Some(v) => v, | 250 | | None => { | 251 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. | 252 | 7 | return Err(nom::Err::Error(nom::error::make_error( | 253 | 7 | bytes, | 254 | 7 | nom::error::ErrorKind::Satisfy, | 255 | 7 | ))); | 256 | | } | 257 | | }; | 258 | | | 259 | | // Overflows aren't properly handled because `out_value` is expected to | 260 | | // overflow way sooner than `shift`. | 261 | 71 | shift += 8; | 262 | | } | 263 | | | 264 | 2 | Ok((&bytes[num_bytes + 1..], out_value)) | 265 | | } | 266 | 0 | _ => unreachable!(), | 267 | | } | 268 | 697k | } |
_RINvNtCsN16ciHI6Qf_7smoldot4util23nom_scale_compact_usizeTRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEEB4_ Line | Count | Source | 155 | 20 | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( | 156 | 20 | bytes: &'a [u8], | 157 | 20 | ) -> nom::IResult<&'a [u8], $num_ty, E> { | 158 | 20 | if bytes.is_empty() { | 159 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); | 160 | 20 | } | 161 | 20 | | 162 | 20 | match bytes[0] & 0b11 { | 163 | | 0b00 => { | 164 | 12 | let value = bytes[0] >> 2; | 165 | 12 | Ok((&bytes[1..], <$num_ty>::from(value))) | 166 | | } | 167 | | 0b01 => { | 168 | 5 | if bytes.len() < 2 { | 169 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 170 | 0 | core::num::NonZeroUsize::new(2 - bytes.len()).unwrap(), | 171 | 0 | ))); | 172 | 5 | } | 173 | 5 | | 174 | 5 | let byte0 = u16::from(bytes[0] >> 2); | 175 | 5 | let byte1 = u16::from(bytes[1]); | 176 | 5 | | 177 | 5 | // Value is invalid if highest byte is 0. | 178 | 5 | if byte1 == 0 { | 179 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 180 | 0 | bytes, | 181 | 0 | nom::error::ErrorKind::Satisfy, | 182 | 0 | ))); | 183 | 5 | } | 184 | 5 | | 185 | 5 | let value = (byte1 << 6) | byte0; | 186 | 5 | Ok((&bytes[2..], <$num_ty>::from(value))) | 187 | | } | 188 | | 0b10 => { | 189 | 3 | if bytes.len() < 4 { | 190 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 191 | 0 | core::num::NonZeroUsize::new(4 - bytes.len()).unwrap(), | 192 | 0 | ))); | 193 | 3 | } | 194 | 3 | | 195 | 3 | // The code below uses `checked_shl` because using plain `<<` sometimes panics | 196 | 3 | // with "attempt to shift left with overflow", even though it is | 197 | 3 | // mathematically impossible for this to happen. I strongly suspect a | 198 | 3 | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed | 199 | 3 | // to isolate the problem in a reproducible case. | 200 | 3 | let byte0 = u32::from(bytes[0] >> 2); | 201 | 3 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); | 202 | 3 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); | 203 | 3 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); | 204 | 3 | | 205 | 3 | // Value is invalid if value could have been encoded with 2 fewer bytes. | 206 | 3 | if byte2 == 0 && byte3 == 00 { | 207 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 208 | 0 | bytes, | 209 | 0 | nom::error::ErrorKind::Satisfy, | 210 | 0 | ))); | 211 | 3 | } | 212 | 3 | | 213 | 3 | let value = byte3 | byte2 | byte1 | byte0; | 214 | 3 | let value = match <$num_ty>::try_from(value) { | 215 | 3 | Ok(v) => v, | 216 | | Err(_) => { | 217 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 218 | 0 | bytes, | 219 | 0 | nom::error::ErrorKind::Satisfy, | 220 | 0 | ))) | 221 | | } | 222 | | }; | 223 | 3 | Ok((&bytes[4..], value)) | 224 | | } | 225 | | 0b11 => { | 226 | 0 | let num_bytes = usize::from(bytes[0] >> 2) + 4; | 227 | 0 |
| 228 | 0 | if bytes.len() < num_bytes + 1 { | 229 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 230 | 0 | core::num::NonZeroUsize::new(num_bytes + 1 - bytes.len()).unwrap(), | 231 | 0 | ))); | 232 | 0 | } | 233 | 0 |
| 234 | 0 | // Value is invalid if highest byte is 0. | 235 | 0 | if bytes[num_bytes] == 0 { | 236 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 237 | 0 | bytes, | 238 | 0 | nom::error::ErrorKind::Satisfy, | 239 | 0 | ))); | 240 | 0 | } | 241 | 0 |
| 242 | 0 | let mut out_value = 0; | 243 | 0 | let mut shift = 0u32; | 244 | 0 | for byte_index in 1..=num_bytes { | 245 | 0 | out_value |= match <$num_ty>::from(1u8) | 246 | 0 | .checked_shl(shift) | 247 | 0 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) | 248 | | { | 249 | 0 | Some(v) => v, | 250 | | None => { | 251 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. | 252 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 253 | 0 | bytes, | 254 | 0 | nom::error::ErrorKind::Satisfy, | 255 | 0 | ))); | 256 | | } | 257 | | }; | 258 | | | 259 | | // Overflows aren't properly handled because `out_value` is expected to | 260 | | // overflow way sooner than `shift`. | 261 | 0 | shift += 8; | 262 | | } | 263 | | | 264 | 0 | Ok((&bytes[num_bytes + 1..], out_value)) | 265 | | } | 266 | 0 | _ => unreachable!(), | 267 | | } | 268 | 20 | } |
_RINvNtCsN16ciHI6Qf_7smoldot4util21nom_scale_compact_u64INtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ Line | Count | Source | 155 | 1.05k | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( | 156 | 1.05k | bytes: &'a [u8], | 157 | 1.05k | ) -> nom::IResult<&'a [u8], $num_ty, E> { | 158 | 1.05k | if bytes.is_empty() { | 159 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); | 160 | 1.05k | } | 161 | 1.05k | | 162 | 1.05k | match bytes[0] & 0b11 { | 163 | | 0b00 => { | 164 | 1.04k | let value = bytes[0] >> 2; | 165 | 1.04k | Ok((&bytes[1..], <$num_ty>::from(value))) | 166 | | } | 167 | | 0b01 => { | 168 | 1 | if bytes.len() < 2 { | 169 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 170 | 0 | core::num::NonZeroUsize::new(2 - bytes.len()).unwrap(), | 171 | 0 | ))); | 172 | 1 | } | 173 | 1 | | 174 | 1 | let byte0 = u16::from(bytes[0] >> 2); | 175 | 1 | let byte1 = u16::from(bytes[1]); | 176 | 1 | | 177 | 1 | // Value is invalid if highest byte is 0. | 178 | 1 | if byte1 == 0 { | 179 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 180 | 0 | bytes, | 181 | 0 | nom::error::ErrorKind::Satisfy, | 182 | 0 | ))); | 183 | 1 | } | 184 | 1 | | 185 | 1 | let value = (byte1 << 6) | byte0; | 186 | 1 | Ok((&bytes[2..], <$num_ty>::from(value))) | 187 | | } | 188 | | 0b10 => { | 189 | 3 | if bytes.len() < 4 { | 190 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 191 | 0 | core::num::NonZeroUsize::new(4 - bytes.len()).unwrap(), | 192 | 0 | ))); | 193 | 3 | } | 194 | 3 | | 195 | 3 | // The code below uses `checked_shl` because using plain `<<` sometimes panics | 196 | 3 | // with "attempt to shift left with overflow", even though it is | 197 | 3 | // mathematically impossible for this to happen. I strongly suspect a | 198 | 3 | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed | 199 | 3 | // to isolate the problem in a reproducible case. | 200 | 3 | let byte0 = u32::from(bytes[0] >> 2); | 201 | 3 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); | 202 | 3 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); | 203 | 3 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); | 204 | 3 | | 205 | 3 | // Value is invalid if value could have been encoded with 2 fewer bytes. | 206 | 3 | if byte2 == 0 && byte3 == 00 { | 207 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 208 | 0 | bytes, | 209 | 0 | nom::error::ErrorKind::Satisfy, | 210 | 0 | ))); | 211 | 3 | } | 212 | 3 | | 213 | 3 | let value = byte3 | byte2 | byte1 | byte0; | 214 | 3 | let value = match <$num_ty>::try_from(value) { | 215 | 3 | Ok(v) => v, | 216 | | Err(_) => { | 217 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 218 | 0 | bytes, | 219 | 0 | nom::error::ErrorKind::Satisfy, | 220 | 0 | ))) | 221 | | } | 222 | | }; | 223 | 3 | Ok((&bytes[4..], value)) | 224 | | } | 225 | | 0b11 => { | 226 | 0 | let num_bytes = usize::from(bytes[0] >> 2) + 4; | 227 | 0 |
| 228 | 0 | if bytes.len() < num_bytes + 1 { | 229 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 230 | 0 | core::num::NonZeroUsize::new(num_bytes + 1 - bytes.len()).unwrap(), | 231 | 0 | ))); | 232 | 0 | } | 233 | 0 |
| 234 | 0 | // Value is invalid if highest byte is 0. | 235 | 0 | if bytes[num_bytes] == 0 { | 236 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 237 | 0 | bytes, | 238 | 0 | nom::error::ErrorKind::Satisfy, | 239 | 0 | ))); | 240 | 0 | } | 241 | 0 |
| 242 | 0 | let mut out_value = 0; | 243 | 0 | let mut shift = 0u32; | 244 | 0 | for byte_index in 1..=num_bytes { | 245 | 0 | out_value |= match <$num_ty>::from(1u8) | 246 | 0 | .checked_shl(shift) | 247 | 0 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) | 248 | | { | 249 | 0 | Some(v) => v, | 250 | | None => { | 251 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. | 252 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 253 | 0 | bytes, | 254 | 0 | nom::error::ErrorKind::Satisfy, | 255 | 0 | ))); | 256 | | } | 257 | | }; | 258 | | | 259 | | // Overflows aren't properly handled because `out_value` is expected to | 260 | | // overflow way sooner than `shift`. | 261 | 0 | shift += 8; | 262 | | } | 263 | | | 264 | 0 | Ok((&bytes[num_bytes + 1..], out_value)) | 265 | | } | 266 | 0 | _ => unreachable!(), | 267 | | } | 268 | 1.05k | } |
Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEECsDDUKWWCHAU_18smoldot_light_wasm _RINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ Line | Count | Source | 155 | 1.44k | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( | 156 | 1.44k | bytes: &'a [u8], | 157 | 1.44k | ) -> nom::IResult<&'a [u8], $num_ty, E> { | 158 | 1.44k | if bytes.is_empty() { | 159 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); | 160 | 1.44k | } | 161 | 1.44k | | 162 | 1.44k | match bytes[0] & 0b11 { | 163 | | 0b00 => { | 164 | 1.44k | let value = bytes[0] >> 2; | 165 | 1.44k | Ok((&bytes[1..], <$num_ty>::from(value))) | 166 | | } | 167 | | 0b01 => { | 168 | 0 | if bytes.len() < 2 { | 169 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 170 | 0 | core::num::NonZeroUsize::new(2 - bytes.len()).unwrap(), | 171 | 0 | ))); | 172 | 0 | } | 173 | 0 |
| 174 | 0 | let byte0 = u16::from(bytes[0] >> 2); | 175 | 0 | let byte1 = u16::from(bytes[1]); | 176 | 0 |
| 177 | 0 | // Value is invalid if highest byte is 0. | 178 | 0 | if byte1 == 0 { | 179 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 180 | 0 | bytes, | 181 | 0 | nom::error::ErrorKind::Satisfy, | 182 | 0 | ))); | 183 | 0 | } | 184 | 0 |
| 185 | 0 | let value = (byte1 << 6) | byte0; | 186 | 0 | Ok((&bytes[2..], <$num_ty>::from(value))) | 187 | | } | 188 | | 0b10 => { | 189 | 1 | if bytes.len() < 4 { | 190 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 191 | 0 | core::num::NonZeroUsize::new(4 - bytes.len()).unwrap(), | 192 | 0 | ))); | 193 | 1 | } | 194 | 1 | | 195 | 1 | // The code below uses `checked_shl` because using plain `<<` sometimes panics | 196 | 1 | // with "attempt to shift left with overflow", even though it is | 197 | 1 | // mathematically impossible for this to happen. I strongly suspect a | 198 | 1 | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed | 199 | 1 | // to isolate the problem in a reproducible case. | 200 | 1 | let byte0 = u32::from(bytes[0] >> 2); | 201 | 1 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); | 202 | 1 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); | 203 | 1 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); | 204 | 1 | | 205 | 1 | // Value is invalid if value could have been encoded with 2 fewer bytes. | 206 | 1 | if byte2 == 0 && byte3 == 00 { | 207 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 208 | 0 | bytes, | 209 | 0 | nom::error::ErrorKind::Satisfy, | 210 | 0 | ))); | 211 | 1 | } | 212 | 1 | | 213 | 1 | let value = byte3 | byte2 | byte1 | byte0; | 214 | 1 | let value = match <$num_ty>::try_from(value) { | 215 | 1 | Ok(v) => v, | 216 | | Err(_) => { | 217 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 218 | 0 | bytes, | 219 | 0 | nom::error::ErrorKind::Satisfy, | 220 | 0 | ))) | 221 | | } | 222 | | }; | 223 | 1 | Ok((&bytes[4..], value)) | 224 | | } | 225 | | 0b11 => { | 226 | 0 | let num_bytes = usize::from(bytes[0] >> 2) + 4; | 227 | 0 |
| 228 | 0 | if bytes.len() < num_bytes + 1 { | 229 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 230 | 0 | core::num::NonZeroUsize::new(num_bytes + 1 - bytes.len()).unwrap(), | 231 | 0 | ))); | 232 | 0 | } | 233 | 0 |
| 234 | 0 | // Value is invalid if highest byte is 0. | 235 | 0 | if bytes[num_bytes] == 0 { | 236 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 237 | 0 | bytes, | 238 | 0 | nom::error::ErrorKind::Satisfy, | 239 | 0 | ))); | 240 | 0 | } | 241 | 0 |
| 242 | 0 | let mut out_value = 0; | 243 | 0 | let mut shift = 0u32; | 244 | 0 | for byte_index in 1..=num_bytes { | 245 | 0 | out_value |= match <$num_ty>::from(1u8) | 246 | 0 | .checked_shl(shift) | 247 | 0 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) | 248 | | { | 249 | 0 | Some(v) => v, | 250 | | None => { | 251 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. | 252 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 253 | 0 | bytes, | 254 | 0 | nom::error::ErrorKind::Satisfy, | 255 | 0 | ))); | 256 | | } | 257 | | }; | 258 | | | 259 | | // Overflows aren't properly handled because `out_value` is expected to | 260 | | // overflow way sooner than `shift`. | 261 | 0 | shift += 8; | 262 | | } | 263 | | | 264 | 0 | Ok((&bytes[num_bytes + 1..], out_value)) | 265 | | } | 266 | 0 | _ => unreachable!(), | 267 | | } | 268 | 1.44k | } |
Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeTRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEEB4_ _RINvNtCseuYC0Zibziv_7smoldot4util21nom_scale_compact_u64INtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEEB4_ Line | Count | Source | 155 | 464 | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( | 156 | 464 | bytes: &'a [u8], | 157 | 464 | ) -> nom::IResult<&'a [u8], $num_ty, E> { | 158 | 464 | if bytes.is_empty() { | 159 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); | 160 | 464 | } | 161 | 464 | | 162 | 464 | match bytes[0] & 0b11 { | 163 | | 0b00 => { | 164 | 464 | let value = bytes[0] >> 2; | 165 | 464 | Ok((&bytes[1..], <$num_ty>::from(value))) | 166 | | } | 167 | | 0b01 => { | 168 | 0 | if bytes.len() < 2 { | 169 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 170 | 0 | core::num::NonZeroUsize::new(2 - bytes.len()).unwrap(), | 171 | 0 | ))); | 172 | 0 | } | 173 | 0 |
| 174 | 0 | let byte0 = u16::from(bytes[0] >> 2); | 175 | 0 | let byte1 = u16::from(bytes[1]); | 176 | 0 |
| 177 | 0 | // Value is invalid if highest byte is 0. | 178 | 0 | if byte1 == 0 { | 179 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 180 | 0 | bytes, | 181 | 0 | nom::error::ErrorKind::Satisfy, | 182 | 0 | ))); | 183 | 0 | } | 184 | 0 |
| 185 | 0 | let value = (byte1 << 6) | byte0; | 186 | 0 | Ok((&bytes[2..], <$num_ty>::from(value))) | 187 | | } | 188 | | 0b10 => { | 189 | 0 | if bytes.len() < 4 { | 190 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 191 | 0 | core::num::NonZeroUsize::new(4 - bytes.len()).unwrap(), | 192 | 0 | ))); | 193 | 0 | } | 194 | 0 |
| 195 | 0 | // The code below uses `checked_shl` because using plain `<<` sometimes panics | 196 | 0 | // with "attempt to shift left with overflow", even though it is | 197 | 0 | // mathematically impossible for this to happen. I strongly suspect a | 198 | 0 | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed | 199 | 0 | // to isolate the problem in a reproducible case. | 200 | 0 | let byte0 = u32::from(bytes[0] >> 2); | 201 | 0 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); | 202 | 0 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); | 203 | 0 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); | 204 | 0 |
| 205 | 0 | // Value is invalid if value could have been encoded with 2 fewer bytes. | 206 | 0 | if byte2 == 0 && byte3 == 0 { | 207 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 208 | 0 | bytes, | 209 | 0 | nom::error::ErrorKind::Satisfy, | 210 | 0 | ))); | 211 | 0 | } | 212 | 0 |
| 213 | 0 | let value = byte3 | byte2 | byte1 | byte0; | 214 | 0 | let value = match <$num_ty>::try_from(value) { | 215 | 0 | Ok(v) => v, | 216 | | Err(_) => { | 217 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 218 | 0 | bytes, | 219 | 0 | nom::error::ErrorKind::Satisfy, | 220 | 0 | ))) | 221 | | } | 222 | | }; | 223 | 0 | Ok((&bytes[4..], value)) | 224 | | } | 225 | | 0b11 => { | 226 | 0 | let num_bytes = usize::from(bytes[0] >> 2) + 4; | 227 | 0 |
| 228 | 0 | if bytes.len() < num_bytes + 1 { | 229 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 230 | 0 | core::num::NonZeroUsize::new(num_bytes + 1 - bytes.len()).unwrap(), | 231 | 0 | ))); | 232 | 0 | } | 233 | 0 |
| 234 | 0 | // Value is invalid if highest byte is 0. | 235 | 0 | if bytes[num_bytes] == 0 { | 236 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 237 | 0 | bytes, | 238 | 0 | nom::error::ErrorKind::Satisfy, | 239 | 0 | ))); | 240 | 0 | } | 241 | 0 |
| 242 | 0 | let mut out_value = 0; | 243 | 0 | let mut shift = 0u32; | 244 | 0 | for byte_index in 1..=num_bytes { | 245 | 0 | out_value |= match <$num_ty>::from(1u8) | 246 | 0 | .checked_shl(shift) | 247 | 0 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) | 248 | | { | 249 | 0 | Some(v) => v, | 250 | | None => { | 251 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. | 252 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 253 | 0 | bytes, | 254 | 0 | nom::error::ErrorKind::Satisfy, | 255 | 0 | ))); | 256 | | } | 257 | | }; | 258 | | | 259 | | // Overflows aren't properly handled because `out_value` is expected to | 260 | | // overflow way sooner than `shift`. | 261 | 0 | shift += 8; | 262 | | } | 263 | | | 264 | 0 | Ok((&bytes[num_bytes + 1..], out_value)) | 265 | | } | 266 | 0 | _ => unreachable!(), | 267 | | } | 268 | 464 | } |
Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEECsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEECscDgN54JpMGG_6author Unexecuted instantiation: _RINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEECsibGXYHQB8Ea_25json_rpc_general_requests |
269 | | }; |
270 | | } |
271 | | |
272 | 71 | decode_scale_compact!(nom_scale_compact_usize, usize); _RNCINvNtCsN16ciHI6Qf_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0B6_ Line | Count | Source | 272 | 71 | decode_scale_compact!(nom_scale_compact_usize, usize); |
Unexecuted instantiation: _RNCINvNtCsN16ciHI6Qf_7smoldot4util23nom_scale_compact_usizeTRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEE0B6_ Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0CsDDUKWWCHAU_18smoldot_light_wasm Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0B6_ Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeTRShNtNtCs6ga8gEqbpRc_3nom5error9ErrorKindEE0B6_ Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0CsiLzmwikkc22_14json_rpc_basic Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0CscDgN54JpMGG_6author Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util23nom_scale_compact_usizeINtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0CsibGXYHQB8Ea_25json_rpc_general_requests |
273 | 0 | decode_scale_compact!(nom_scale_compact_u64, u64); Unexecuted instantiation: _RNCINvNtCsN16ciHI6Qf_7smoldot4util21nom_scale_compact_u64INtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0B6_ Unexecuted instantiation: _RNCINvNtCseuYC0Zibziv_7smoldot4util21nom_scale_compact_u64INtNtCs6ga8gEqbpRc_3nom5error5ErrorRShEE0B6_ |
274 | | |
275 | | macro_rules! encode_scale_compact { |
276 | | ($fn_name:ident, $num_ty:ty) => { |
277 | | /// Returns a buffer containing the SCALE-compact encoding of the parameter. |
278 | 1.58M | pub(crate) fn $fn_name(mut value: $num_ty) -> impl AsRef<[u8]> + Clone { |
279 | 1.58M | const MAX_BITS: usize = 1 + (<$num_ty>::BITS as usize) / 8; |
280 | 1.58M | let mut array = arrayvec::ArrayVec::<u8, MAX_BITS>::new(); |
281 | 1.58M | |
282 | 1.58M | if value < 64 { |
283 | 1.57M | array.push(u8::try_from(value).unwrap() << 2); |
284 | 1.57M | } else if value < (1 << 14)8.83k { |
285 | 8.78k | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b01); |
286 | 8.78k | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); |
287 | 8.78k | } else if value < (1 << 30)55 { |
288 | 55 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b10); |
289 | 55 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); |
290 | 55 | array.push(u8::try_from((value >> 14) & 0xff).unwrap()); |
291 | 55 | array.push(u8::try_from((value >> 22) & 0xff).unwrap()); |
292 | 55 | } else { |
293 | 0 | array.push(0); |
294 | 0 | while value != 0 { |
295 | 0 | array.push(u8::try_from(value & 0xff).unwrap()); |
296 | 0 | value >>= 8; |
297 | 0 | } |
298 | 0 | array[0] = (u8::try_from(array.len() - 1 - 4).unwrap() << 2) | 0b11; |
299 | | } |
300 | | |
301 | 1.58M | array |
302 | 1.58M | } _RNvNtCsN16ciHI6Qf_7smoldot4util24encode_scale_compact_u64 Line | Count | Source | 278 | 1.04k | pub(crate) fn $fn_name(mut value: $num_ty) -> impl AsRef<[u8]> + Clone { | 279 | 1.04k | const MAX_BITS: usize = 1 + (<$num_ty>::BITS as usize) / 8; | 280 | 1.04k | let mut array = arrayvec::ArrayVec::<u8, MAX_BITS>::new(); | 281 | 1.04k | | 282 | 1.04k | if value < 64 { | 283 | 1.04k | array.push(u8::try_from(value).unwrap() << 2); | 284 | 1.04k | } else if value < (1 << 14)1 { | 285 | 0 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b01); | 286 | 0 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 287 | 1 | } else if value < (1 << 30) { | 288 | 1 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b10); | 289 | 1 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 290 | 1 | array.push(u8::try_from((value >> 14) & 0xff).unwrap()); | 291 | 1 | array.push(u8::try_from((value >> 22) & 0xff).unwrap()); | 292 | 1 | } else { | 293 | 0 | array.push(0); | 294 | 0 | while value != 0 { | 295 | 0 | array.push(u8::try_from(value & 0xff).unwrap()); | 296 | 0 | value >>= 8; | 297 | 0 | } | 298 | 0 | array[0] = (u8::try_from(array.len() - 1 - 4).unwrap() << 2) | 0b11; | 299 | | } | 300 | | | 301 | 1.04k | array | 302 | 1.04k | } |
_RNvNtCsN16ciHI6Qf_7smoldot4util26encode_scale_compact_usize Line | Count | Source | 278 | 1.57M | pub(crate) fn $fn_name(mut value: $num_ty) -> impl AsRef<[u8]> + Clone { | 279 | 1.57M | const MAX_BITS: usize = 1 + (<$num_ty>::BITS as usize) / 8; | 280 | 1.57M | let mut array = arrayvec::ArrayVec::<u8, MAX_BITS>::new(); | 281 | 1.57M | | 282 | 1.57M | if value < 64 { | 283 | 1.56M | array.push(u8::try_from(value).unwrap() << 2); | 284 | 1.56M | } else if value < (1 << 14)8.12k { | 285 | 8.10k | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b01); | 286 | 8.10k | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 287 | 8.10k | } else if value < (1 << 30)12 { | 288 | 12 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b10); | 289 | 12 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 290 | 12 | array.push(u8::try_from((value >> 14) & 0xff).unwrap()); | 291 | 12 | array.push(u8::try_from((value >> 22) & 0xff).unwrap()); | 292 | 12 | } else { | 293 | 0 | array.push(0); | 294 | 0 | while value != 0 { | 295 | 0 | array.push(u8::try_from(value & 0xff).unwrap()); | 296 | 0 | value >>= 8; | 297 | 0 | } | 298 | 0 | array[0] = (u8::try_from(array.len() - 1 - 4).unwrap() << 2) | 0b11; | 299 | | } | 300 | | | 301 | 1.57M | array | 302 | 1.57M | } |
_RNvNtCseuYC0Zibziv_7smoldot4util24encode_scale_compact_u64 Line | Count | Source | 278 | 147 | pub(crate) fn $fn_name(mut value: $num_ty) -> impl AsRef<[u8]> + Clone { | 279 | 147 | const MAX_BITS: usize = 1 + (<$num_ty>::BITS as usize) / 8; | 280 | 147 | let mut array = arrayvec::ArrayVec::<u8, MAX_BITS>::new(); | 281 | 147 | | 282 | 147 | if value < 64 { | 283 | 147 | array.push(u8::try_from(value).unwrap() << 2); | 284 | 147 | } else if value < (1 << 14)0 { | 285 | 0 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b01); | 286 | 0 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 287 | 0 | } else if value < (1 << 30) { | 288 | 0 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b10); | 289 | 0 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 290 | 0 | array.push(u8::try_from((value >> 14) & 0xff).unwrap()); | 291 | 0 | array.push(u8::try_from((value >> 22) & 0xff).unwrap()); | 292 | 0 | } else { | 293 | 0 | array.push(0); | 294 | 0 | while value != 0 { | 295 | 0 | array.push(u8::try_from(value & 0xff).unwrap()); | 296 | 0 | value >>= 8; | 297 | 0 | } | 298 | 0 | array[0] = (u8::try_from(array.len() - 1 - 4).unwrap() << 2) | 0b11; | 299 | | } | 300 | | | 301 | 147 | array | 302 | 147 | } |
_RNvNtCseuYC0Zibziv_7smoldot4util26encode_scale_compact_usize Line | Count | Source | 278 | 3.93k | pub(crate) fn $fn_name(mut value: $num_ty) -> impl AsRef<[u8]> + Clone { | 279 | 3.93k | const MAX_BITS: usize = 1 + (<$num_ty>::BITS as usize) / 8; | 280 | 3.93k | let mut array = arrayvec::ArrayVec::<u8, MAX_BITS>::new(); | 281 | 3.93k | | 282 | 3.93k | if value < 64 { | 283 | 3.21k | array.push(u8::try_from(value).unwrap() << 2); | 284 | 3.21k | } else if value < (1 << 14)714 { | 285 | 672 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b01); | 286 | 672 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 287 | 672 | } else if value < (1 << 30)42 { | 288 | 42 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b10); | 289 | 42 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 290 | 42 | array.push(u8::try_from((value >> 14) & 0xff).unwrap()); | 291 | 42 | array.push(u8::try_from((value >> 22) & 0xff).unwrap()); | 292 | 42 | } else { | 293 | 0 | array.push(0); | 294 | 0 | while value != 0 { | 295 | 0 | array.push(u8::try_from(value & 0xff).unwrap()); | 296 | 0 | value >>= 8; | 297 | 0 | } | 298 | 0 | array[0] = (u8::try_from(array.len() - 1 - 4).unwrap() << 2) | 0b11; | 299 | | } | 300 | | | 301 | 3.93k | array | 302 | 3.93k | } |
|
303 | | }; |
304 | | } |
305 | | |
306 | | encode_scale_compact!(encode_scale_compact_u64, u64); |
307 | | encode_scale_compact!(encode_scale_compact_usize, usize); |