1- use std:: borrow:: Cow ;
21use std:: iter:: FromIterator ;
32
43use crate :: types:: FluentValue ;
@@ -53,7 +52,7 @@ use crate::types::FluentValue;
5352/// );
5453/// ```
5554#[ derive( Debug , Default ) ]
56- pub struct FluentArgs < ' args > ( Vec < ( Cow < ' args , str > , FluentValue < ' args > ) > ) ;
55+ pub struct FluentArgs < ' args > ( Vec < ( & ' args str , FluentValue < ' args > ) > ) ;
5756
5857impl < ' args > FluentArgs < ' args > {
5958 /// Creates a new empty argument map.
@@ -67,45 +66,42 @@ impl<'args> FluentArgs<'args> {
6766 }
6867
6968 /// 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) {
7671 Some ( & self . 0 [ idx] . 1 )
7772 } else {
7873 None
7974 }
8075 }
8176
8277 /// 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 )
8479 where
85- K : Into < Cow < ' args , str > > ,
8680 V : Into < FluentValue < ' args > > ,
8781 {
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 > ) {
8986 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) ) ,
9289 } ;
9390 }
9491
9592 /// 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) )
9895 }
9996}
10097
101- impl < ' args , K , V > FromIterator < ( K , V ) > for FluentArgs < ' args >
98+ impl < ' args , V > FromIterator < ( & ' args str , V ) > for FluentArgs < ' args >
10299where
103- K : Into < Cow < ' args , str > > ,
104100 V : Into < FluentValue < ' args > > ,
105101{
106102 fn from_iter < I > ( iter : I ) -> Self
107103 where
108- I : IntoIterator < Item = ( K , V ) > ,
104+ I : IntoIterator < Item = ( & ' args str , V ) > ,
109105 {
110106 let iter = iter. into_iter ( ) ;
111107 let mut args = if let Some ( size) = iter. size_hint ( ) . 1 {
@@ -123,7 +119,7 @@ where
123119}
124120
125121impl < ' args > IntoIterator for FluentArgs < ' args > {
126- type Item = ( Cow < ' args , str > , FluentValue < ' args > ) ;
122+ type Item = ( & ' args str , FluentValue < ' args > ) ;
127123 type IntoIter = std:: vec:: IntoIter < Self :: Item > ;
128124
129125 fn into_iter ( self ) -> Self :: IntoIter {
@@ -133,6 +129,8 @@ impl<'args> IntoIterator for FluentArgs<'args> {
133129
134130#[ cfg( test) ]
135131mod tests {
132+ use std:: borrow:: Cow ;
133+
136134 use super :: * ;
137135
138136 #[ test]
0 commit comments