1 // Take a look at the license at the top of the repository in the LICENSE file. 2 3 use std::borrow::Cow; 4 use std::default::Default; 5 6 use crate::translate::*; 7 use crate::variant::*; 8 use crate::variant_type::*; 9 10 wrapper! { 11 /// `VariantDict` is a mutable key/value store where the keys are always 12 /// strings and the values are [`Variant`s](variant/struct.Variant.html). 13 /// 14 /// Variant dictionaries can easily be converted to/from `Variant`s of the 15 /// appropriate type. In `glib` terms, this is a variant of the form `"a{sv}"`. 16 /// 17 /// # Panics 18 /// 19 /// Note, pretty much all methods on this struct will panic if the 20 /// [`end_unsafe()`](#method.end_unsafe) method was called on the instance. 21 #[doc(alias = "GVariantDict")] 22 pub struct VariantDict(Shared<ffi::GVariantDict>); 23 24 match fn { 25 ref => |ptr| ffi::g_variant_dict_ref(ptr), 26 unref => |ptr| ffi::g_variant_dict_unref(ptr), 27 type_ => || ffi::g_variant_dict_get_type(), 28 } 29 } 30 31 impl VariantDict { 32 /// Create a new `VariantDict` optionally populating it with the given `Variant` 33 /// 34 /// Since `Variant`s are immutable, this does not couple the `VariantDict` with 35 /// the input `Variant`, instead the contents are copied into the `VariantDict`. 36 /// 37 /// # Panics 38 /// 39 /// This function will panic if the given `Variant` is not of the correct type. 40 #[doc(alias = "g_variant_dict_new")] new(from_asv: Option<&Variant>) -> Self41 pub fn new(from_asv: Option<&Variant>) -> Self { 42 if let Some(var) = from_asv { 43 assert_eq!(var.type_(), VariantDict::static_variant_type()); 44 } 45 unsafe { from_glib_full(ffi::g_variant_dict_new(from_asv.to_glib_none().0)) } 46 } 47 48 /// Check if this `VariantDict` contains the given key. 49 /// 50 /// Look up whether or not the given key is present, returning `true` if it 51 /// is present in `self`. 52 #[doc(alias = "g_variant_dict_contains")] contains(&self, key: &str) -> bool53 pub fn contains(&self, key: &str) -> bool { 54 unsafe { 55 from_glib(ffi::g_variant_dict_contains( 56 self.to_glib_none().0, 57 key.to_glib_none().0, 58 )) 59 } 60 } 61 62 /// Look up and return a value from this `VariantDict`. 63 /// 64 /// The given `key` is looked up in `self`. If `expected_type` is not 65 /// `None` then it will be matched against the type of any found value. 66 /// 67 /// This will return `None` if the `key` is not present in the dictionary 68 /// or if it is present but the type of the value does not match a given 69 /// `expected_type`. Otherwise, `Some(value)` will be returned where 70 /// the `value` is an instance of [`Variant`](variant/struct.Variant.html). 71 #[doc(alias = "g_variant_dict_lookup_value")] lookup_value(&self, key: &str, expected_type: Option<&VariantTy>) -> Option<Variant>72 pub fn lookup_value(&self, key: &str, expected_type: Option<&VariantTy>) -> Option<Variant> { 73 unsafe { 74 from_glib_full(ffi::g_variant_dict_lookup_value( 75 self.to_glib_none().0, 76 key.to_glib_none().0, 77 expected_type.to_glib_none().0, 78 )) 79 } 80 } 81 82 /// Insert a variant into the dictionary. 83 /// 84 /// The given `key`/`value` pair is inserted into `self`. If a value 85 /// was previously associated with `key` then it is overwritten. 86 /// 87 /// For convenience, you may use the [`insert()`](#method.insert) if 88 /// you have a value which implements [`ToVariant`](variant/trait.ToVariant.html). 89 #[doc(alias = "g_variant_dict_insert_value")] insert_value(&self, key: &str, value: &Variant)90 pub fn insert_value(&self, key: &str, value: &Variant) { 91 unsafe { 92 ffi::g_variant_dict_insert_value( 93 self.to_glib_none().0, 94 key.to_glib_none().0, 95 value.to_glib_none().0, 96 ) 97 } 98 } 99 100 /// Insert a value into the dictionary 101 /// 102 /// The given `key`/`value` pair is inserted into `self`. If a value 103 /// was previously associated with `key` then it is overwritten. 104 /// 105 /// This is a convenience method which automatically calls 106 /// [`to_variant()`](variant/trait.ToVariant.html#method.to_variant) for you 107 /// on the given value. 108 /// 109 /// If, on the other hand, you have a [`Variant`](variant/struct.Variant.html) 110 /// instance already, you should use the [`insert_value()`](#method.insert_value) 111 /// method instead. 112 #[doc(alias = "g_variant_dict_insert_value")] insert<T: ToVariant>(&self, key: &str, value: &T)113 pub fn insert<T: ToVariant>(&self, key: &str, value: &T) { 114 unsafe { 115 ffi::g_variant_dict_insert_value( 116 self.to_glib_none().0, 117 key.to_glib_none().0, 118 value.to_variant().to_glib_none().0, 119 ) 120 } 121 } 122 123 /// Remove the given `key` from the dictionary. 124 /// 125 /// This removes the given `key` from the dictionary, releasing the reference 126 /// on the associated value if one is present. 127 /// 128 /// If a `key`/`value` pair was removed from the dictionary, `true` is 129 /// returned. If `key` was not present then `false` is returned instead. 130 #[doc(alias = "g_variant_dict_remove")] remove(&self, key: &str) -> bool131 pub fn remove(&self, key: &str) -> bool { 132 unsafe { 133 from_glib(ffi::g_variant_dict_remove( 134 self.to_glib_none().0, 135 key.to_glib_none().0, 136 )) 137 } 138 } 139 140 /// Convert this dictionary to a [`Variant`](variant/struct.Variant.html) 141 /// 142 /// This method converts `self` into an instance of [`Variant`](variant/struct.Variant.html) 143 /// but in doing so renders it very unsafe to use. 144 /// 145 /// # Safety 146 /// 147 /// After calling this, the underlying `GVariantDict` is in a state where 148 /// the only valid operations to perform as reference ones. As such 149 /// any attempt to read/update the dictionary *will* fail and emit warnings 150 /// of such. 151 /// 152 /// You should only use this function if the extra cost of the safe function 153 /// is too much for your performance critical codepaths end_unsafe(&self) -> Variant154 pub unsafe fn end_unsafe(&self) -> Variant { 155 from_glib_none(ffi::g_variant_dict_end(self.to_glib_none().0)) 156 } 157 158 /// Convert this dictionary to a [`Variant`](variant/struct.Variant.html) 159 /// 160 /// This method converts `self` into an instance of [`Variant`](variant/struct.Variant.html) 161 /// and then reinitialises itself in order to be safe for further use. 162 /// 163 /// If you are certain that nothing other than disposing of references will 164 /// be done after ending the instance, you can call the 165 /// [`end_unsafe()`](#method.end_unsafe) method instead to avoid the unnecessary 166 /// reinitialisation of the dictionary. end(&self) -> Variant167 pub fn end(&self) -> Variant { 168 unsafe { 169 let ret = self.end_unsafe(); 170 // Reinitialise the dict so that we can continue safely 171 ffi::g_variant_dict_init(self.to_glib_none().0, None::<Variant>.to_glib_none().0); 172 ret 173 } 174 } 175 } 176 177 impl Default for VariantDict { default() -> Self178 fn default() -> Self { 179 Self::new(None) 180 } 181 } 182 183 impl StaticVariantType for VariantDict { static_variant_type() -> Cow<'static, VariantTy>184 fn static_variant_type() -> Cow<'static, VariantTy> { 185 unsafe { VariantTy::from_str_unchecked("a{sv}").into() } 186 } 187 } 188 189 impl ToVariant for VariantDict { to_variant(&self) -> Variant190 fn to_variant(&self) -> Variant { 191 self.end() 192 } 193 } 194 195 impl FromVariant for VariantDict { from_variant(variant: &Variant) -> Option<Self>196 fn from_variant(variant: &Variant) -> Option<Self> { 197 if variant.type_() == VariantDict::static_variant_type() { 198 Some(Self::new(Some(variant))) 199 } else { 200 None 201 } 202 } 203 } 204 205 impl From<Variant> for VariantDict { from(other: Variant) -> Self206 fn from(other: Variant) -> Self { 207 Self::new(Some(&other)) 208 } 209 } 210 211 #[cfg(test)] 212 mod test { 213 use super::*; 214 215 #[test] create_destroy()216 fn create_destroy() { 217 let _dict = VariantDict::new(None); 218 } 219 220 #[test] create_roundtrip()221 fn create_roundtrip() { 222 let dict = VariantDict::default(); 223 let var: Variant = dict.to_variant(); 224 let _dict2: VariantDict = var.into(); 225 } 226 227 #[test] create_populate_destroy()228 fn create_populate_destroy() { 229 let dict = VariantDict::default(); 230 dict.insert_value("one", &(1u8.to_variant())); 231 assert_eq!(dict.lookup_value("one", None), Some(1u8.to_variant())); 232 } 233 234 #[test] create_populate_roundtrip()235 fn create_populate_roundtrip() { 236 let dict = VariantDict::default(); 237 dict.insert_value("one", &(1u8.to_variant())); 238 let var: Variant = dict.to_variant(); 239 let dict = VariantDict::from_variant(&var).expect("Not a dict?"); 240 assert_eq!(dict.lookup_value("one", None), Some(1u8.to_variant())); 241 } 242 243 #[test] create_populate_remove()244 fn create_populate_remove() { 245 let dict = VariantDict::default(); 246 let empty_var = dict.to_variant(); 247 dict.insert("one", &1u64); 248 assert!(dict.remove("one")); 249 assert!(!dict.remove("one")); 250 let var2 = dict.to_variant(); 251 assert_eq!(empty_var, var2); 252 } 253 } 254