1
- use std:: borrow:: Cow ;
2
1
use std:: iter:: FromIterator ;
3
2
4
3
use crate :: types:: FluentValue ;
@@ -53,7 +52,7 @@ use crate::types::FluentValue;
53
52
/// );
54
53
/// ```
55
54
#[ derive( Debug , Default ) ]
56
- pub struct FluentArgs < ' args > ( Vec < ( Cow < ' args , str > , FluentValue < ' args > ) > ) ;
55
+ pub struct FluentArgs < ' args > ( Vec < ( & ' args str , FluentValue < ' args > ) > ) ;
57
56
58
57
impl < ' args > FluentArgs < ' args > {
59
58
/// Creates a new empty argument map.
@@ -67,45 +66,42 @@ impl<'args> FluentArgs<'args> {
67
66
}
68
67
69
68
/// Gets the [`FluentValue`] at the `key` if it exists.
70
- pub fn get < K > ( & self , key : K ) -> Option < & FluentValue < ' args > >
71
- where
72
- K : Into < Cow < ' args , str > > ,
73
- {
74
- let key = key. into ( ) ;
75
- if let Ok ( idx) = self . 0 . binary_search_by_key ( & & key, |( k, _) | k) {
69
+ pub fn get < ' s > ( & ' s self , key : & str ) -> Option < & ' s FluentValue < ' args > > {
70
+ if let Ok ( idx) = self . 0 . binary_search_by_key ( & key, |( k, _) | k) {
76
71
Some ( & self . 0 [ idx] . 1 )
77
72
} else {
78
73
None
79
74
}
80
75
}
81
76
82
77
/// Sets the key value pair.
83
- pub fn set < K , V > ( & mut self , key : K , value : V )
78
+ pub fn set < V > ( & mut self , key : & ' args str , value : V )
84
79
where
85
- K : Into < Cow < ' args , str > > ,
86
80
V : Into < FluentValue < ' args > > ,
87
81
{
88
- let key = key. into ( ) ;
82
+ self . set_inner ( key, value. into ( ) ) ;
83
+ }
84
+
85
+ fn set_inner ( & mut self , key : & ' args str , value : FluentValue < ' args > ) {
89
86
match self . 0 . binary_search_by_key ( & & key, |( k, _) | k) {
90
- Ok ( idx) => self . 0 [ idx] = ( key, value. into ( ) ) ,
91
- Err ( idx) => self . 0 . insert ( idx, ( key, value. into ( ) ) ) ,
87
+ Ok ( idx) => self . 0 [ idx] = ( key, value) ,
88
+ Err ( idx) => self . 0 . insert ( idx, ( key, value) ) ,
92
89
} ;
93
90
}
94
91
95
92
/// Iterate over a tuple of the key an [`FluentValue`].
96
- pub fn iter ( & self ) -> impl Iterator < Item = ( & str , & FluentValue ) > {
97
- self . 0 . iter ( ) . map ( |( k, v) | ( k . as_ref ( ) , v) )
93
+ pub fn iter ( & self ) -> impl Iterator < Item = ( & ' args str , & FluentValue < ' args > ) > {
94
+ self . 0 . iter ( ) . map ( |( k, v) | ( * k , v) )
98
95
}
99
96
}
100
97
101
- impl < ' args , K , V > FromIterator < ( K , V ) > for FluentArgs < ' args >
98
+ impl < ' args , V > FromIterator < ( & ' args str , V ) > for FluentArgs < ' args >
102
99
where
103
- K : Into < Cow < ' args , str > > ,
104
100
V : Into < FluentValue < ' args > > ,
105
101
{
106
102
fn from_iter < I > ( iter : I ) -> Self
107
103
where
108
- I : IntoIterator < Item = ( K , V ) > ,
104
+ I : IntoIterator < Item = ( & ' args str , V ) > ,
109
105
{
110
106
let iter = iter. into_iter ( ) ;
111
107
let mut args = if let Some ( size) = iter. size_hint ( ) . 1 {
@@ -123,7 +119,7 @@ where
123
119
}
124
120
125
121
impl < ' args > IntoIterator for FluentArgs < ' args > {
126
- type Item = ( Cow < ' args , str > , FluentValue < ' args > ) ;
122
+ type Item = ( & ' args str , FluentValue < ' args > ) ;
127
123
type IntoIter = std:: vec:: IntoIter < Self :: Item > ;
128
124
129
125
fn into_iter ( self ) -> Self :: IntoIter {
@@ -133,6 +129,8 @@ impl<'args> IntoIterator for FluentArgs<'args> {
133
129
134
130
#[ cfg( test) ]
135
131
mod tests {
132
+ use std:: borrow:: Cow ;
133
+
136
134
use super :: * ;
137
135
138
136
#[ test]
0 commit comments