/__w/smoldot/smoldot/repo/light-base/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 | | 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(input: impl Iterator<Item = char> + Clone, limit: usize) -> impl fmt::Display { |
24 | | struct Iter<I>(I, usize); |
25 | | |
26 | | impl<I: Iterator<Item = char> + Clone> fmt::Display for Iter<I> { |
27 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
28 | 0 | let mut counter = 0; |
29 | 0 | for c in self.0.clone() { |
30 | 0 | f.write_char(c)?; |
31 | | |
32 | 0 | counter += 1; |
33 | 0 | if counter >= self.1 { |
34 | 0 | f.write_char('…')?; |
35 | 0 | break; |
36 | 0 | } |
37 | | } |
38 | | |
39 | 0 | Ok(()) |
40 | 0 | } Unexecuted instantiation: _RNvXININvNtCsgnEOxJmACC4_13smoldot_light4util13truncated_str0pEINtB5_4IterpENtNtCs1p5UDGgVI4d_4core3fmt7Display3fmtB9_ Unexecuted instantiation: _RNvXNvNtCs508CmrSPkZh_13smoldot_light4util13truncated_strINtB2_4IterINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6filter6FilterNtNtNtB1d_3str4iter5CharsNCNCNvMNtB6_16json_rpc_serviceINtB2u_8FrontendNtNtCs7snhGEhbuap_18smoldot_light_wasm8platform11PlatformRefE22next_json_rpc_response00EENtNtB1d_3fmt7Display3fmtB3b_ Unexecuted instantiation: _RNvXNvNtCs508CmrSPkZh_13smoldot_light4util13truncated_strINtB2_4IterINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6filter6FilterNtNtNtB1d_3str4iter5CharsNCNvMNtB6_16json_rpc_serviceINtB2s_8FrontendNtNtCs7snhGEhbuap_18smoldot_light_wasm8platform11PlatformRefE17queue_rpc_request0EENtNtB1d_3fmt7Display3fmtB39_ Unexecuted instantiation: _RNvXININvNtCs508CmrSPkZh_13smoldot_light4util13truncated_str0pEINtB5_4IterpENtNtCs1p5UDGgVI4d_4core3fmt7Display3fmtB9_ |
41 | | } |
42 | | |
43 | 0 | Iter(input, limit) |
44 | 0 | } Unexecuted instantiation: _RINvNtCsgnEOxJmACC4_13smoldot_light4util13truncated_strpEB4_ Unexecuted instantiation: _RINvNtCs508CmrSPkZh_13smoldot_light4util13truncated_strINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6filter6FilterNtNtNtB10_3str4iter5CharsNCNCNvMNtB4_16json_rpc_serviceINtB2h_8FrontendNtNtCs7snhGEhbuap_18smoldot_light_wasm8platform11PlatformRefE22next_json_rpc_response00EEB2Y_ Unexecuted instantiation: _RINvNtCs508CmrSPkZh_13smoldot_light4util13truncated_strINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6filter6FilterNtNtNtB10_3str4iter5CharsNCNvMNtB4_16json_rpc_serviceINtB2f_8FrontendNtNtCs7snhGEhbuap_18smoldot_light_wasm8platform11PlatformRefE17queue_rpc_request0EEB2W_ Unexecuted instantiation: _RINvNtCs508CmrSPkZh_13smoldot_light4util13truncated_strpEB4_ |
45 | | |
46 | | /// Implementation of the `BuildHasher` trait for the sip hasher. |
47 | | /// |
48 | | /// Contrary to the one in the standard library, a seed is explicitly passed here, making the |
49 | | /// hashing predictable. This is a good thing for tests and no-std compatibility. |
50 | | pub struct SipHasherBuild([u8; 16]); |
51 | | |
52 | | impl SipHasherBuild { |
53 | 0 | pub fn new(seed: [u8; 16]) -> SipHasherBuild { |
54 | 0 | SipHasherBuild(seed) |
55 | 0 | } Unexecuted instantiation: _RNvMNtCsgnEOxJmACC4_13smoldot_light4utilNtB2_14SipHasherBuild3new Unexecuted instantiation: _RNvMNtCs508CmrSPkZh_13smoldot_light4utilNtB2_14SipHasherBuild3new |
56 | | } |
57 | | |
58 | | impl core::hash::BuildHasher for SipHasherBuild { |
59 | | type Hasher = siphasher::sip::SipHasher13; |
60 | | |
61 | 0 | fn build_hasher(&self) -> Self::Hasher { |
62 | 0 | siphasher::sip::SipHasher13::new_with_key(&self.0) |
63 | 0 | } Unexecuted instantiation: _RNvXs_NtCsgnEOxJmACC4_13smoldot_light4utilNtB4_14SipHasherBuildNtNtCs1p5UDGgVI4d_4core4hash11BuildHasher12build_hasher Unexecuted instantiation: _RNvXs_NtCs508CmrSPkZh_13smoldot_light4utilNtB4_14SipHasherBuildNtNtCs1p5UDGgVI4d_4core4hash11BuildHasher12build_hasher |
64 | | } |