/__w/smoldot/smoldot/repo/light-base/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 | | use core::fmt::{self, Write as _}; |
19 | | |
20 | | /// Returns an opaque object implementing the `fmt::Display` trait. Truncates the given `char` |
21 | | /// yielding iterator to the given number of elements, and if the limit is reached adds a `…` at |
22 | | /// the end. |
23 | 0 | pub fn truncated_str<'a>( |
24 | 0 | input: impl Iterator<Item = char> + Clone + 'a, |
25 | 0 | limit: usize, |
26 | 0 | ) -> impl fmt::Display + 'a { |
27 | 0 | struct Iter<I>(I, usize); |
28 | 0 |
|
29 | 0 | impl<I: Iterator<Item = char> + Clone> fmt::Display for Iter<I> { |
30 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
31 | 0 | let mut counter = 0; |
32 | 0 | for c in self.0.clone() { |
33 | 0 | f.write_char(c)?; |
34 | 0 |
|
35 | 0 | counter += 1; |
36 | 0 | if counter >= self.1 { |
37 | 0 | f.write_char('…')?; |
38 | 0 | break; |
39 | 0 | } |
40 | 0 | } |
41 | 0 |
|
42 | 0 | Ok(()) |
43 | 0 | } Unexecuted instantiation: _RNvXININvNtCsiGub1lfKphe_13smoldot_light4util13truncated_str0pEINtB5_4IterpENtNtCsaYZPK01V26L_4core3fmt7Display3fmtB9_ Unexecuted instantiation: _RNvXNvNtCsih6EgvAwZF2_13smoldot_light4util13truncated_strINtB2_4IterINtNtNtNtCsaYZPK01V26L_4core4iter8adapters6filter6FilterNtNtNtB1d_3str4iter5CharsNCNCNvMNtB6_16json_rpc_serviceINtB2u_8FrontendNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE22next_json_rpc_response00EENtNtB1d_3fmt7Display3fmtB3b_ Unexecuted instantiation: _RNvXNvNtCsih6EgvAwZF2_13smoldot_light4util13truncated_strINtB2_4IterINtNtNtNtCsaYZPK01V26L_4core4iter8adapters6filter6FilterNtNtNtB1d_3str4iter5CharsNCNvMNtB6_16json_rpc_serviceINtB2s_8FrontendNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE17queue_rpc_request0EENtNtB1d_3fmt7Display3fmtB39_ Unexecuted instantiation: _RNvXININvNtCsih6EgvAwZF2_13smoldot_light4util13truncated_str0pEINtB5_4IterpENtNtCsaYZPK01V26L_4core3fmt7Display3fmtB9_ |
44 | 0 | } |
45 | 0 |
|
46 | 0 | Iter(input, limit) |
47 | 0 | } Unexecuted instantiation: _RINvNtCsiGub1lfKphe_13smoldot_light4util13truncated_strpEB4_ Unexecuted instantiation: _RINvNtCsih6EgvAwZF2_13smoldot_light4util13truncated_strINtNtNtNtCsaYZPK01V26L_4core4iter8adapters6filter6FilterNtNtNtB10_3str4iter5CharsNCNCNvMNtB4_16json_rpc_serviceINtB2h_8FrontendNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE22next_json_rpc_response00EEB2Y_ Unexecuted instantiation: _RINvNtCsih6EgvAwZF2_13smoldot_light4util13truncated_strINtNtNtNtCsaYZPK01V26L_4core4iter8adapters6filter6FilterNtNtNtB10_3str4iter5CharsNCNvMNtB4_16json_rpc_serviceINtB2f_8FrontendNtNtCsDDUKWWCHAU_18smoldot_light_wasm8platform11PlatformRefE17queue_rpc_request0EEB2W_ Unexecuted instantiation: _RINvNtCsih6EgvAwZF2_13smoldot_light4util13truncated_strpEB4_ |
48 | | |
49 | | /// Implementation of the `BuildHasher` trait for the sip hasher. |
50 | | /// |
51 | | /// Contrary to the one in the standard library, a seed is explicitly passed here, making the |
52 | | /// hashing predictable. This is a good thing for tests and no-std compatibility. |
53 | | pub struct SipHasherBuild([u8; 16]); |
54 | | |
55 | | impl SipHasherBuild { |
56 | 0 | pub fn new(seed: [u8; 16]) -> SipHasherBuild { |
57 | 0 | SipHasherBuild(seed) |
58 | 0 | } Unexecuted instantiation: _RNvMNtCsiGub1lfKphe_13smoldot_light4utilNtB2_14SipHasherBuild3new Unexecuted instantiation: _RNvMNtCsih6EgvAwZF2_13smoldot_light4utilNtB2_14SipHasherBuild3new |
59 | | } |
60 | | |
61 | | impl core::hash::BuildHasher for SipHasherBuild { |
62 | | type Hasher = siphasher::sip::SipHasher13; |
63 | | |
64 | 0 | fn build_hasher(&self) -> Self::Hasher { |
65 | 0 | siphasher::sip::SipHasher13::new_with_key(&self.0) |
66 | 0 | } Unexecuted instantiation: _RNvXs_NtCsiGub1lfKphe_13smoldot_light4utilNtB4_14SipHasherBuildNtNtCsaYZPK01V26L_4core4hash11BuildHasher12build_hasher Unexecuted instantiation: _RNvXs_NtCsih6EgvAwZF2_13smoldot_light4utilNtB4_14SipHasherBuildNtNtCsaYZPK01V26L_4core4hash11BuildHasher12build_hasher |
67 | | } |