/__w/smoldot/smoldot/repo/lib/src/util.rs
Line | Count | Source |
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 | } _RNvMNtCsjlkOsLH0Zfj_7smoldot4utilNtB2_14SipHasherBuild3new Line | Count | Source | 33 | 69 | pub fn new(seed: [u8; 16]) -> SipHasherBuild { | 34 | 69 | SipHasherBuild(seed) | 35 | 69 | } |
_RNvMNtCsc1ywvx6YAnK_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_NtCsjlkOsLH0Zfj_7smoldot4utilNtB4_14SipHasherBuildNtNtCs1p5UDGgVI4d_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_NtCsc1ywvx6YAnK_7smoldot4utilNtB4_14SipHasherBuildNtNtCs1p5UDGgVI4d_4core4hash11BuildHasher12build_hasher |
44 | | } |
45 | | |
46 | | /// Returns an iterator that yields the content of `container`. |
47 | 1.44M | pub(crate) fn as_ref_iter<T: Clone>( |
48 | 1.44M | container: impl AsRef<[T]>, |
49 | 1.44M | ) -> impl ExactSizeIterator<Item = T> + iter::FusedIterator { |
50 | | struct Iter<C, T>(C, usize, marker::PhantomData<T>); |
51 | | |
52 | | impl<T: Clone, C: AsRef<[T]>> Iterator for Iter<C, T> { |
53 | | type Item = T; |
54 | | |
55 | 3.98M | fn next(&mut self) -> Option<Self::Item> { |
56 | 3.98M | let as_ref = self.0.as_ref(); |
57 | | |
58 | 3.98M | if self.1 == as_ref.len() { |
59 | 1.44M | return None; |
60 | 2.54M | } |
61 | | |
62 | 2.54M | let item = as_ref[self.1].clone(); |
63 | 2.54M | self.1 += 1; |
64 | 2.54M | Some(item) |
65 | 3.98M | } Unexecuted instantiation: _RNvXNvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextB6_ _RNvXNvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextB6_ Line | Count | Source | 55 | 3.98M | fn next(&mut self) -> Option<Self::Item> { | 56 | 3.98M | let as_ref = self.0.as_ref(); | 57 | | | 58 | 3.98M | if self.1 == as_ref.len() { | 59 | 1.44M | return None; | 60 | 2.54M | } | 61 | | | 62 | 2.54M | let item = as_ref[self.1].clone(); | 63 | 2.54M | self.1 += 1; | 64 | 2.54M | Some(item) | 65 | 3.98M | } |
_RNvXNvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextB6_ Line | Count | Source | 55 | 431 | fn next(&mut self) -> Option<Self::Item> { | 56 | 431 | let as_ref = self.0.as_ref(); | 57 | | | 58 | 431 | if self.1 == as_ref.len() { | 59 | 15 | return None; | 60 | 416 | } | 61 | | | 62 | 416 | let item = as_ref[self.1].clone(); | 63 | 416 | self.1 += 1; | 64 | 416 | Some(item) | 65 | 431 | } |
_RNvXNvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextB6_ Line | Count | Source | 55 | 33 | fn next(&mut self) -> Option<Self::Item> { | 56 | 33 | let as_ref = self.0.as_ref(); | 57 | | | 58 | 33 | if self.1 == as_ref.len() { | 59 | 1 | return None; | 60 | 32 | } | 61 | | | 62 | 32 | let item = as_ref[self.1].clone(); | 63 | 32 | self.1 += 1; | 64 | 32 | Some(item) | 65 | 33 | } |
Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCscoAnRPySggw_6author Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCscoAnRPySggw_6author Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCscoAnRPySggw_6author Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCscoAnRPySggw_6author Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCsfFWJyR6nd6r_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCsfFWJyR6nd6r_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCsfFWJyR6nd6r_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCsfFWJyR6nd6r_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextB6_ Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextB6_ Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextB6_ Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextB6_ Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCs7snhGEhbuap_18smoldot_light_wasm Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCs7snhGEhbuap_18smoldot_light_wasm Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCs7snhGEhbuap_18smoldot_light_wasm Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCs7snhGEhbuap_18smoldot_light_wasm Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCs4VrkfB1pvQ3_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCs4VrkfB1pvQ3_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCs4VrkfB1pvQ3_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator4nextCs4VrkfB1pvQ3_25json_rpc_general_requests |
66 | | |
67 | 398k | fn size_hint(&self) -> (usize, Option<usize>) { |
68 | 398k | let as_ref = self.0.as_ref(); |
69 | 398k | let len = as_ref.len() - self.1; |
70 | 398k | (len, Some(len)) |
71 | 398k | } _RNvXNvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintB6_ Line | Count | Source | 67 | 398k | fn size_hint(&self) -> (usize, Option<usize>) { | 68 | 398k | let as_ref = self.0.as_ref(); | 69 | 398k | let len = as_ref.len() - self.1; | 70 | 398k | (len, Some(len)) | 71 | 398k | } |
_RNvXNvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_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 | } |
_RNvXNvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_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: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCscoAnRPySggw_6author Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCscoAnRPySggw_6author Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCscoAnRPySggw_6author Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCscoAnRPySggw_6author Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCsfFWJyR6nd6r_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCsfFWJyR6nd6r_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCsfFWJyR6nd6r_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCsfFWJyR6nd6r_17smoldot_full_node Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintB6_ Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintB6_ Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintB6_ Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintB6_ Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1r_EEB1r_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCs4VrkfB1pvQ3_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_ANtNtNtB6_4trie6nibble6Nibblej1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecB1v_EEB20_EB1v_ENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCs4VrkfB1pvQ3_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherIBW_RShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1G_11Interpreter11read_memory12AccessOffsetB1u_EEB1u_EhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCs4VrkfB1pvQ3_25json_rpc_general_requests Unexecuted instantiation: _RNvXNvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterINtB2_4IterINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB6_8executor2vm11interpreterNtB1C_11Interpreter11read_memory12AccessOffsetB1q_EEhENtNtNtNtCs1p5UDGgVI4d_4core4iter6traits8iterator8Iterator9size_hintCs4VrkfB1pvQ3_25json_rpc_general_requests |
72 | | } |
73 | | |
74 | | impl<T: Clone, C: AsRef<[T]>> ExactSizeIterator for Iter<C, T> {} |
75 | | impl<T: Clone, C: AsRef<[T]>> iter::FusedIterator for Iter<C, T> {} |
76 | | |
77 | 1.44M | Iter(container, 0, marker::PhantomData::<T>) |
78 | 1.44M | } _RINvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherIB1b_ABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEB1R_EEB4_ Line | Count | Source | 47 | 1.44M | pub(crate) fn as_ref_iter<T: Clone>( | 48 | 1.44M | container: impl AsRef<[T]>, | 49 | 1.44M | ) -> impl ExactSizeIterator<Item = T> + iter::FusedIterator { | 50 | | struct Iter<C, T>(C, usize, marker::PhantomData<T>); | 51 | | | 52 | | impl<T: Clone, C: AsRef<[T]>> Iterator for Iter<C, T> { | 53 | | type Item = T; | 54 | | | 55 | | fn next(&mut self) -> Option<Self::Item> { | 56 | | let as_ref = self.0.as_ref(); | 57 | | | 58 | | if self.1 == as_ref.len() { | 59 | | return None; | 60 | | } | 61 | | | 62 | | let item = as_ref[self.1].clone(); | 63 | | self.1 += 1; | 64 | | Some(item) | 65 | | } | 66 | | | 67 | | fn size_hint(&self) -> (usize, Option<usize>) { | 68 | | let as_ref = self.0.as_ref(); | 69 | | let len = as_ref.len() - self.1; | 70 | | (len, Some(len)) | 71 | | } | 72 | | } | 73 | | | 74 | | impl<T: Clone, C: AsRef<[T]>> ExactSizeIterator for Iter<C, T> {} | 75 | | impl<T: Clone, C: AsRef<[T]>> iter::FusedIterator for Iter<C, T> {} | 76 | | | 77 | 1.44M | Iter(container, 0, marker::PhantomData::<T>) | 78 | 1.44M | } |
Unexecuted instantiation: _RINvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEEB4_ _RINvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterhINtCsfDbbEgL1j7J_6either6EitherIBK_RShINtNvMs5_NtNtNtB4_8executor2vm11interpreterNtB1u_11Interpreter11read_memory12AccessOffsetB1i_EEB1i_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 | | struct Iter<C, T>(C, usize, marker::PhantomData<T>); | 51 | | | 52 | | impl<T: Clone, C: AsRef<[T]>> Iterator for Iter<C, T> { | 53 | | type Item = T; | 54 | | | 55 | | fn next(&mut self) -> Option<Self::Item> { | 56 | | let as_ref = self.0.as_ref(); | 57 | | | 58 | | if self.1 == as_ref.len() { | 59 | | return None; | 60 | | } | 61 | | | 62 | | let item = as_ref[self.1].clone(); | 63 | | self.1 += 1; | 64 | | Some(item) | 65 | | } | 66 | | | 67 | | fn size_hint(&self) -> (usize, Option<usize>) { | 68 | | let as_ref = self.0.as_ref(); | 69 | | let len = as_ref.len() - self.1; | 70 | | (len, Some(len)) | 71 | | } | 72 | | } | 73 | | | 74 | | impl<T: Clone, C: AsRef<[T]>> ExactSizeIterator for Iter<C, T> {} | 75 | | impl<T: Clone, C: AsRef<[T]>> iter::FusedIterator for Iter<C, T> {} | 76 | | | 77 | 15 | Iter(container, 0, marker::PhantomData::<T>) | 78 | 15 | } |
_RINvNtCsjlkOsLH0Zfj_7smoldot4util11as_ref_iterhINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB4_8executor2vm11interpreterNtB1q_11Interpreter11read_memory12AccessOffsetB1e_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 | | struct Iter<C, T>(C, usize, marker::PhantomData<T>); | 51 | | | 52 | | impl<T: Clone, C: AsRef<[T]>> Iterator for Iter<C, T> { | 53 | | type Item = T; | 54 | | | 55 | | fn next(&mut self) -> Option<Self::Item> { | 56 | | let as_ref = self.0.as_ref(); | 57 | | | 58 | | if self.1 == as_ref.len() { | 59 | | return None; | 60 | | } | 61 | | | 62 | | let item = as_ref[self.1].clone(); | 63 | | self.1 += 1; | 64 | | Some(item) | 65 | | } | 66 | | | 67 | | fn size_hint(&self) -> (usize, Option<usize>) { | 68 | | let as_ref = self.0.as_ref(); | 69 | | let len = as_ref.len() - self.1; | 70 | | (len, Some(len)) | 71 | | } | 72 | | } | 73 | | | 74 | | impl<T: Clone, C: AsRef<[T]>> ExactSizeIterator for Iter<C, T> {} | 75 | | impl<T: Clone, C: AsRef<[T]>> iter::FusedIterator for Iter<C, T> {} | 76 | | | 77 | 1 | Iter(container, 0, marker::PhantomData::<T>) | 78 | 1 | } |
Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEECscoAnRPySggw_6author Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherIB1b_ABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEB1R_EECscoAnRPySggw_6author Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEECsfFWJyR6nd6r_17smoldot_full_node Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherIB1b_ABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEB1R_EECsfFWJyR6nd6r_17smoldot_full_node Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEEB4_ Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherIB1b_ABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEB1R_EEB4_ Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterhINtCsfDbbEgL1j7J_6either6EitherIBK_RShINtNvMs5_NtNtNtB4_8executor2vm11interpreterNtB1u_11Interpreter11read_memory12AccessOffsetB1i_EEB1i_EEB4_ Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterhINtCsfDbbEgL1j7J_6either6EitherRShINtNvMs5_NtNtNtB4_8executor2vm11interpreterNtB1q_11Interpreter11read_memory12AccessOffsetB1e_EEEB4_ Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEECs7snhGEhbuap_18smoldot_light_wasm Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherIB1b_ABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEB1R_EECs7snhGEhbuap_18smoldot_light_wasm Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEECsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherIB1b_ABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEB1R_EECsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEECs4VrkfB1pvQ3_25json_rpc_general_requests Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util11as_ref_iterNtNtNtB4_4trie6nibble6NibbleINtCsfDbbEgL1j7J_6either6EitherIB1b_ABI_j1_RINtNtCsaFPxhswmqCN_5alloc3vec3VecBI_EEB1R_EECs4VrkfB1pvQ3_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 nom::Parser<&'a [u8], Output = O, Error = E>, |
86 | 10 | ) -> impl nom::Parser<&'a [u8], Output = Option<O>, Error = E> { |
87 | 10 | nom::branch::alt(( |
88 | 10 | nom::combinator::map(nom::bytes::streaming::tag(&[0][..]), |_| None), |
89 | 10 | nom::combinator::map( |
90 | 10 | nom::sequence::preceded(nom::bytes::streaming::tag(&[1][..]), inner_decode), |
91 | | Some, |
92 | | ), |
93 | | )) |
94 | 10 | } _RINvNtCsjlkOsLH0Zfj_7smoldot4util17nom_option_decodeNtNtNtB4_4sync4para26PersistedValidationDataRefINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEINtNtB1E_8internal3MapTINvB2_16nom_bytes_decodeB1z_EINtB2f_6MapOptNCINvNtNtB1E_5bytes9streaming4takejB28_B1z_E0NCINvB2_29nom_varsize_number_decode_u64B1z_E0ENCIB3j_mB28_B1z_E0INvNtNtB1E_6number9streaming6le_u32B28_B1z_EENCINvBQ_25persisted_validation_dataB1z_E0EEB4_ Line | Count | Source | 84 | 1 | pub(crate) fn nom_option_decode<'a, O, E: nom::error::ParseError<&'a [u8]>>( | 85 | 1 | inner_decode: impl nom::Parser<&'a [u8], Output = O, Error = E>, | 86 | 1 | ) -> impl nom::Parser<&'a [u8], Output = Option<O>, Error = 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 | | Some, | 92 | | ), | 93 | | )) | 94 | 1 | } |
_RINvNtCsjlkOsLH0Zfj_7smoldot4util17nom_option_decodemINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEINvNtNtBU_6number9streaming6le_u32B1o_BP_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 nom::Parser<&'a [u8], Output = O, Error = E>, | 86 | 4 | ) -> impl nom::Parser<&'a [u8], Output = Option<O>, Error = 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 | | Some, | 92 | | ), | 93 | | )) | 94 | 4 | } |
_RINvNtCsjlkOsLH0Zfj_7smoldot4util17nom_option_decodemTRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEINvNtNtBX_6number9streaming6le_u32BQ_BP_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 nom::Parser<&'a [u8], Output = O, Error = E>, | 86 | 5 | ) -> impl nom::Parser<&'a [u8], Output = Option<O>, Error = 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 | | Some, | 92 | | ), | 93 | | )) | 94 | 5 | } |
Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util17nom_option_decodemTRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEINvNtNtBX_6number9streaming6le_u32BQ_BP_EEB4_ Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util17nom_option_decodeNtNtNtB4_4sync4para26PersistedValidationDataRefINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEINtNtB1E_8internal3MapTINvB2_16nom_bytes_decodeB1z_EINtB2f_6MapOptNCINvNtNtB1E_5bytes9streaming4takejB28_B1z_E0NCINvB2_29nom_varsize_number_decode_u64B1z_E0ENCIB3j_mB28_B1z_E0INvNtNtB1E_6number9streaming6le_u32B28_B1z_EENCINvBQ_25persisted_validation_dataB1z_E0EEB4_ Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util17nom_option_decodemINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEINvNtNtBU_6number9streaming6le_u32B1o_BP_EEB4_ |
95 | | |
96 | | /// Decodes a SCALE-encoded vector of bytes. |
97 | 22.5k | pub(crate) fn nom_bytes_decode<'a, E: nom::error::ParseError<&'a [u8]>>( |
98 | 22.5k | bytes: &'a [u8], |
99 | 22.5k | ) -> nom::IResult<&'a [u8], &'a [u8], E> { |
100 | 22.5k | nom::Parser::parse( |
101 | 22.5k | &mut nom::multi::length_data(crate::util::nom_scale_compact_usize), |
102 | 22.5k | bytes, |
103 | | ) |
104 | 22.5k | } _RINvNtCsjlkOsLH0Zfj_7smoldot4util16nom_bytes_decodeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ Line | Count | Source | 97 | 22.5k | pub(crate) fn nom_bytes_decode<'a, E: nom::error::ParseError<&'a [u8]>>( | 98 | 22.5k | bytes: &'a [u8], | 99 | 22.5k | ) -> nom::IResult<&'a [u8], &'a [u8], E> { | 100 | 22.5k | nom::Parser::parse( | 101 | 22.5k | &mut nom::multi::length_data(crate::util::nom_scale_compact_usize), | 102 | 22.5k | bytes, | 103 | | ) | 104 | 22.5k | } |
Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util16nom_bytes_decodeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEECscoAnRPySggw_6author Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util16nom_bytes_decodeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util16nom_bytes_decodeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEECs7snhGEhbuap_18smoldot_light_wasm Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util16nom_bytes_decodeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEECsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util16nom_bytes_decodeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEECs4VrkfB1pvQ3_25json_rpc_general_requests |
105 | | |
106 | | /// Decodes a SCALE-encoded string. |
107 | 766 | pub(crate) fn nom_string_decode< |
108 | 766 | 'a, |
109 | 766 | E: nom::error::ParseError<&'a [u8]> + nom::error::FromExternalError<&'a [u8], str::Utf8Error>, |
110 | 766 | >( |
111 | 766 | bytes: &'a [u8], |
112 | 766 | ) -> nom::IResult<&'a [u8], &'a str, E> { |
113 | 766 | nom::Parser::parse( |
114 | 766 | &mut nom::combinator::map_res( |
115 | 766 | nom::multi::length_data(crate::util::nom_scale_compact_usize), |
116 | 766 | str::from_utf8, |
117 | 766 | ), |
118 | 766 | bytes, |
119 | | ) |
120 | 766 | } _RINvNtCsjlkOsLH0Zfj_7smoldot4util17nom_string_decodeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ Line | Count | Source | 107 | 170 | pub(crate) fn nom_string_decode< | 108 | 170 | 'a, | 109 | 170 | E: nom::error::ParseError<&'a [u8]> + nom::error::FromExternalError<&'a [u8], str::Utf8Error>, | 110 | 170 | >( | 111 | 170 | bytes: &'a [u8], | 112 | 170 | ) -> nom::IResult<&'a [u8], &'a str, E> { | 113 | 170 | nom::Parser::parse( | 114 | 170 | &mut nom::combinator::map_res( | 115 | 170 | nom::multi::length_data(crate::util::nom_scale_compact_usize), | 116 | 170 | str::from_utf8, | 117 | 170 | ), | 118 | 170 | bytes, | 119 | | ) | 120 | 170 | } |
_RINvNtCsc1ywvx6YAnK_7smoldot4util17nom_string_decodeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ Line | Count | Source | 107 | 596 | pub(crate) fn nom_string_decode< | 108 | 596 | 'a, | 109 | 596 | E: nom::error::ParseError<&'a [u8]> + nom::error::FromExternalError<&'a [u8], str::Utf8Error>, | 110 | 596 | >( | 111 | 596 | bytes: &'a [u8], | 112 | 596 | ) -> nom::IResult<&'a [u8], &'a str, E> { | 113 | 596 | nom::Parser::parse( | 114 | 596 | &mut nom::combinator::map_res( | 115 | 596 | nom::multi::length_data(crate::util::nom_scale_compact_usize), | 116 | 596 | str::from_utf8, | 117 | 596 | ), | 118 | 596 | bytes, | 119 | | ) | 120 | 596 | } |
|
121 | | |
122 | | /// Decodes a SCALE-encoded boolean. |
123 | 0 | pub(crate) fn nom_bool_decode<'a, E: nom::error::ParseError<&'a [u8]>>( |
124 | 0 | bytes: &'a [u8], |
125 | 0 | ) -> nom::IResult<&'a [u8], bool, E> { |
126 | 0 | nom::Parser::parse( |
127 | 0 | &mut nom::branch::alt(( |
128 | 0 | nom::combinator::map(nom::bytes::streaming::tag(&[0][..]), |_| false), |
129 | 0 | nom::combinator::map(nom::bytes::streaming::tag(&[1][..]), |_| true), |
130 | | )), |
131 | 0 | bytes, |
132 | | ) |
133 | 0 | } Unexecuted instantiation: _RINvNtCsjlkOsLH0Zfj_7smoldot4util15nom_bool_decodepEB4_ Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util15nom_bool_decodeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ |
134 | | |
135 | | /// Decodes into a `u64` a SCALE-encoded number whose number of bytes isn't known at compile-time. |
136 | | /// |
137 | | /// Returns an error if the decoded number doesn't fit into a `u64`. |
138 | 135 | pub(crate) fn nom_varsize_number_decode_u64<'a, E: nom::error::ParseError<&'a [u8]>>( |
139 | 135 | num_bytes: usize, |
140 | 135 | ) -> impl nom::Parser<&'a [u8], Output = u64, Error = E> { |
141 | 135 | nom::combinator::map_opt( |
142 | 135 | nom::bytes::streaming::take(num_bytes), |
143 | 19 | move |slice: &[u8]| { |
144 | | // `slice` contains the little endian block number. We extend the block |
145 | | // number to 64bits if it is smaller, or return an error if it is larger |
146 | | // than 64bits and doesn't fit in a u64. |
147 | 19 | let mut slice_out = [0; 8]; |
148 | 19 | let clamp = cmp::min(8, num_bytes); |
149 | 19 | if slice.iter().skip(clamp).any(|b| *b0 != 0) { Unexecuted instantiation: _RNCNCINvNtCsjlkOsLH0Zfj_7smoldot4util29nom_varsize_number_decode_u64INtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE00B8_ Unexecuted instantiation: _RNCNCINvNtCsjlkOsLH0Zfj_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEE00B8_ Unexecuted instantiation: _RNCNCINvNtCsc1ywvx6YAnK_7smoldot4util29nom_varsize_number_decode_u64INtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE00B8_ Unexecuted instantiation: _RNCNCINvNtCsc1ywvx6YAnK_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEE00B8_ |
150 | 0 | return None; |
151 | 19 | } |
152 | 19 | slice_out[..clamp].copy_from_slice(&slice[..clamp]); |
153 | 19 | Some(u64::from_le_bytes(slice_out)) |
154 | 19 | }, _RNCINvNtCsjlkOsLH0Zfj_7smoldot4util29nom_varsize_number_decode_u64INtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE0B6_ Line | Count | Source | 143 | 16 | move |slice: &[u8]| { | 144 | | // `slice` contains the little endian block number. We extend the block | 145 | | // number to 64bits if it is smaller, or return an error if it is larger | 146 | | // than 64bits and doesn't fit in a u64. | 147 | 16 | let mut slice_out = [0; 8]; | 148 | 16 | let clamp = cmp::min(8, num_bytes); | 149 | 16 | if slice.iter().skip(clamp).any(|b| *b != 0) { | 150 | 0 | return None; | 151 | 16 | } | 152 | 16 | slice_out[..clamp].copy_from_slice(&slice[..clamp]); | 153 | 16 | Some(u64::from_le_bytes(slice_out)) | 154 | 16 | }, |
_RNCINvNtCsjlkOsLH0Zfj_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEE0B6_ Line | Count | Source | 143 | 3 | move |slice: &[u8]| { | 144 | | // `slice` contains the little endian block number. We extend the block | 145 | | // number to 64bits if it is smaller, or return an error if it is larger | 146 | | // than 64bits and doesn't fit in a u64. | 147 | 3 | let mut slice_out = [0; 8]; | 148 | 3 | let clamp = cmp::min(8, num_bytes); | 149 | 3 | if slice.iter().skip(clamp).any(|b| *b != 0) { | 150 | 0 | return None; | 151 | 3 | } | 152 | 3 | slice_out[..clamp].copy_from_slice(&slice[..clamp]); | 153 | 3 | Some(u64::from_le_bytes(slice_out)) | 154 | 3 | }, |
Unexecuted instantiation: _RNCINvNtCsc1ywvx6YAnK_7smoldot4util29nom_varsize_number_decode_u64INtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE0B6_ Unexecuted instantiation: _RNCINvNtCsc1ywvx6YAnK_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEE0B6_ |
155 | | ) |
156 | 135 | } _RINvNtCsjlkOsLH0Zfj_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEEB4_ Line | Count | Source | 138 | 125 | pub(crate) fn nom_varsize_number_decode_u64<'a, E: nom::error::ParseError<&'a [u8]>>( | 139 | 125 | num_bytes: usize, | 140 | 125 | ) -> impl nom::Parser<&'a [u8], Output = u64, Error = E> { | 141 | 125 | nom::combinator::map_opt( | 142 | 125 | nom::bytes::streaming::take(num_bytes), | 143 | | move |slice: &[u8]| { | 144 | | // `slice` contains the little endian block number. We extend the block | 145 | | // number to 64bits if it is smaller, or return an error if it is larger | 146 | | // than 64bits and doesn't fit in a u64. | 147 | | let mut slice_out = [0; 8]; | 148 | | let clamp = cmp::min(8, num_bytes); | 149 | | if slice.iter().skip(clamp).any(|b| *b != 0) { | 150 | | return None; | 151 | | } | 152 | | slice_out[..clamp].copy_from_slice(&slice[..clamp]); | 153 | | Some(u64::from_le_bytes(slice_out)) | 154 | | }, | 155 | | ) | 156 | 125 | } |
_RINvNtCsjlkOsLH0Zfj_7smoldot4util29nom_varsize_number_decode_u64INtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ Line | Count | Source | 138 | 10 | pub(crate) fn nom_varsize_number_decode_u64<'a, E: nom::error::ParseError<&'a [u8]>>( | 139 | 10 | num_bytes: usize, | 140 | 10 | ) -> impl nom::Parser<&'a [u8], Output = u64, Error = E> { | 141 | 10 | nom::combinator::map_opt( | 142 | 10 | nom::bytes::streaming::take(num_bytes), | 143 | | move |slice: &[u8]| { | 144 | | // `slice` contains the little endian block number. We extend the block | 145 | | // number to 64bits if it is smaller, or return an error if it is larger | 146 | | // than 64bits and doesn't fit in a u64. | 147 | | let mut slice_out = [0; 8]; | 148 | | let clamp = cmp::min(8, num_bytes); | 149 | | if slice.iter().skip(clamp).any(|b| *b != 0) { | 150 | | return None; | 151 | | } | 152 | | slice_out[..clamp].copy_from_slice(&slice[..clamp]); | 153 | | Some(u64::from_le_bytes(slice_out)) | 154 | | }, | 155 | | ) | 156 | 10 | } |
Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util29nom_varsize_number_decode_u64TRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEEB4_ Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util29nom_varsize_number_decode_u64INtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ |
157 | | |
158 | | macro_rules! decode_scale_compact { |
159 | | ($fn_name:ident, $num_ty:ty) => { |
160 | | /// Decodes a SCALE-compact-encoded integer. |
161 | | /// |
162 | | /// > **Note**: When using this function outside of a `nom` "context", you might have to |
163 | | /// > explicit the type of `E`. Use `nom::error::Error<&[u8]>`. |
164 | 704k | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( |
165 | 704k | bytes: &'a [u8], |
166 | 704k | ) -> nom::IResult<&'a [u8], $num_ty, E> { |
167 | 704k | if bytes.is_empty() { |
168 | 20 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); |
169 | 704k | } |
170 | | |
171 | 704k | match bytes[0] & 0b11 { |
172 | | 0b00 => { |
173 | 694k | let value = bytes[0] >> 2; |
174 | 694k | Ok((&bytes[1..], <$num_ty>::from(value))) |
175 | | } |
176 | | 0b01 => { |
177 | 9.74k | if bytes.len() < 2 { |
178 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( |
179 | 0 | core::num::NonZero::<usize>::new(2 - bytes.len()).unwrap(), |
180 | 0 | ))); |
181 | 9.74k | } |
182 | | |
183 | 9.74k | let byte0 = u16::from(bytes[0] >> 2); |
184 | 9.74k | let byte1 = u16::from(bytes[1]); |
185 | | |
186 | | // Value is invalid if highest byte is 0. |
187 | 9.74k | if byte1 == 0 { |
188 | 0 | return Err(nom::Err::Error(nom::error::make_error( |
189 | 0 | bytes, |
190 | 0 | nom::error::ErrorKind::Satisfy, |
191 | 0 | ))); |
192 | 9.74k | } |
193 | | |
194 | 9.74k | let value = (byte1 << 6) | byte0; |
195 | 9.74k | Ok((&bytes[2..], <$num_ty>::from(value))) |
196 | | } |
197 | | 0b10 => { |
198 | 29 | if bytes.len() < 4 { |
199 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( |
200 | 0 | core::num::NonZero::<usize>::new(4 - bytes.len()).unwrap(), |
201 | 0 | ))); |
202 | 29 | } |
203 | | |
204 | | // The code below uses `checked_shl` because using plain `<<` sometimes panics |
205 | | // with "attempt to shift left with overflow", even though it is |
206 | | // mathematically impossible for this to happen. I strongly suspect a |
207 | | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed |
208 | | // to isolate the problem in a reproducible case. |
209 | 29 | let byte0 = u32::from(bytes[0] >> 2); |
210 | 29 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); |
211 | 29 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); |
212 | 29 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); |
213 | | |
214 | | // Value is invalid if value could have been encoded with 2 fewer bytes. |
215 | 29 | if byte2 == 0 && byte3 == 02 { |
216 | 2 | return Err(nom::Err::Error(nom::error::make_error( |
217 | 2 | bytes, |
218 | 2 | nom::error::ErrorKind::Satisfy, |
219 | 2 | ))); |
220 | 27 | } |
221 | | |
222 | 27 | let value = byte3 | byte2 | byte1 | byte0; |
223 | 27 | let value = match <$num_ty>::try_from(value) { |
224 | 27 | Ok(v) => v, |
225 | | Err(_) => { |
226 | 0 | return Err(nom::Err::Error(nom::error::make_error( |
227 | 0 | bytes, |
228 | 0 | nom::error::ErrorKind::Satisfy, |
229 | 0 | ))); |
230 | | } |
231 | | }; |
232 | 27 | Ok((&bytes[4..], value)) |
233 | | } |
234 | | 0b11 => { |
235 | 19 | let num_bytes = usize::from(bytes[0] >> 2) + 4; |
236 | | |
237 | 19 | if bytes.len() < num_bytes + 1 { |
238 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( |
239 | 0 | core::num::NonZero::<usize>::new(num_bytes + 1 - bytes.len()).unwrap(), |
240 | 0 | ))); |
241 | 19 | } |
242 | | |
243 | | // Value is invalid if highest byte is 0. |
244 | 19 | if bytes[num_bytes] == 0 { |
245 | 7 | return Err(nom::Err::Error(nom::error::make_error( |
246 | 7 | bytes, |
247 | 7 | nom::error::ErrorKind::Satisfy, |
248 | 7 | ))); |
249 | 12 | } |
250 | | |
251 | 12 | let mut out_value = 0; |
252 | 12 | let mut shift = 0u32; |
253 | 105 | for byte_index in 1..=num_bytes12 { |
254 | 105 | out_value |= match <$num_ty>::from(1u8) |
255 | 105 | .checked_shl(shift) |
256 | 105 | .and_then(|shl| <$num_ty>::from95 (bytes[byte_index]95 ).checked_mul95 (shl95 )) Unexecuted instantiation: _RNCINvNtCsjlkOsLH0Zfj_7smoldot4util21nom_scale_compact_u64INtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE0B6_ _RNCINvNtCsjlkOsLH0Zfj_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE0B6_ Line | Count | Source | 256 | 95 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) |
Unexecuted instantiation: _RNCINvNtCsjlkOsLH0Zfj_7smoldot4util23nom_scale_compact_usizeTRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEE0B6_ Unexecuted instantiation: _RNCINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE0CscoAnRPySggw_6author Unexecuted instantiation: _RNCINvNtCsc1ywvx6YAnK_7smoldot4util21nom_scale_compact_u64INtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE0B6_ Unexecuted instantiation: _RNCINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE0B6_ Unexecuted instantiation: _RNCINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeTRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEE0B6_ Unexecuted instantiation: _RNCINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE0Cs7snhGEhbuap_18smoldot_light_wasm Unexecuted instantiation: _RNCINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE0CsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RNCINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEE0Cs4VrkfB1pvQ3_25json_rpc_general_requests |
257 | | { |
258 | 95 | Some(v) => v, |
259 | | None => { |
260 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. |
261 | 10 | return Err(nom::Err::Error(nom::error::make_error( |
262 | 10 | bytes, |
263 | 10 | nom::error::ErrorKind::Satisfy, |
264 | 10 | ))); |
265 | | } |
266 | | }; |
267 | | |
268 | | // Overflows aren't properly handled because `out_value` is expected to |
269 | | // overflow way sooner than `shift`. |
270 | 95 | shift += 8; |
271 | | } |
272 | | |
273 | 2 | Ok((&bytes[num_bytes + 1..], out_value)) |
274 | | } |
275 | 0 | _ => unreachable!(), |
276 | | } |
277 | 704k | } _RINvNtCsjlkOsLH0Zfj_7smoldot4util21nom_scale_compact_u64INtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ Line | Count | Source | 164 | 1.05k | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( | 165 | 1.05k | bytes: &'a [u8], | 166 | 1.05k | ) -> nom::IResult<&'a [u8], $num_ty, E> { | 167 | 1.05k | if bytes.is_empty() { | 168 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); | 169 | 1.05k | } | 170 | | | 171 | 1.05k | match bytes[0] & 0b11 { | 172 | | 0b00 => { | 173 | 1.04k | let value = bytes[0] >> 2; | 174 | 1.04k | Ok((&bytes[1..], <$num_ty>::from(value))) | 175 | | } | 176 | | 0b01 => { | 177 | 1 | if bytes.len() < 2 { | 178 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 179 | 0 | core::num::NonZero::<usize>::new(2 - bytes.len()).unwrap(), | 180 | 0 | ))); | 181 | 1 | } | 182 | | | 183 | 1 | let byte0 = u16::from(bytes[0] >> 2); | 184 | 1 | let byte1 = u16::from(bytes[1]); | 185 | | | 186 | | // Value is invalid if highest byte is 0. | 187 | 1 | if byte1 == 0 { | 188 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 189 | 0 | bytes, | 190 | 0 | nom::error::ErrorKind::Satisfy, | 191 | 0 | ))); | 192 | 1 | } | 193 | | | 194 | 1 | let value = (byte1 << 6) | byte0; | 195 | 1 | Ok((&bytes[2..], <$num_ty>::from(value))) | 196 | | } | 197 | | 0b10 => { | 198 | 3 | if bytes.len() < 4 { | 199 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 200 | 0 | core::num::NonZero::<usize>::new(4 - bytes.len()).unwrap(), | 201 | 0 | ))); | 202 | 3 | } | 203 | | | 204 | | // The code below uses `checked_shl` because using plain `<<` sometimes panics | 205 | | // with "attempt to shift left with overflow", even though it is | 206 | | // mathematically impossible for this to happen. I strongly suspect a | 207 | | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed | 208 | | // to isolate the problem in a reproducible case. | 209 | 3 | let byte0 = u32::from(bytes[0] >> 2); | 210 | 3 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); | 211 | 3 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); | 212 | 3 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); | 213 | | | 214 | | // Value is invalid if value could have been encoded with 2 fewer bytes. | 215 | 3 | if byte2 == 0 && byte3 == 00 { | 216 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 217 | 0 | bytes, | 218 | 0 | nom::error::ErrorKind::Satisfy, | 219 | 0 | ))); | 220 | 3 | } | 221 | | | 222 | 3 | let value = byte3 | byte2 | byte1 | byte0; | 223 | 3 | let value = match <$num_ty>::try_from(value) { | 224 | 3 | Ok(v) => v, | 225 | | Err(_) => { | 226 | | return Err(nom::Err::Error(nom::error::make_error( | 227 | | bytes, | 228 | | nom::error::ErrorKind::Satisfy, | 229 | | ))); | 230 | | } | 231 | | }; | 232 | 3 | Ok((&bytes[4..], value)) | 233 | | } | 234 | | 0b11 => { | 235 | 0 | let num_bytes = usize::from(bytes[0] >> 2) + 4; | 236 | | | 237 | 0 | if bytes.len() < num_bytes + 1 { | 238 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 239 | 0 | core::num::NonZero::<usize>::new(num_bytes + 1 - bytes.len()).unwrap(), | 240 | 0 | ))); | 241 | 0 | } | 242 | | | 243 | | // Value is invalid if highest byte is 0. | 244 | 0 | if bytes[num_bytes] == 0 { | 245 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 246 | 0 | bytes, | 247 | 0 | nom::error::ErrorKind::Satisfy, | 248 | 0 | ))); | 249 | 0 | } | 250 | | | 251 | 0 | let mut out_value = 0; | 252 | 0 | let mut shift = 0u32; | 253 | 0 | for byte_index in 1..=num_bytes { | 254 | 0 | out_value |= match <$num_ty>::from(1u8) | 255 | 0 | .checked_shl(shift) | 256 | 0 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) | 257 | | { | 258 | 0 | Some(v) => v, | 259 | | None => { | 260 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. | 261 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 262 | 0 | bytes, | 263 | 0 | nom::error::ErrorKind::Satisfy, | 264 | 0 | ))); | 265 | | } | 266 | | }; | 267 | | | 268 | | // Overflows aren't properly handled because `out_value` is expected to | 269 | | // overflow way sooner than `shift`. | 270 | 0 | shift += 8; | 271 | | } | 272 | | | 273 | 0 | Ok((&bytes[num_bytes + 1..], out_value)) | 274 | | } | 275 | 0 | _ => unreachable!(), | 276 | | } | 277 | 1.05k | } |
_RINvNtCsjlkOsLH0Zfj_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ Line | Count | Source | 164 | 701k | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( | 165 | 701k | bytes: &'a [u8], | 166 | 701k | ) -> nom::IResult<&'a [u8], $num_ty, E> { | 167 | 701k | if bytes.is_empty() { | 168 | 20 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); | 169 | 701k | } | 170 | | | 171 | 701k | match bytes[0] & 0b11 { | 172 | | 0b00 => { | 173 | 691k | let value = bytes[0] >> 2; | 174 | 691k | Ok((&bytes[1..], <$num_ty>::from(value))) | 175 | | } | 176 | | 0b01 => { | 177 | 9.74k | if bytes.len() < 2 { | 178 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 179 | 0 | core::num::NonZero::<usize>::new(2 - bytes.len()).unwrap(), | 180 | 0 | ))); | 181 | 9.74k | } | 182 | | | 183 | 9.74k | let byte0 = u16::from(bytes[0] >> 2); | 184 | 9.74k | let byte1 = u16::from(bytes[1]); | 185 | | | 186 | | // Value is invalid if highest byte is 0. | 187 | 9.74k | if byte1 == 0 { | 188 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 189 | 0 | bytes, | 190 | 0 | nom::error::ErrorKind::Satisfy, | 191 | 0 | ))); | 192 | 9.74k | } | 193 | | | 194 | 9.74k | let value = (byte1 << 6) | byte0; | 195 | 9.74k | Ok((&bytes[2..], <$num_ty>::from(value))) | 196 | | } | 197 | | 0b10 => { | 198 | 22 | if bytes.len() < 4 { | 199 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 200 | 0 | core::num::NonZero::<usize>::new(4 - bytes.len()).unwrap(), | 201 | 0 | ))); | 202 | 22 | } | 203 | | | 204 | | // The code below uses `checked_shl` because using plain `<<` sometimes panics | 205 | | // with "attempt to shift left with overflow", even though it is | 206 | | // mathematically impossible for this to happen. I strongly suspect a | 207 | | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed | 208 | | // to isolate the problem in a reproducible case. | 209 | 22 | let byte0 = u32::from(bytes[0] >> 2); | 210 | 22 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); | 211 | 22 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); | 212 | 22 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); | 213 | | | 214 | | // Value is invalid if value could have been encoded with 2 fewer bytes. | 215 | 22 | if byte2 == 0 && byte3 == 02 { | 216 | 2 | return Err(nom::Err::Error(nom::error::make_error( | 217 | 2 | bytes, | 218 | 2 | nom::error::ErrorKind::Satisfy, | 219 | 2 | ))); | 220 | 20 | } | 221 | | | 222 | 20 | let value = byte3 | byte2 | byte1 | byte0; | 223 | 20 | let value = match <$num_ty>::try_from(value) { | 224 | 20 | Ok(v) => v, | 225 | | Err(_) => { | 226 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 227 | 0 | bytes, | 228 | 0 | nom::error::ErrorKind::Satisfy, | 229 | 0 | ))); | 230 | | } | 231 | | }; | 232 | 20 | Ok((&bytes[4..], value)) | 233 | | } | 234 | | 0b11 => { | 235 | 19 | let num_bytes = usize::from(bytes[0] >> 2) + 4; | 236 | | | 237 | 19 | if bytes.len() < num_bytes + 1 { | 238 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 239 | 0 | core::num::NonZero::<usize>::new(num_bytes + 1 - bytes.len()).unwrap(), | 240 | 0 | ))); | 241 | 19 | } | 242 | | | 243 | | // Value is invalid if highest byte is 0. | 244 | 19 | if bytes[num_bytes] == 0 { | 245 | 7 | return Err(nom::Err::Error(nom::error::make_error( | 246 | 7 | bytes, | 247 | 7 | nom::error::ErrorKind::Satisfy, | 248 | 7 | ))); | 249 | 12 | } | 250 | | | 251 | 12 | let mut out_value = 0; | 252 | 12 | let mut shift = 0u32; | 253 | 105 | for byte_index in 1..=num_bytes12 { | 254 | 105 | out_value |= match <$num_ty>::from(1u8) | 255 | 105 | .checked_shl(shift) | 256 | 105 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) | 257 | | { | 258 | 95 | Some(v) => v, | 259 | | None => { | 260 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. | 261 | 10 | return Err(nom::Err::Error(nom::error::make_error( | 262 | 10 | bytes, | 263 | 10 | nom::error::ErrorKind::Satisfy, | 264 | 10 | ))); | 265 | | } | 266 | | }; | 267 | | | 268 | | // Overflows aren't properly handled because `out_value` is expected to | 269 | | // overflow way sooner than `shift`. | 270 | 95 | shift += 8; | 271 | | } | 272 | | | 273 | 2 | Ok((&bytes[num_bytes + 1..], out_value)) | 274 | | } | 275 | 0 | _ => unreachable!(), | 276 | | } | 277 | 701k | } |
_RINvNtCsjlkOsLH0Zfj_7smoldot4util23nom_scale_compact_usizeTRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEEB4_ Line | Count | Source | 164 | 20 | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( | 165 | 20 | bytes: &'a [u8], | 166 | 20 | ) -> nom::IResult<&'a [u8], $num_ty, E> { | 167 | 20 | if bytes.is_empty() { | 168 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); | 169 | 20 | } | 170 | | | 171 | 20 | match bytes[0] & 0b11 { | 172 | | 0b00 => { | 173 | 12 | let value = bytes[0] >> 2; | 174 | 12 | Ok((&bytes[1..], <$num_ty>::from(value))) | 175 | | } | 176 | | 0b01 => { | 177 | 5 | if bytes.len() < 2 { | 178 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 179 | 0 | core::num::NonZero::<usize>::new(2 - bytes.len()).unwrap(), | 180 | 0 | ))); | 181 | 5 | } | 182 | | | 183 | 5 | let byte0 = u16::from(bytes[0] >> 2); | 184 | 5 | let byte1 = u16::from(bytes[1]); | 185 | | | 186 | | // Value is invalid if highest byte is 0. | 187 | 5 | if byte1 == 0 { | 188 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 189 | 0 | bytes, | 190 | 0 | nom::error::ErrorKind::Satisfy, | 191 | 0 | ))); | 192 | 5 | } | 193 | | | 194 | 5 | let value = (byte1 << 6) | byte0; | 195 | 5 | Ok((&bytes[2..], <$num_ty>::from(value))) | 196 | | } | 197 | | 0b10 => { | 198 | 3 | if bytes.len() < 4 { | 199 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 200 | 0 | core::num::NonZero::<usize>::new(4 - bytes.len()).unwrap(), | 201 | 0 | ))); | 202 | 3 | } | 203 | | | 204 | | // The code below uses `checked_shl` because using plain `<<` sometimes panics | 205 | | // with "attempt to shift left with overflow", even though it is | 206 | | // mathematically impossible for this to happen. I strongly suspect a | 207 | | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed | 208 | | // to isolate the problem in a reproducible case. | 209 | 3 | let byte0 = u32::from(bytes[0] >> 2); | 210 | 3 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); | 211 | 3 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); | 212 | 3 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); | 213 | | | 214 | | // Value is invalid if value could have been encoded with 2 fewer bytes. | 215 | 3 | if byte2 == 0 && byte3 == 00 { | 216 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 217 | 0 | bytes, | 218 | 0 | nom::error::ErrorKind::Satisfy, | 219 | 0 | ))); | 220 | 3 | } | 221 | | | 222 | 3 | let value = byte3 | byte2 | byte1 | byte0; | 223 | 3 | let value = match <$num_ty>::try_from(value) { | 224 | 3 | Ok(v) => v, | 225 | | Err(_) => { | 226 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 227 | 0 | bytes, | 228 | 0 | nom::error::ErrorKind::Satisfy, | 229 | 0 | ))); | 230 | | } | 231 | | }; | 232 | 3 | Ok((&bytes[4..], value)) | 233 | | } | 234 | | 0b11 => { | 235 | 0 | let num_bytes = usize::from(bytes[0] >> 2) + 4; | 236 | | | 237 | 0 | if bytes.len() < num_bytes + 1 { | 238 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 239 | 0 | core::num::NonZero::<usize>::new(num_bytes + 1 - bytes.len()).unwrap(), | 240 | 0 | ))); | 241 | 0 | } | 242 | | | 243 | | // Value is invalid if highest byte is 0. | 244 | 0 | if bytes[num_bytes] == 0 { | 245 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 246 | 0 | bytes, | 247 | 0 | nom::error::ErrorKind::Satisfy, | 248 | 0 | ))); | 249 | 0 | } | 250 | | | 251 | 0 | let mut out_value = 0; | 252 | 0 | let mut shift = 0u32; | 253 | 0 | for byte_index in 1..=num_bytes { | 254 | 0 | out_value |= match <$num_ty>::from(1u8) | 255 | 0 | .checked_shl(shift) | 256 | 0 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) | 257 | | { | 258 | 0 | Some(v) => v, | 259 | | None => { | 260 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. | 261 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 262 | 0 | bytes, | 263 | 0 | nom::error::ErrorKind::Satisfy, | 264 | 0 | ))); | 265 | | } | 266 | | }; | 267 | | | 268 | | // Overflows aren't properly handled because `out_value` is expected to | 269 | | // overflow way sooner than `shift`. | 270 | 0 | shift += 8; | 271 | | } | 272 | | | 273 | 0 | Ok((&bytes[num_bytes + 1..], out_value)) | 274 | | } | 275 | 0 | _ => unreachable!(), | 276 | | } | 277 | 20 | } |
Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEECscoAnRPySggw_6author _RINvNtCsc1ywvx6YAnK_7smoldot4util21nom_scale_compact_u64INtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ Line | Count | Source | 164 | 464 | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( | 165 | 464 | bytes: &'a [u8], | 166 | 464 | ) -> nom::IResult<&'a [u8], $num_ty, E> { | 167 | 464 | if bytes.is_empty() { | 168 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); | 169 | 464 | } | 170 | | | 171 | 464 | match bytes[0] & 0b11 { | 172 | | 0b00 => { | 173 | 464 | let value = bytes[0] >> 2; | 174 | 464 | Ok((&bytes[1..], <$num_ty>::from(value))) | 175 | | } | 176 | | 0b01 => { | 177 | 0 | if bytes.len() < 2 { | 178 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 179 | 0 | core::num::NonZero::<usize>::new(2 - bytes.len()).unwrap(), | 180 | 0 | ))); | 181 | 0 | } | 182 | | | 183 | 0 | let byte0 = u16::from(bytes[0] >> 2); | 184 | 0 | let byte1 = u16::from(bytes[1]); | 185 | | | 186 | | // Value is invalid if highest byte is 0. | 187 | 0 | if byte1 == 0 { | 188 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 189 | 0 | bytes, | 190 | 0 | nom::error::ErrorKind::Satisfy, | 191 | 0 | ))); | 192 | 0 | } | 193 | | | 194 | 0 | let value = (byte1 << 6) | byte0; | 195 | 0 | Ok((&bytes[2..], <$num_ty>::from(value))) | 196 | | } | 197 | | 0b10 => { | 198 | 0 | if bytes.len() < 4 { | 199 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 200 | 0 | core::num::NonZero::<usize>::new(4 - bytes.len()).unwrap(), | 201 | 0 | ))); | 202 | 0 | } | 203 | | | 204 | | // The code below uses `checked_shl` because using plain `<<` sometimes panics | 205 | | // with "attempt to shift left with overflow", even though it is | 206 | | // mathematically impossible for this to happen. I strongly suspect a | 207 | | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed | 208 | | // to isolate the problem in a reproducible case. | 209 | 0 | let byte0 = u32::from(bytes[0] >> 2); | 210 | 0 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); | 211 | 0 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); | 212 | 0 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); | 213 | | | 214 | | // Value is invalid if value could have been encoded with 2 fewer bytes. | 215 | 0 | if byte2 == 0 && byte3 == 0 { | 216 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 217 | 0 | bytes, | 218 | 0 | nom::error::ErrorKind::Satisfy, | 219 | 0 | ))); | 220 | 0 | } | 221 | | | 222 | 0 | let value = byte3 | byte2 | byte1 | byte0; | 223 | 0 | let value = match <$num_ty>::try_from(value) { | 224 | 0 | Ok(v) => v, | 225 | | Err(_) => { | 226 | | return Err(nom::Err::Error(nom::error::make_error( | 227 | | bytes, | 228 | | nom::error::ErrorKind::Satisfy, | 229 | | ))); | 230 | | } | 231 | | }; | 232 | 0 | Ok((&bytes[4..], value)) | 233 | | } | 234 | | 0b11 => { | 235 | 0 | let num_bytes = usize::from(bytes[0] >> 2) + 4; | 236 | | | 237 | 0 | if bytes.len() < num_bytes + 1 { | 238 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 239 | 0 | core::num::NonZero::<usize>::new(num_bytes + 1 - bytes.len()).unwrap(), | 240 | 0 | ))); | 241 | 0 | } | 242 | | | 243 | | // Value is invalid if highest byte is 0. | 244 | 0 | if bytes[num_bytes] == 0 { | 245 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 246 | 0 | bytes, | 247 | 0 | nom::error::ErrorKind::Satisfy, | 248 | 0 | ))); | 249 | 0 | } | 250 | | | 251 | 0 | let mut out_value = 0; | 252 | 0 | let mut shift = 0u32; | 253 | 0 | for byte_index in 1..=num_bytes { | 254 | 0 | out_value |= match <$num_ty>::from(1u8) | 255 | 0 | .checked_shl(shift) | 256 | 0 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) | 257 | | { | 258 | 0 | Some(v) => v, | 259 | | None => { | 260 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. | 261 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 262 | 0 | bytes, | 263 | 0 | nom::error::ErrorKind::Satisfy, | 264 | 0 | ))); | 265 | | } | 266 | | }; | 267 | | | 268 | | // Overflows aren't properly handled because `out_value` is expected to | 269 | | // overflow way sooner than `shift`. | 270 | 0 | shift += 8; | 271 | | } | 272 | | | 273 | 0 | Ok((&bytes[num_bytes + 1..], out_value)) | 274 | | } | 275 | 0 | _ => unreachable!(), | 276 | | } | 277 | 464 | } |
_RINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEEB4_ Line | Count | Source | 164 | 1.44k | pub(crate) fn $fn_name<'a, E: nom::error::ParseError<&'a [u8]>>( | 165 | 1.44k | bytes: &'a [u8], | 166 | 1.44k | ) -> nom::IResult<&'a [u8], $num_ty, E> { | 167 | 1.44k | if bytes.is_empty() { | 168 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Unknown)); | 169 | 1.44k | } | 170 | | | 171 | 1.44k | match bytes[0] & 0b11 { | 172 | | 0b00 => { | 173 | 1.44k | let value = bytes[0] >> 2; | 174 | 1.44k | Ok((&bytes[1..], <$num_ty>::from(value))) | 175 | | } | 176 | | 0b01 => { | 177 | 0 | if bytes.len() < 2 { | 178 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 179 | 0 | core::num::NonZero::<usize>::new(2 - bytes.len()).unwrap(), | 180 | 0 | ))); | 181 | 0 | } | 182 | | | 183 | 0 | let byte0 = u16::from(bytes[0] >> 2); | 184 | 0 | let byte1 = u16::from(bytes[1]); | 185 | | | 186 | | // Value is invalid if highest byte is 0. | 187 | 0 | if byte1 == 0 { | 188 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 189 | 0 | bytes, | 190 | 0 | nom::error::ErrorKind::Satisfy, | 191 | 0 | ))); | 192 | 0 | } | 193 | | | 194 | 0 | let value = (byte1 << 6) | byte0; | 195 | 0 | Ok((&bytes[2..], <$num_ty>::from(value))) | 196 | | } | 197 | | 0b10 => { | 198 | 1 | if bytes.len() < 4 { | 199 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 200 | 0 | core::num::NonZero::<usize>::new(4 - bytes.len()).unwrap(), | 201 | 0 | ))); | 202 | 1 | } | 203 | | | 204 | | // The code below uses `checked_shl` because using plain `<<` sometimes panics | 205 | | // with "attempt to shift left with overflow", even though it is | 206 | | // mathematically impossible for this to happen. I strongly suspect a | 207 | | // miscompilation when using `<<` instead of `checked_sub`, but haven't managed | 208 | | // to isolate the problem in a reproducible case. | 209 | 1 | let byte0 = u32::from(bytes[0] >> 2); | 210 | 1 | let byte1 = u32::from(bytes[1]).checked_shl(6).unwrap(); | 211 | 1 | let byte2 = u32::from(bytes[2]).checked_shl(14).unwrap(); | 212 | 1 | let byte3 = u32::from(bytes[3]).checked_shl(22).unwrap(); | 213 | | | 214 | | // Value is invalid if value could have been encoded with 2 fewer bytes. | 215 | 1 | if byte2 == 0 && byte3 == 00 { | 216 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 217 | 0 | bytes, | 218 | 0 | nom::error::ErrorKind::Satisfy, | 219 | 0 | ))); | 220 | 1 | } | 221 | | | 222 | 1 | let value = byte3 | byte2 | byte1 | byte0; | 223 | 1 | let value = match <$num_ty>::try_from(value) { | 224 | 1 | Ok(v) => v, | 225 | | Err(_) => { | 226 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 227 | 0 | bytes, | 228 | 0 | nom::error::ErrorKind::Satisfy, | 229 | 0 | ))); | 230 | | } | 231 | | }; | 232 | 1 | Ok((&bytes[4..], value)) | 233 | | } | 234 | | 0b11 => { | 235 | 0 | let num_bytes = usize::from(bytes[0] >> 2) + 4; | 236 | | | 237 | 0 | if bytes.len() < num_bytes + 1 { | 238 | 0 | return Err(nom::Err::Incomplete(nom::Needed::Size( | 239 | 0 | core::num::NonZero::<usize>::new(num_bytes + 1 - bytes.len()).unwrap(), | 240 | 0 | ))); | 241 | 0 | } | 242 | | | 243 | | // Value is invalid if highest byte is 0. | 244 | 0 | if bytes[num_bytes] == 0 { | 245 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 246 | 0 | bytes, | 247 | 0 | nom::error::ErrorKind::Satisfy, | 248 | 0 | ))); | 249 | 0 | } | 250 | | | 251 | 0 | let mut out_value = 0; | 252 | 0 | let mut shift = 0u32; | 253 | 0 | for byte_index in 1..=num_bytes { | 254 | 0 | out_value |= match <$num_ty>::from(1u8) | 255 | 0 | .checked_shl(shift) | 256 | 0 | .and_then(|shl| <$num_ty>::from(bytes[byte_index]).checked_mul(shl)) | 257 | | { | 258 | 0 | Some(v) => v, | 259 | | None => { | 260 | | // Overflow. The SCALE-encoded value is too large to fit a `usize`. | 261 | 0 | return Err(nom::Err::Error(nom::error::make_error( | 262 | 0 | bytes, | 263 | 0 | nom::error::ErrorKind::Satisfy, | 264 | 0 | ))); | 265 | | } | 266 | | }; | 267 | | | 268 | | // Overflows aren't properly handled because `out_value` is expected to | 269 | | // overflow way sooner than `shift`. | 270 | 0 | shift += 8; | 271 | | } | 272 | | | 273 | 0 | Ok((&bytes[num_bytes + 1..], out_value)) | 274 | | } | 275 | 0 | _ => unreachable!(), | 276 | | } | 277 | 1.44k | } |
Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeTRShNtNtCsfjEU5fc64iO_3nom5error9ErrorKindEEB4_ Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEECs7snhGEhbuap_18smoldot_light_wasm Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEECsjyNE3yDMkgA_14json_rpc_basic Unexecuted instantiation: _RINvNtCsc1ywvx6YAnK_7smoldot4util23nom_scale_compact_usizeINtNtCsfjEU5fc64iO_3nom5error5ErrorRShEECs4VrkfB1pvQ3_25json_rpc_general_requests |
278 | | }; |
279 | | } |
280 | | |
281 | | decode_scale_compact!(nom_scale_compact_usize, usize); |
282 | | decode_scale_compact!(nom_scale_compact_u64, u64); |
283 | | |
284 | | macro_rules! encode_scale_compact { |
285 | | ($fn_name:ident, $num_ty:ty) => { |
286 | | /// Returns a buffer containing the SCALE-compact encoding of the parameter. |
287 | 1.59M | pub(crate) fn $fn_name(mut value: $num_ty) -> impl AsRef<[u8]> + Clone + use<> { |
288 | | const MAX_BITS: usize = 1 + (<$num_ty>::BITS as usize) / 8; |
289 | 1.59M | let mut array = arrayvec::ArrayVec::<u8, MAX_BITS>::new(); |
290 | | |
291 | 1.59M | if value < 64 { |
292 | 1.58M | array.push(u8::try_from(value).unwrap() << 2); |
293 | 1.58M | } else if value < (1 << 14)8.96k { |
294 | 8.91k | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b01); |
295 | 8.91k | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); |
296 | 8.91k | } else if value < (1 << 30)55 { |
297 | 55 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b10); |
298 | 55 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); |
299 | 55 | array.push(u8::try_from((value >> 14) & 0xff).unwrap()); |
300 | 55 | array.push(u8::try_from((value >> 22) & 0xff).unwrap()); |
301 | 55 | } else { |
302 | 0 | array.push(0); |
303 | 0 | while value != 0 { |
304 | 0 | array.push(u8::try_from(value & 0xff).unwrap()); |
305 | 0 | value >>= 8; |
306 | 0 | } |
307 | 0 | array[0] = (u8::try_from(array.len() - 1 - 4).unwrap() << 2) | 0b11; |
308 | | } |
309 | | |
310 | 1.59M | array |
311 | 1.59M | } _RNvNtCsjlkOsLH0Zfj_7smoldot4util24encode_scale_compact_u64 Line | Count | Source | 287 | 1.04k | pub(crate) fn $fn_name(mut value: $num_ty) -> impl AsRef<[u8]> + Clone + use<> { | 288 | | const MAX_BITS: usize = 1 + (<$num_ty>::BITS as usize) / 8; | 289 | 1.04k | let mut array = arrayvec::ArrayVec::<u8, MAX_BITS>::new(); | 290 | | | 291 | 1.04k | if value < 64 { | 292 | 1.04k | array.push(u8::try_from(value).unwrap() << 2); | 293 | 1.04k | } else if value < (1 << 14)1 { | 294 | 0 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b01); | 295 | 0 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 296 | 1 | } else if value < (1 << 30) { | 297 | 1 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b10); | 298 | 1 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 299 | 1 | array.push(u8::try_from((value >> 14) & 0xff).unwrap()); | 300 | 1 | array.push(u8::try_from((value >> 22) & 0xff).unwrap()); | 301 | 1 | } else { | 302 | 0 | array.push(0); | 303 | 0 | while value != 0 { | 304 | 0 | array.push(u8::try_from(value & 0xff).unwrap()); | 305 | 0 | value >>= 8; | 306 | 0 | } | 307 | 0 | array[0] = (u8::try_from(array.len() - 1 - 4).unwrap() << 2) | 0b11; | 308 | | } | 309 | | | 310 | 1.04k | array | 311 | 1.04k | } |
_RNvNtCsjlkOsLH0Zfj_7smoldot4util26encode_scale_compact_usize Line | Count | Source | 287 | 1.58M | pub(crate) fn $fn_name(mut value: $num_ty) -> impl AsRef<[u8]> + Clone + use<> { | 288 | | const MAX_BITS: usize = 1 + (<$num_ty>::BITS as usize) / 8; | 289 | 1.58M | let mut array = arrayvec::ArrayVec::<u8, MAX_BITS>::new(); | 290 | | | 291 | 1.58M | if value < 64 { | 292 | 1.57M | array.push(u8::try_from(value).unwrap() << 2); | 293 | 1.57M | } else if value < (1 << 14)8.25k { | 294 | 8.23k | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b01); | 295 | 8.23k | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 296 | 8.23k | } else if value < (1 << 30)12 { | 297 | 12 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b10); | 298 | 12 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 299 | 12 | array.push(u8::try_from((value >> 14) & 0xff).unwrap()); | 300 | 12 | array.push(u8::try_from((value >> 22) & 0xff).unwrap()); | 301 | 12 | } else { | 302 | 0 | array.push(0); | 303 | 0 | while value != 0 { | 304 | 0 | array.push(u8::try_from(value & 0xff).unwrap()); | 305 | 0 | value >>= 8; | 306 | 0 | } | 307 | 0 | array[0] = (u8::try_from(array.len() - 1 - 4).unwrap() << 2) | 0b11; | 308 | | } | 309 | | | 310 | 1.58M | array | 311 | 1.58M | } |
_RNvNtCsc1ywvx6YAnK_7smoldot4util24encode_scale_compact_u64 Line | Count | Source | 287 | 147 | pub(crate) fn $fn_name(mut value: $num_ty) -> impl AsRef<[u8]> + Clone + use<> { | 288 | | const MAX_BITS: usize = 1 + (<$num_ty>::BITS as usize) / 8; | 289 | 147 | let mut array = arrayvec::ArrayVec::<u8, MAX_BITS>::new(); | 290 | | | 291 | 147 | if value < 64 { | 292 | 147 | array.push(u8::try_from(value).unwrap() << 2); | 293 | 147 | } else if value < (1 << 14)0 { | 294 | 0 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b01); | 295 | 0 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 296 | 0 | } else if value < (1 << 30) { | 297 | 0 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b10); | 298 | 0 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 299 | 0 | array.push(u8::try_from((value >> 14) & 0xff).unwrap()); | 300 | 0 | array.push(u8::try_from((value >> 22) & 0xff).unwrap()); | 301 | 0 | } else { | 302 | 0 | array.push(0); | 303 | 0 | while value != 0 { | 304 | 0 | array.push(u8::try_from(value & 0xff).unwrap()); | 305 | 0 | value >>= 8; | 306 | 0 | } | 307 | 0 | array[0] = (u8::try_from(array.len() - 1 - 4).unwrap() << 2) | 0b11; | 308 | | } | 309 | | | 310 | 147 | array | 311 | 147 | } |
_RNvNtCsc1ywvx6YAnK_7smoldot4util26encode_scale_compact_usize Line | Count | Source | 287 | 3.93k | pub(crate) fn $fn_name(mut value: $num_ty) -> impl AsRef<[u8]> + Clone + use<> { | 288 | | const MAX_BITS: usize = 1 + (<$num_ty>::BITS as usize) / 8; | 289 | 3.93k | let mut array = arrayvec::ArrayVec::<u8, MAX_BITS>::new(); | 290 | | | 291 | 3.93k | if value < 64 { | 292 | 3.21k | array.push(u8::try_from(value).unwrap() << 2); | 293 | 3.21k | } else if value < (1 << 14)714 { | 294 | 672 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b01); | 295 | 672 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 296 | 672 | } else if value < (1 << 30)42 { | 297 | 42 | array.push((u8::try_from(value & 0b111111).unwrap() << 2) | 0b10); | 298 | 42 | array.push(u8::try_from((value >> 6) & 0xff).unwrap()); | 299 | 42 | array.push(u8::try_from((value >> 14) & 0xff).unwrap()); | 300 | 42 | array.push(u8::try_from((value >> 22) & 0xff).unwrap()); | 301 | 42 | } else { | 302 | 0 | array.push(0); | 303 | 0 | while value != 0 { | 304 | 0 | array.push(u8::try_from(value & 0xff).unwrap()); | 305 | 0 | value >>= 8; | 306 | 0 | } | 307 | 0 | array[0] = (u8::try_from(array.len() - 1 - 4).unwrap() << 2) | 0b11; | 308 | | } | 309 | | | 310 | 3.93k | array | 311 | 3.93k | } |
|
312 | | }; |
313 | | } |
314 | | |
315 | | encode_scale_compact!(encode_scale_compact_u64, u64); |
316 | | encode_scale_compact!(encode_scale_compact_usize, usize); |