@@ -23,7 +23,7 @@ use std::ops::Deref;
23
23
use std:: sync:: Arc ;
24
24
25
25
use crate :: localization:: L10nManager ;
26
- use crate :: { Color , Data , Point , Rect , Size } ;
26
+ use crate :: { ArcStr , Color , Data , Point , Rect , Size } ;
27
27
28
28
/// An environment passed down through all widget traversals.
29
29
///
@@ -52,7 +52,7 @@ pub struct Env(Arc<EnvImpl>);
52
52
53
53
#[ derive( Clone ) ]
54
54
struct EnvImpl {
55
- map : HashMap < String , Value > ,
55
+ map : HashMap < ArcStr , Value > ,
56
56
debug_colors : Vec < Color > ,
57
57
l10n : Arc < L10nManager > ,
58
58
}
@@ -107,7 +107,7 @@ pub enum Value {
107
107
Float ( f64 ) ,
108
108
Bool ( bool ) ,
109
109
UnsignedInt ( u64 ) ,
110
- String ( String ) ,
110
+ String ( ArcStr ) ,
111
111
}
112
112
// ANCHOR_END: value_type
113
113
@@ -132,17 +132,9 @@ pub enum KeyOrValue<T> {
132
132
}
133
133
134
134
/// Values which can be stored in an environment.
135
- ///
136
- /// Note that for "expensive" types this is the reference. For example,
137
- /// for strings, this trait is implemented on `&'a str`. The trait is
138
- /// parametrized on a lifetime so that it can be used for references in
139
- /// this way.
140
- pub trait ValueType < ' a > : Sized {
141
- /// The corresponding owned type.
142
- type Owned : Into < Value > ;
143
-
135
+ pub trait ValueType : Sized + Into < Value > {
144
136
/// Attempt to convert the generic `Value` into this type.
145
- fn try_from_value ( v : & ' a Value ) -> Result < Self , ValueTypeError > ;
137
+ fn try_from_value ( v : & Value ) -> Result < Self , ValueTypeError > ;
146
138
}
147
139
148
140
/// The error type for environment access.
@@ -214,7 +206,7 @@ impl Env {
214
206
/// # Panics
215
207
///
216
208
/// Panics if the key is not found, or if it is present with the wrong type.
217
- pub fn get < ' a , V : ValueType < ' a > > ( & ' a self , key : impl Borrow < Key < V > > ) -> V {
209
+ pub fn get < V : ValueType > ( & self , key : impl Borrow < Key < V > > ) -> V {
218
210
match self . try_get ( key) {
219
211
Ok ( value) => value,
220
212
Err ( err) => panic ! ( "{}" , err) ,
@@ -228,10 +220,7 @@ impl Env {
228
220
/// # Panics
229
221
///
230
222
/// Panics if the value for the key is found, but has the wrong type.
231
- pub fn try_get < ' a , V : ValueType < ' a > > (
232
- & ' a self ,
233
- key : impl Borrow < Key < V > > ,
234
- ) -> Result < V , MissingKeyError > {
223
+ pub fn try_get < V : ValueType > ( & self , key : impl Borrow < Key < V > > ) -> Result < V , MissingKeyError > {
235
224
self . 0
236
225
. map
237
226
. get ( key. borrow ( ) . key )
@@ -275,12 +264,12 @@ impl Env {
275
264
///
276
265
/// *WARNING:* This is not intended for general use, but only for inspecting an `Env` e.g.
277
266
/// for debugging, theme editing, and theme loading.
278
- pub fn get_all ( & self ) -> impl ExactSizeIterator < Item = ( & String , & Value ) > {
267
+ pub fn get_all ( & self ) -> impl ExactSizeIterator < Item = ( & ArcStr , & Value ) > {
279
268
self . 0 . map . iter ( )
280
269
}
281
270
282
271
/// Adds a key/value, acting like a builder.
283
- pub fn adding < ' a , V : ValueType < ' a > > ( mut self , key : Key < V > , value : impl Into < V :: Owned > ) -> Env {
272
+ pub fn adding < V : ValueType > ( mut self , key : Key < V > , value : impl Into < V > ) -> Env {
284
273
let env = Arc :: make_mut ( & mut self . 0 ) ;
285
274
env. map . insert ( key. into ( ) , value. into ( ) . into ( ) ) ;
286
275
self
@@ -292,7 +281,7 @@ impl Env {
292
281
///
293
282
/// Panics if the environment already has a value for the key, but it is
294
283
/// of a different type.
295
- pub fn set < ' a , V : ValueType < ' a > > ( & ' a mut self , key : Key < V > , value : impl Into < V :: Owned > ) {
284
+ pub fn set < V : ValueType > ( & mut self , key : Key < V > , value : impl Into < V > ) {
296
285
let env = Arc :: make_mut ( & mut self . 0 ) ;
297
286
let value = value. into ( ) . into ( ) ;
298
287
let key = key. into ( ) ;
@@ -367,7 +356,7 @@ impl Value {
367
356
/// # Panics
368
357
///
369
358
/// Panics when the value variant doesn't match the provided type.
370
- pub fn to_inner_unchecked < ' a , V : ValueType < ' a > > ( & ' a self ) -> V {
359
+ pub fn to_inner_unchecked < V : ValueType > ( & self ) -> V {
371
360
match ValueType :: try_from_value ( self ) {
372
361
Ok ( v) => v,
373
362
Err ( s) => panic ! ( "{}" , s) ,
@@ -418,7 +407,7 @@ impl Data for Value {
418
407
( Float ( f1) , Float ( f2) ) => f1. same ( & f2) ,
419
408
( Bool ( b1) , Bool ( b2) ) => b1 == b2,
420
409
( UnsignedInt ( f1) , UnsignedInt ( f2) ) => f1. same ( & f2) ,
421
- ( String ( s1) , String ( s2) ) => s1 == s2 ,
410
+ ( String ( s1) , String ( s2) ) => s1. same ( s2 ) ,
422
411
_ => false ,
423
412
}
424
413
}
@@ -480,9 +469,9 @@ impl Default for Env {
480
469
}
481
470
}
482
471
483
- impl < T > From < Key < T > > for String {
484
- fn from ( src : Key < T > ) -> String {
485
- String :: from ( src. key )
472
+ impl < T > From < Key < T > > for ArcStr {
473
+ fn from ( src : Key < T > ) -> ArcStr {
474
+ ArcStr :: from ( src. key )
486
475
}
487
476
}
488
477
@@ -518,10 +507,9 @@ impl std::error::Error for ValueTypeError {}
518
507
impl std:: error:: Error for MissingKeyError { }
519
508
520
509
/// Use this macro for types which are cheap to clone (ie all `Copy` types).
521
- macro_rules! impl_value_type_owned {
510
+ macro_rules! impl_value_type {
522
511
( $ty: ty, $var: ident) => {
523
- impl <' a> ValueType <' a> for $ty {
524
- type Owned = $ty;
512
+ impl ValueType for $ty {
525
513
fn try_from_value( value: & Value ) -> Result <Self , ValueTypeError > {
526
514
match value {
527
515
Value :: $var( f) => Ok ( f. to_owned( ) ) ,
@@ -538,57 +526,35 @@ macro_rules! impl_value_type_owned {
538
526
} ;
539
527
}
540
528
541
- /// Use this macro for types which require allocation but are not too
542
- /// expensive to clone.
543
- macro_rules! impl_value_type_borrowed {
544
- ( $ty: ty, $owned: ty, $var: ident) => {
545
- impl <' a> ValueType <' a> for & ' a $ty {
546
- type Owned = $owned;
547
- fn try_from_value( value: & ' a Value ) -> Result <Self , ValueTypeError > {
548
- match value {
549
- Value :: $var( f) => Ok ( f) ,
550
- other => Err ( ValueTypeError :: new( any:: type_name:: <$ty>( ) , other. clone( ) ) ) ,
551
- }
552
- }
553
- }
554
-
555
- impl Into <Value > for $owned {
556
- fn into( self ) -> Value {
557
- Value :: $var( self )
558
- }
559
- }
560
- } ;
561
- }
562
-
563
- impl_value_type_owned ! ( f64 , Float ) ;
564
- impl_value_type_owned ! ( bool , Bool ) ;
565
- impl_value_type_owned ! ( u64 , UnsignedInt ) ;
566
- impl_value_type_owned ! ( Color , Color ) ;
567
- impl_value_type_owned ! ( Rect , Rect ) ;
568
- impl_value_type_owned ! ( Point , Point ) ;
569
- impl_value_type_owned ! ( Size , Size ) ;
570
- impl_value_type_borrowed ! ( str , String , String ) ;
529
+ impl_value_type ! ( f64 , Float ) ;
530
+ impl_value_type ! ( bool , Bool ) ;
531
+ impl_value_type ! ( u64 , UnsignedInt ) ;
532
+ impl_value_type ! ( Color , Color ) ;
533
+ impl_value_type ! ( Rect , Rect ) ;
534
+ impl_value_type ! ( Point , Point ) ;
535
+ impl_value_type ! ( Size , Size ) ;
536
+ impl_value_type ! ( ArcStr , String ) ;
571
537
572
- impl < ' a , T : ValueType < ' a > > KeyOrValue < T > {
538
+ impl < T : ValueType > KeyOrValue < T > {
573
539
/// Resolve the concrete type `T` from this `KeyOrValue`, using the provided
574
540
/// [`Env`] if required.
575
541
///
576
542
/// [`Env`]: struct.Env.html
577
- pub fn resolve ( & ' a self , env : & ' a Env ) -> T {
543
+ pub fn resolve ( & self , env : & Env ) -> T {
578
544
match self {
579
545
KeyOrValue :: Concrete ( value) => value. to_inner_unchecked ( ) ,
580
546
KeyOrValue :: Key ( key) => env. get ( key) ,
581
547
}
582
548
}
583
549
}
584
550
585
- impl < ' a , V : Into < Value > , T : ValueType < ' a , Owned = V > > From < V > for KeyOrValue < T > {
586
- fn from ( value : V ) -> KeyOrValue < T > {
551
+ impl < T : Into < Value > > From < T > for KeyOrValue < T > {
552
+ fn from ( value : T ) -> KeyOrValue < T > {
587
553
KeyOrValue :: Concrete ( value. into ( ) )
588
554
}
589
555
}
590
556
591
- impl < ' a , T : ValueType < ' a > > From < Key < T > > for KeyOrValue < T > {
557
+ impl < T : ValueType > From < Key < T > > for KeyOrValue < T > {
592
558
fn from ( key : Key < T > ) -> KeyOrValue < T > {
593
559
KeyOrValue :: Key ( key)
594
560
}
@@ -600,12 +566,12 @@ mod tests {
600
566
601
567
#[ test]
602
568
fn string_key_or_value ( ) {
603
- const MY_KEY : Key < & str > = Key :: new ( "test.my-string-key" ) ;
604
- let env = Env :: default ( ) . adding ( MY_KEY , "Owned" . to_string ( ) ) ;
605
- assert_eq ! ( env. get( MY_KEY ) , "Owned" ) ;
569
+ const MY_KEY : Key < ArcStr > = Key :: new ( "test.my-string-key" ) ;
570
+ let env = Env :: default ( ) . adding ( MY_KEY , "Owned" ) ;
571
+ assert_eq ! ( env. get( MY_KEY ) . as_ref ( ) , "Owned" ) ;
606
572
607
- let key: KeyOrValue < & str > = MY_KEY . into ( ) ;
608
- let value: KeyOrValue < & str > = "Owned" . to_string ( ) . into ( ) ;
573
+ let key: KeyOrValue < ArcStr > = MY_KEY . into ( ) ;
574
+ let value: KeyOrValue < ArcStr > = ArcStr :: from ( "Owned" ) . into ( ) ;
609
575
610
576
assert_eq ! ( key. resolve( & env) , value. resolve( & env) ) ;
611
577
}
0 commit comments