1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! Shared collection utils module.
//!
//! This module declares functions used by `translatable_proc`
//! and `translatable_shared` together, mostly used to convert
//! compile-time structures into runtime representations of
//! the same structures.
use HashMap;
use TokenStream as TokenStream2;
use ;
/// [`HashMap<K, V>`] runtime conversion.
///
/// This function converts a [`HashMap<K, V>`] into a [`TokenStream2`]
/// that when generated on a macro contains the same values as the initial
/// map.
///
/// The type of the keys and values of the map must implement [`ToTokens`].
///
/// **Parameters**
/// * `map` - The map to convert into tokens.
///
/// **Returns**
/// The provided `map` parameter represented as [`TokenStream2`].
/// [`HashMap<K, V>`] runtime conversion and mapping.
///
/// Similarly to [`map_to_tokens`] this function converts a [`HashMap<K, V>`]
/// into a [`TokenStream2`] that when generated on a macro contains the same
/// values as the original map. The difference is that in this function the keys
/// and values types don't need to implement [`ToTokens`], as this takes a
/// predicate which lets you modify values before converting it to tokens.
///
/// The predicate must return a [`TokenStream2`] containing tuples, the internal
/// conversion is as `vec![$($converted),*]` collected into a [`HashMap<K, V>`]
/// in runtime.
///
/// **Parameters**
/// * `map` - The map to convert into tokens.
/// * `predicate` - A predicate taking a key and a value that should return a
/// [`TokenStream2`]
/// containing a tuple of the key and the value transformed in any way.
///
/// **Returns**
/// The provided `map` parameter mutated with the `predicate` and converted to a
/// [`TokenStream2`].