yew_stdweb/virtual_dom/
key.rs1use crate::html::ImplicitClone;
4use std::fmt::{self, Display, Formatter};
5use std::ops::Deref;
6use std::rc::Rc;
7
8#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
12pub struct Key {
13 key: Rc<str>,
14}
15
16impl Display for Key {
17 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
18 self.key.fmt(f)
19 }
20}
21
22impl Deref for Key {
23 type Target = str;
24
25 fn deref(&self) -> &str {
26 self.key.as_ref()
27 }
28}
29
30impl From<Rc<str>> for Key {
31 fn from(key: Rc<str>) -> Self {
32 Self { key }
33 }
34}
35
36impl From<&'_ str> for Key {
37 fn from(key: &'_ str) -> Self {
38 let key: Rc<str> = Rc::from(key);
39 Self::from(key)
40 }
41}
42
43impl ImplicitClone for Key {}
44
45macro_rules! key_impl_from_to_string {
46 ($type:ty) => {
47 impl From<$type> for Key {
48 fn from(key: $type) -> Self {
49 Self::from(key.to_string().as_str())
50 }
51 }
52 };
53}
54
55key_impl_from_to_string!(String);
56key_impl_from_to_string!(char);
57key_impl_from_to_string!(u8);
58key_impl_from_to_string!(u16);
59key_impl_from_to_string!(u32);
60key_impl_from_to_string!(u64);
61key_impl_from_to_string!(u128);
62key_impl_from_to_string!(usize);
63key_impl_from_to_string!(i8);
64key_impl_from_to_string!(i16);
65key_impl_from_to_string!(i32);
66key_impl_from_to_string!(i64);
67key_impl_from_to_string!(i128);
68key_impl_from_to_string!(isize);
69
70#[cfg(test)]
71mod test {
72 use crate::html;
73 use std::rc::Rc;
74
75 #[cfg(feature = "wasm_test")]
76 use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
77
78 #[cfg(feature = "wasm_test")]
79 wasm_bindgen_test_configure!(run_in_browser);
80
81 #[test]
82 fn all_key_conversions() {
83 html! {
84 <key="string literal">
85 <img key="String".to_owned() />
86 <p key=Rc::<str>::from("rc")></p>
87 <key='a'>
88 <p key=11_usize></p>
89 <p key=12_u8></p>
90 <p key=13_u16></p>
91 <p key=14_u32></p>
92 <p key=15_u64></p>
93 <p key=15_u128></p>
94 <p key=21_isize></p>
95 <p key=22_i8></p>
96 <p key=23_i16></p>
97 <p key=24_i32></p>
98 <p key=25_i128></p>
99 </>
100 </>
101 };
102 }
103}