11//! This module contains the implementation yew's virtual nodes' keys.
22
3+ use std:: fmt:: { self , Display , Formatter } ;
34use std:: ops:: Deref ;
45use std:: rc:: Rc ;
56
67/// Represents the (optional) key of Yew's virtual nodes.
78///
89/// Keys are cheap to clone.
9- // TODO (#1263): Explain when keys are useful and add an example.
10- #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
10+ #[ derive( Clone , Debug , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
1111pub struct Key {
12- key : Rc < String > ,
12+ key : Rc < str > ,
1313}
1414
15- impl core :: fmt :: Display for Key {
16- fn fmt ( & self , f : & mut core :: fmt :: Formatter < ' _ > ) -> core :: fmt:: Result {
17- write ! ( f , "{}" , & self . key)
15+ impl Display for Key {
16+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
17+ self . key . fmt ( f )
1818 }
1919}
2020
21- impl core:: fmt:: Debug for Key {
22- fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
23- write ! ( f, "{}" , & self . key)
21+ impl Deref for Key {
22+ type Target = str ;
23+
24+ fn deref ( & self ) -> & str {
25+ self . key . as_ref ( )
2426 }
2527}
2628
27- impl From < Rc < String > > for Key {
28- fn from ( key : Rc < String > ) -> Self {
29- Key { key }
29+ impl From < Rc < str > > for Key {
30+ fn from ( key : Rc < str > ) -> Self {
31+ Self { key }
3032 }
3133}
3234
33- impl Deref for Key {
34- type Target = str ;
35-
36- #[ inline]
37- fn deref ( & self ) -> & str {
38- self . key . as_ref ( )
35+ impl From < & ' _ str > for Key {
36+ fn from ( key : & ' _ str ) -> Self {
37+ let key: Rc < str > = Rc :: from ( key) ;
38+ Self :: from ( key)
3939 }
4040}
4141
4242macro_rules! key_impl_from_to_string {
4343 ( $type: ty) => {
4444 impl From <$type> for Key {
4545 fn from( key: $type) -> Self {
46- Key {
47- key: Rc :: new( key. to_string( ) ) ,
48- }
46+ Self :: from( key. to_string( ) . as_str( ) )
4947 }
5048 }
5149 } ;
5250}
53- key_impl_from_to_string ! ( & ' static str ) ;
51+
5452key_impl_from_to_string ! ( String ) ;
53+ key_impl_from_to_string ! ( Rc <String >) ;
5554key_impl_from_to_string ! ( char ) ;
56- key_impl_from_to_string ! ( usize ) ;
5755key_impl_from_to_string ! ( u8 ) ;
5856key_impl_from_to_string ! ( u16 ) ;
5957key_impl_from_to_string ! ( u32 ) ;
6058key_impl_from_to_string ! ( u64 ) ;
6159key_impl_from_to_string ! ( u128 ) ;
60+ key_impl_from_to_string ! ( usize ) ;
6261key_impl_from_to_string ! ( i8 ) ;
6362key_impl_from_to_string ! ( i16 ) ;
6463key_impl_from_to_string ! ( i32 ) ;
6564key_impl_from_to_string ! ( i64 ) ;
6665key_impl_from_to_string ! ( i128 ) ;
66+ key_impl_from_to_string ! ( isize ) ;
6767
6868#[ cfg( test) ]
6969mod test {
@@ -78,11 +78,11 @@ mod test {
7878
7979 #[ test]
8080 fn all_key_conversions ( ) {
81- let rc_key = Rc :: new ( "rc" . to_string ( ) ) ;
8281 html ! {
8382 <key="string literal" >
84- <img key="String" . to_string( ) />
85- <p key=rc_key></p>
83+ <img key="String" . to_owned( ) />
84+ <p key=Rc :: new( "rc" . to_owned( ) ) ></p>
85+ <p key=Rc :: <str >:: from( "rc" ) ></p>
8686 <key='a' >
8787 <p key=11_usize ></p>
8888 <p key=12_u8 ></p>
0 commit comments