@@ -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 )
@@ -277,12 +266,12 @@ impl Env {
277
266
///
278
267
/// *WARNING:* This is not intended for general use, but only for inspecting an `Env` e.g.
279
268
/// for debugging, theme editing, and theme loading.
280
- pub fn get_all ( & self ) -> impl ExactSizeIterator < Item = ( & String , & Value ) > {
269
+ pub fn get_all ( & self ) -> impl ExactSizeIterator < Item = ( & ArcStr , & Value ) > {
281
270
self . 0 . map . iter ( )
282
271
}
283
272
284
273
/// Adds a key/value, acting like a builder.
285
- pub fn adding < ' a , V : ValueType < ' a > > ( mut self , key : Key < V > , value : impl Into < V :: Owned > ) -> Env {
274
+ pub fn adding < V : ValueType > ( mut self , key : Key < V > , value : impl Into < V > ) -> Env {
286
275
let env = Arc :: make_mut ( & mut self . 0 ) ;
287
276
env. map . insert ( key. into ( ) , value. into ( ) . into ( ) ) ;
288
277
self
@@ -294,7 +283,7 @@ impl Env {
294
283
///
295
284
/// Panics if the environment already has a value for the key, but it is
296
285
/// of a different type.
297
- pub fn set < ' a , V : ValueType < ' a > > ( & ' a mut self , key : Key < V > , value : impl Into < V :: Owned > ) {
286
+ pub fn set < V : ValueType > ( & mut self , key : Key < V > , value : impl Into < V > ) {
298
287
let env = Arc :: make_mut ( & mut self . 0 ) ;
299
288
let value = value. into ( ) . into ( ) ;
300
289
let key = key. into ( ) ;
@@ -369,7 +358,7 @@ impl Value {
369
358
/// # Panics
370
359
///
371
360
/// Panics when the value variant doesn't match the provided type.
372
- pub fn to_inner_unchecked < ' a , V : ValueType < ' a > > ( & ' a self ) -> V {
361
+ pub fn to_inner_unchecked < V : ValueType > ( & self ) -> V {
373
362
match ValueType :: try_from_value ( self ) {
374
363
Ok ( v) => v,
375
364
Err ( s) => panic ! ( "{}" , s) ,
@@ -420,7 +409,7 @@ impl Data for Value {
420
409
( Float ( f1) , Float ( f2) ) => f1. same ( & f2) ,
421
410
( Bool ( b1) , Bool ( b2) ) => b1 == b2,
422
411
( UnsignedInt ( f1) , UnsignedInt ( f2) ) => f1. same ( & f2) ,
423
- ( String ( s1) , String ( s2) ) => s1 == s2 ,
412
+ ( String ( s1) , String ( s2) ) => s1. same ( s2 ) ,
424
413
_ => false ,
425
414
}
426
415
}
@@ -482,9 +471,9 @@ impl Default for Env {
482
471
}
483
472
}
484
473
485
- impl < T > From < Key < T > > for String {
486
- fn from ( src : Key < T > ) -> String {
487
- String :: from ( src. key )
474
+ impl < T > From < Key < T > > for ArcStr {
475
+ fn from ( src : Key < T > ) -> ArcStr {
476
+ ArcStr :: from ( src. key )
488
477
}
489
478
}
490
479
@@ -520,10 +509,9 @@ impl std::error::Error for ValueTypeError {}
520
509
impl std:: error:: Error for MissingKeyError { }
521
510
522
511
/// Use this macro for types which are cheap to clone (ie all `Copy` types).
523
- macro_rules! impl_value_type_owned {
512
+ macro_rules! impl_value_type {
524
513
( $ty: ty, $var: ident) => {
525
- impl <' a> ValueType <' a> for $ty {
526
- type Owned = $ty;
514
+ impl ValueType for $ty {
527
515
fn try_from_value( value: & Value ) -> Result <Self , ValueTypeError > {
528
516
match value {
529
517
Value :: $var( f) => Ok ( f. to_owned( ) ) ,
@@ -540,57 +528,35 @@ macro_rules! impl_value_type_owned {
540
528
} ;
541
529
}
542
530
543
- /// Use this macro for types which require allocation but are not too
544
- /// expensive to clone.
545
- macro_rules! impl_value_type_borrowed {
546
- ( $ty: ty, $owned: ty, $var: ident) => {
547
- impl <' a> ValueType <' a> for & ' a $ty {
548
- type Owned = $owned;
549
- fn try_from_value( value: & ' a Value ) -> Result <Self , ValueTypeError > {
550
- match value {
551
- Value :: $var( f) => Ok ( f) ,
552
- other => Err ( ValueTypeError :: new( any:: type_name:: <$ty>( ) , other. clone( ) ) ) ,
553
- }
554
- }
555
- }
556
-
557
- impl Into <Value > for $owned {
558
- fn into( self ) -> Value {
559
- Value :: $var( self )
560
- }
561
- }
562
- } ;
563
- }
564
-
565
- impl_value_type_owned ! ( f64 , Float ) ;
566
- impl_value_type_owned ! ( bool , Bool ) ;
567
- impl_value_type_owned ! ( u64 , UnsignedInt ) ;
568
- impl_value_type_owned ! ( Color , Color ) ;
569
- impl_value_type_owned ! ( Rect , Rect ) ;
570
- impl_value_type_owned ! ( Point , Point ) ;
571
- impl_value_type_owned ! ( Size , Size ) ;
572
- impl_value_type_borrowed ! ( str , String , String ) ;
531
+ impl_value_type ! ( f64 , Float ) ;
532
+ impl_value_type ! ( bool , Bool ) ;
533
+ impl_value_type ! ( u64 , UnsignedInt ) ;
534
+ impl_value_type ! ( Color , Color ) ;
535
+ impl_value_type ! ( Rect , Rect ) ;
536
+ impl_value_type ! ( Point , Point ) ;
537
+ impl_value_type ! ( Size , Size ) ;
538
+ impl_value_type ! ( ArcStr , String ) ;
573
539
574
- impl < ' a , T : ValueType < ' a > > KeyOrValue < T > {
540
+ impl < T : ValueType > KeyOrValue < T > {
575
541
/// Resolve the concrete type `T` from this `KeyOrValue`, using the provided
576
542
/// [`Env`] if required.
577
543
///
578
544
/// [`Env`]: struct.Env.html
579
- pub fn resolve ( & ' a self , env : & ' a Env ) -> T {
545
+ pub fn resolve ( & self , env : & Env ) -> T {
580
546
match self {
581
547
KeyOrValue :: Concrete ( value) => value. to_inner_unchecked ( ) ,
582
548
KeyOrValue :: Key ( key) => env. get ( key) ,
583
549
}
584
550
}
585
551
}
586
552
587
- impl < ' a , V : Into < Value > , T : ValueType < ' a , Owned = V > > From < V > for KeyOrValue < T > {
588
- fn from ( value : V ) -> KeyOrValue < T > {
553
+ impl < T : Into < Value > > From < T > for KeyOrValue < T > {
554
+ fn from ( value : T ) -> KeyOrValue < T > {
589
555
KeyOrValue :: Concrete ( value. into ( ) )
590
556
}
591
557
}
592
558
593
- impl < ' a , T : ValueType < ' a > > From < Key < T > > for KeyOrValue < T > {
559
+ impl < T : ValueType > From < Key < T > > for KeyOrValue < T > {
594
560
fn from ( key : Key < T > ) -> KeyOrValue < T > {
595
561
KeyOrValue :: Key ( key)
596
562
}
@@ -602,12 +568,12 @@ mod tests {
602
568
603
569
#[ test]
604
570
fn string_key_or_value ( ) {
605
- const MY_KEY : Key < & str > = Key :: new ( "test.my-string-key" ) ;
606
- let env = Env :: default ( ) . adding ( MY_KEY , "Owned" . to_string ( ) ) ;
607
- assert_eq ! ( env. get( MY_KEY ) , "Owned" ) ;
571
+ const MY_KEY : Key < ArcStr > = Key :: new ( "test.my-string-key" ) ;
572
+ let env = Env :: default ( ) . adding ( MY_KEY , "Owned" ) ;
573
+ assert_eq ! ( env. get( MY_KEY ) . as_ref ( ) , "Owned" ) ;
608
574
609
- let key: KeyOrValue < & str > = MY_KEY . into ( ) ;
610
- let value: KeyOrValue < & str > = "Owned" . to_string ( ) . into ( ) ;
575
+ let key: KeyOrValue < ArcStr > = MY_KEY . into ( ) ;
576
+ let value: KeyOrValue < ArcStr > = ArcStr :: from ( "Owned" ) . into ( ) ;
611
577
612
578
assert_eq ! ( key. resolve( & env) , value. resolve( & env) ) ;
613
579
}
0 commit comments