glib/variant.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3// rustdoc-stripper-ignore-next
4//! `Variant` binding and helper traits.
5//!
6//! [`Variant`](struct.Variant.html) is an immutable dynamically-typed generic
7//! container. Its type and value are defined at construction and never change.
8//!
9//! `Variant` types are described by [`VariantType`](../struct.VariantType.html)
10//! "type strings".
11//!
12//! `GVariant` supports arbitrarily complex types built from primitives like integers, floating point
13//! numbers, strings, arrays, tuples and dictionaries. See [`ToVariant#foreign-impls`] for
14//! a full list of supported types. You may also implement [`ToVariant`] and [`FromVariant`]
15//! manually, or derive them using the [`Variant`](derive@crate::Variant) derive macro.
16//!
17//! # Examples
18//!
19//! ```
20//! use glib::prelude::*; // or `use gtk::prelude::*;`
21//! use glib::variant::{Variant, FromVariant};
22//! use std::collections::HashMap;
23//!
24//! // Using the `ToVariant` trait.
25//! let num = 10.to_variant();
26//!
27//! // `is` tests the type of the value.
28//! assert!(num.is::<i32>());
29//!
30//! // `get` tries to extract the value.
31//! assert_eq!(num.get::<i32>(), Some(10));
32//! assert_eq!(num.get::<u32>(), None);
33//!
34//! // `get_str` tries to borrow a string slice.
35//! let hello = "Hello!".to_variant();
36//! assert_eq!(hello.str(), Some("Hello!"));
37//! assert_eq!(num.str(), None);
38//!
39//! // `fixed_array` tries to borrow a fixed size array (u8, bool, i16, etc.),
40//! // rather than creating a deep copy which would be expensive for
41//! // nontrivially sized arrays of fixed size elements.
42//! // The test data here is the zstd compression header, which
43//! // stands in for arbitrary binary data (e.g. not UTF-8).
44//! let bufdata = b"\xFD\x2F\xB5\x28";
45//! let bufv = glib::Variant::array_from_fixed_array(&bufdata[..]);
46//! assert_eq!(bufv.fixed_array::<u8>().unwrap(), bufdata);
47//! assert!(num.fixed_array::<u8>().is_err());
48//!
49//! // Variant carrying a Variant
50//! let variant = Variant::from_variant(&hello);
51//! let variant = variant.as_variant().unwrap();
52//! assert_eq!(variant.str(), Some("Hello!"));
53//!
54//! // Variant carrying an array
55//! let array = ["Hello", "there!"];
56//! let variant = array.into_iter().collect::<Variant>();
57//! assert_eq!(variant.n_children(), 2);
58//! assert_eq!(variant.child_value(0).str(), Some("Hello"));
59//! assert_eq!(variant.child_value(1).str(), Some("there!"));
60//!
61//! // You can also convert from and to a Vec
62//! let variant = vec!["Hello", "there!"].to_variant();
63//! assert_eq!(variant.n_children(), 2);
64//! let vec = <Vec<String>>::from_variant(&variant).unwrap();
65//! assert_eq!(vec[0], "Hello");
66//!
67//! // Conversion to and from HashMap and BTreeMap is also possible
68//! let mut map: HashMap<u16, &str> = HashMap::new();
69//! map.insert(1, "hi");
70//! map.insert(2, "there");
71//! let variant = map.to_variant();
72//! assert_eq!(variant.n_children(), 2);
73//! let map: HashMap<u16, String> = HashMap::from_variant(&variant).unwrap();
74//! assert_eq!(map[&1], "hi");
75//! assert_eq!(map[&2], "there");
76//!
77//! // And conversion to and from tuples.
78//! let variant = ("hello", 42u16, vec![ "there", "you" ],).to_variant();
79//! assert_eq!(variant.n_children(), 3);
80//! assert_eq!(variant.type_().as_str(), "(sqas)");
81//! let tuple = <(String, u16, Vec<String>)>::from_variant(&variant).unwrap();
82//! assert_eq!(tuple.0, "hello");
83//! assert_eq!(tuple.1, 42);
84//! assert_eq!(tuple.2, &[ "there", "you"]);
85//!
86//! // `Option` is supported as well, through maybe types
87//! let variant = Some("hello").to_variant();
88//! assert_eq!(variant.n_children(), 1);
89//! let mut s = <Option<String>>::from_variant(&variant).unwrap();
90//! assert_eq!(s.unwrap(), "hello");
91//! s = None;
92//! let variant = s.to_variant();
93//! assert_eq!(variant.n_children(), 0);
94//! let s = <Option<String>>::from_variant(&variant).unwrap();
95//! assert!(s.is_none());
96//!
97//! // Paths may be converted, too. Please note the portability warning above!
98//! use std::path::{Path, PathBuf};
99//! let path = Path::new("foo/bar");
100//! let path_variant = path.to_variant();
101//! assert_eq!(PathBuf::from_variant(&path_variant).as_deref(), Some(path));
102//! ```
103
104use std::{
105 borrow::Cow,
106 cmp::Ordering,
107 collections::{BTreeMap, HashMap},
108 fmt,
109 fmt::Display,
110 hash::{BuildHasher, Hash, Hasher},
111 mem, ptr, slice, str,
112};
113
114use crate::{
115 Bytes, Type, VariantIter, VariantStrIter, VariantTy, VariantType, ffi, gobject_ffi, prelude::*,
116 translate::*,
117};
118
119wrapper! {
120 // rustdoc-stripper-ignore-next
121 /// A generic immutable value capable of carrying various types.
122 ///
123 /// See the [module documentation](index.html) for more details.
124 // rustdoc-stripper-ignore-next-stop
125 /// `GVariant` is a variant datatype; it can contain one or more values
126 /// along with information about the type of the values.
127 ///
128 /// A `GVariant` may contain simple types, like an integer, or a boolean value;
129 /// or complex types, like an array of two strings, or a dictionary of key
130 /// value pairs. A `GVariant` is also immutable: once it’s been created neither
131 /// its type nor its content can be modified further.
132 ///
133 /// `GVariant` is useful whenever data needs to be serialized, for example when
134 /// sending method parameters in D-Bus, or when saving settings using
135 /// [`GSettings`](../gio/class.Settings.html).
136 ///
137 /// When creating a new `GVariant`, you pass the data you want to store in it
138 /// along with a string representing the type of data you wish to pass to it.
139 ///
140 /// For instance, if you want to create a `GVariant` holding an integer value you
141 /// can use:
142 ///
143 /// **⚠️ The following code is in c ⚠️**
144 ///
145 /// ```c
146 /// GVariant *v = g_variant_new ("u", 40);
147 /// ```
148 ///
149 /// The string `u` in the first argument tells `GVariant` that the data passed to
150 /// the constructor (`40`) is going to be an unsigned integer.
151 ///
152 /// More advanced examples of `GVariant` in use can be found in documentation for
153 /// [`GVariant` format strings](gvariant-format-strings.html#pointers).
154 ///
155 /// The range of possible values is determined by the type.
156 ///
157 /// The type system used by `GVariant` is [type@GLib.VariantType].
158 ///
159 /// `GVariant` instances always have a type and a value (which are given
160 /// at construction time). The type and value of a `GVariant` instance
161 /// can never change other than by the `GVariant` itself being
162 /// destroyed. A `GVariant` cannot contain a pointer.
163 ///
164 /// `GVariant` is reference counted using `GLib::Variant::ref()` and
165 /// `GLib::Variant::unref()`. `GVariant` also has floating reference counts —
166 /// see [`ref_sink()`][Self::ref_sink()].
167 ///
168 /// `GVariant` is completely threadsafe. A `GVariant` instance can be
169 /// concurrently accessed in any way from any number of threads without
170 /// problems.
171 ///
172 /// `GVariant` is heavily optimised for dealing with data in serialized
173 /// form. It works particularly well with data located in memory-mapped
174 /// files. It can perform nearly all deserialization operations in a
175 /// small constant time, usually touching only a single memory page.
176 /// Serialized `GVariant` data can also be sent over the network.
177 ///
178 /// `GVariant` is largely compatible with D-Bus. Almost all types of
179 /// `GVariant` instances can be sent over D-Bus. See [type@GLib.VariantType] for
180 /// exceptions. (However, `GVariant`’s serialization format is not the same
181 /// as the serialization format of a D-Bus message body: use
182 /// [GDBusMessage](../gio/class.DBusMessage.html), in the GIO library, for those.)
183 ///
184 /// For space-efficiency, the `GVariant` serialization format does not
185 /// automatically include the variant’s length, type or endianness,
186 /// which must either be implied from context (such as knowledge that a
187 /// particular file format always contains a little-endian
188 /// `G_VARIANT_TYPE_VARIANT` which occupies the whole length of the file)
189 /// or supplied out-of-band (for instance, a length, type and/or endianness
190 /// indicator could be placed at the beginning of a file, network message
191 /// or network stream).
192 ///
193 /// A `GVariant`’s size is limited mainly by any lower level operating
194 /// system constraints, such as the number of bits in `gsize`. For
195 /// example, it is reasonable to have a 2GB file mapped into memory
196 /// with `GLib::MappedFile`, and call `GLib::Variant::new_from_data()` on
197 /// it.
198 ///
199 /// For convenience to C programmers, `GVariant` features powerful
200 /// varargs-based value construction and destruction. This feature is
201 /// designed to be embedded in other libraries.
202 ///
203 /// There is a Python-inspired text language for describing `GVariant`
204 /// values. `GVariant` includes a printer for this language and a parser
205 /// with type inferencing.
206 ///
207 /// ## Memory Use
208 ///
209 /// `GVariant` tries to be quite efficient with respect to memory use.
210 /// This section gives a rough idea of how much memory is used by the
211 /// current implementation. The information here is subject to change
212 /// in the future.
213 ///
214 /// The memory allocated by `GVariant` can be grouped into 4 broad
215 /// purposes: memory for serialized data, memory for the type
216 /// information cache, buffer management memory and memory for the
217 /// `GVariant` structure itself.
218 ///
219 /// ## Serialized Data Memory
220 ///
221 /// This is the memory that is used for storing `GVariant` data in
222 /// serialized form. This is what would be sent over the network or
223 /// what would end up on disk, not counting any indicator of the
224 /// endianness, or of the length or type of the top-level variant.
225 ///
226 /// The amount of memory required to store a boolean is 1 byte. 16,
227 /// 32 and 64 bit integers and double precision floating point numbers
228 /// use their ‘natural’ size. Strings (including object path and
229 /// signature strings) are stored with a nul terminator, and as such
230 /// use the length of the string plus 1 byte.
231 ///
232 /// ‘Maybe’ types use no space at all to represent the null value and
233 /// use the same amount of space (sometimes plus one byte) as the
234 /// equivalent non-maybe-typed value to represent the non-null case.
235 ///
236 /// Arrays use the amount of space required to store each of their
237 /// members, concatenated. Additionally, if the items stored in an
238 /// array are not of a fixed-size (ie: strings, other arrays, etc)
239 /// then an additional framing offset is stored for each item. The
240 /// size of this offset is either 1, 2 or 4 bytes depending on the
241 /// overall size of the container. Additionally, extra padding bytes
242 /// are added as required for alignment of child values.
243 ///
244 /// Tuples (including dictionary entries) use the amount of space
245 /// required to store each of their members, concatenated, plus one
246 /// framing offset (as per arrays) for each non-fixed-sized item in
247 /// the tuple, except for the last one. Additionally, extra padding
248 /// bytes are added as required for alignment of child values.
249 ///
250 /// Variants use the same amount of space as the item inside of the
251 /// variant, plus 1 byte, plus the length of the type string for the
252 /// item inside the variant.
253 ///
254 /// As an example, consider a dictionary mapping strings to variants.
255 /// In the case that the dictionary is empty, 0 bytes are required for
256 /// the serialization.
257 ///
258 /// If we add an item ‘width’ that maps to the int32 value of 500 then
259 /// we will use 4 bytes to store the int32 (so 6 for the variant
260 /// containing it) and 6 bytes for the string. The variant must be
261 /// aligned to 8 after the 6 bytes of the string, so that’s 2 extra
262 /// bytes. 6 (string) + 2 (padding) + 6 (variant) is 14 bytes used
263 /// for the dictionary entry. An additional 1 byte is added to the
264 /// array as a framing offset making a total of 15 bytes.
265 ///
266 /// If we add another entry, ‘title’ that maps to a nullable string
267 /// that happens to have a value of null, then we use 0 bytes for the
268 /// null value (and 3 bytes for the variant to contain it along with
269 /// its type string) plus 6 bytes for the string. Again, we need 2
270 /// padding bytes. That makes a total of 6 + 2 + 3 = 11 bytes.
271 ///
272 /// We now require extra padding between the two items in the array.
273 /// After the 14 bytes of the first item, that’s 2 bytes required.
274 /// We now require 2 framing offsets for an extra two
275 /// bytes. 14 + 2 + 11 + 2 = 29 bytes to encode the entire two-item
276 /// dictionary.
277 ///
278 /// ## Type Information Cache
279 ///
280 /// For each `GVariant` type that currently exists in the program a type
281 /// information structure is kept in the type information cache. The
282 /// type information structure is required for rapid deserialization.
283 ///
284 /// Continuing with the above example, if a `GVariant` exists with the
285 /// type `a{sv}` then a type information struct will exist for
286 /// `a{sv}`, `{sv}`, `s`, and `v`. Multiple uses of the same type
287 /// will share the same type information. Additionally, all
288 /// single-digit types are stored in read-only static memory and do
289 /// not contribute to the writable memory footprint of a program using
290 /// `GVariant`.
291 ///
292 /// Aside from the type information structures stored in read-only
293 /// memory, there are two forms of type information. One is used for
294 /// container types where there is a single element type: arrays and
295 /// maybe types. The other is used for container types where there
296 /// are multiple element types: tuples and dictionary entries.
297 ///
298 /// Array type info structures are `6 * sizeof (void *)`, plus the
299 /// memory required to store the type string itself. This means that
300 /// on 32-bit systems, the cache entry for `a{sv}` would require 30
301 /// bytes of memory (plus allocation overhead).
302 ///
303 /// Tuple type info structures are `6 * sizeof (void *)`, plus `4 *
304 /// sizeof (void *)` for each item in the tuple, plus the memory
305 /// required to store the type string itself. A 2-item tuple, for
306 /// example, would have a type information structure that consumed
307 /// writable memory in the size of `14 * sizeof (void *)` (plus type
308 /// string) This means that on 32-bit systems, the cache entry for
309 /// `{sv}` would require 61 bytes of memory (plus allocation overhead).
310 ///
311 /// This means that in total, for our `a{sv}` example, 91 bytes of
312 /// type information would be allocated.
313 ///
314 /// The type information cache, additionally, uses a `GLib::HashTable` to
315 /// store and look up the cached items and stores a pointer to this
316 /// hash table in static storage. The hash table is freed when there
317 /// are zero items in the type cache.
318 ///
319 /// Although these sizes may seem large it is important to remember
320 /// that a program will probably only have a very small number of
321 /// different types of values in it and that only one type information
322 /// structure is required for many different values of the same type.
323 ///
324 /// ## Buffer Management Memory
325 ///
326 /// `GVariant` uses an internal buffer management structure to deal
327 /// with the various different possible sources of serialized data
328 /// that it uses. The buffer is responsible for ensuring that the
329 /// correct call is made when the data is no longer in use by
330 /// `GVariant`. This may involve a `free()` or
331 /// even `GLib::MappedFile::unref()`.
332 ///
333 /// One buffer management structure is used for each chunk of
334 /// serialized data. The size of the buffer management structure
335 /// is `4 * (void *)`. On 32-bit systems, that’s 16 bytes.
336 ///
337 /// ## GVariant structure
338 ///
339 /// The size of a `GVariant` structure is `6 * (void *)`. On 32-bit
340 /// systems, that’s 24 bytes.
341 ///
342 /// `GVariant` structures only exist if they are explicitly created
343 /// with API calls. For example, if a `GVariant` is constructed out of
344 /// serialized data for the example given above (with the dictionary)
345 /// then although there are 9 individual values that comprise the
346 /// entire dictionary (two keys, two values, two variants containing
347 /// the values, two dictionary entries, plus the dictionary itself),
348 /// only 1 `GVariant` instance exists — the one referring to the
349 /// dictionary.
350 ///
351 /// If calls are made to start accessing the other values then
352 /// `GVariant` instances will exist for those values only for as long
353 /// as they are in use (ie: until you call `GLib::Variant::unref()`). The
354 /// type information is shared. The serialized data and the buffer
355 /// management structure for that serialized data is shared by the
356 /// child.
357 ///
358 /// ## Summary
359 ///
360 /// To put the entire example together, for our dictionary mapping
361 /// strings to variants (with two entries, as given above), we are
362 /// using 91 bytes of memory for type information, 29 bytes of memory
363 /// for the serialized data, 16 bytes for buffer management and 24
364 /// bytes for the `GVariant` instance, or a total of 160 bytes, plus
365 /// allocation overhead. If we were to use [`child_value()`][Self::child_value()]
366 /// to access the two dictionary entries, we would use an additional 48
367 /// bytes. If we were to have other dictionaries of the same type, we
368 /// would use more memory for the serialized data and buffer
369 /// management for those dictionaries, but the type information would
370 /// be shared.
371 #[doc(alias = "GVariant")]
372 pub struct Variant(Shared<ffi::GVariant>);
373
374 match fn {
375 ref => |ptr| ffi::g_variant_ref_sink(ptr),
376 unref => |ptr| ffi::g_variant_unref(ptr),
377 }
378}
379
380impl StaticType for Variant {
381 #[inline]
382 fn static_type() -> Type {
383 Type::VARIANT
384 }
385}
386
387#[doc(hidden)]
388impl crate::value::ValueType for Variant {
389 type Type = Variant;
390}
391
392#[doc(hidden)]
393impl crate::value::ValueTypeOptional for Variant {}
394
395#[doc(hidden)]
396unsafe impl<'a> crate::value::FromValue<'a> for Variant {
397 type Checker = crate::value::GenericValueTypeOrNoneChecker<Self>;
398
399 unsafe fn from_value(value: &'a crate::Value) -> Self {
400 unsafe {
401 let ptr = gobject_ffi::g_value_dup_variant(value.to_glib_none().0);
402 debug_assert!(!ptr.is_null());
403 from_glib_full(ptr)
404 }
405 }
406}
407
408#[doc(hidden)]
409impl crate::value::ToValue for Variant {
410 fn to_value(&self) -> crate::Value {
411 unsafe {
412 let mut value = crate::Value::from_type_unchecked(Variant::static_type());
413 gobject_ffi::g_value_take_variant(value.to_glib_none_mut().0, self.to_glib_full());
414 value
415 }
416 }
417
418 fn value_type(&self) -> crate::Type {
419 Variant::static_type()
420 }
421}
422
423#[doc(hidden)]
424impl From<Variant> for crate::Value {
425 #[inline]
426 fn from(v: Variant) -> Self {
427 unsafe {
428 let mut value = crate::Value::from_type_unchecked(Variant::static_type());
429 gobject_ffi::g_value_take_variant(value.to_glib_none_mut().0, v.into_glib_ptr());
430 value
431 }
432 }
433}
434
435#[doc(hidden)]
436impl crate::value::ToValueOptional for Variant {
437 fn to_value_optional(s: Option<&Self>) -> crate::Value {
438 let mut value = crate::Value::for_value_type::<Self>();
439 unsafe {
440 gobject_ffi::g_value_take_variant(value.to_glib_none_mut().0, s.to_glib_full());
441 }
442
443 value
444 }
445}
446
447// rustdoc-stripper-ignore-next
448/// An error returned from the [`try_get`](struct.Variant.html#method.try_get) function
449/// on a [`Variant`](struct.Variant.html) when the expected type does not match the actual type.
450#[derive(Clone, PartialEq, Eq, Debug)]
451pub struct VariantTypeMismatchError {
452 pub actual: VariantType,
453 pub expected: VariantType,
454}
455
456impl VariantTypeMismatchError {
457 pub fn new(actual: VariantType, expected: VariantType) -> Self {
458 Self { actual, expected }
459 }
460}
461
462impl fmt::Display for VariantTypeMismatchError {
463 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
464 write!(
465 f,
466 "Type mismatch: Expected '{}' got '{}'",
467 self.expected, self.actual
468 )
469 }
470}
471
472impl std::error::Error for VariantTypeMismatchError {}
473
474impl Variant {
475 // rustdoc-stripper-ignore-next
476 /// Returns the type of the value.
477 // rustdoc-stripper-ignore-next-stop
478 /// Determines the type of @self.
479 ///
480 /// The return value is valid for the lifetime of @self and must not
481 /// be freed.
482 ///
483 /// # Returns
484 ///
485 /// a #GVariantType
486 #[doc(alias = "g_variant_get_type")]
487 pub fn type_(&self) -> &VariantTy {
488 unsafe { VariantTy::from_ptr(ffi::g_variant_get_type(self.to_glib_none().0)) }
489 }
490
491 // rustdoc-stripper-ignore-next
492 /// Returns `true` if the type of the value corresponds to `T`.
493 #[inline]
494 #[doc(alias = "g_variant_is_of_type")]
495 pub fn is<T: StaticVariantType>(&self) -> bool {
496 self.is_type(&T::static_variant_type())
497 }
498
499 // rustdoc-stripper-ignore-next
500 /// Returns `true` if the type of the value corresponds to `type_`.
501 ///
502 /// This is equivalent to [`self.type_().is_subtype_of(type_)`](VariantTy::is_subtype_of).
503 #[inline]
504 #[doc(alias = "g_variant_is_of_type")]
505 pub fn is_type(&self, type_: &VariantTy) -> bool {
506 unsafe {
507 from_glib(ffi::g_variant_is_of_type(
508 self.to_glib_none().0,
509 type_.to_glib_none().0,
510 ))
511 }
512 }
513
514 // rustdoc-stripper-ignore-next
515 /// Returns the classification of the variant.
516 // rustdoc-stripper-ignore-next-stop
517 /// Classifies @self according to its top-level type.
518 ///
519 /// # Returns
520 ///
521 /// the #GVariantClass of @self
522 #[doc(alias = "g_variant_classify")]
523 pub fn classify(&self) -> crate::VariantClass {
524 unsafe { from_glib(ffi::g_variant_classify(self.to_glib_none().0)) }
525 }
526
527 // rustdoc-stripper-ignore-next
528 /// Tries to extract a value of type `T`.
529 ///
530 /// Returns `Some` if `T` matches the variant's type.
531 // rustdoc-stripper-ignore-next-stop
532 /// Deconstructs a #GVariant instance.
533 ///
534 /// Think of this function as an analogue to scanf().
535 ///
536 /// The arguments that are expected by this function are entirely
537 /// determined by @format_string. @format_string also restricts the
538 /// permissible types of @self. It is an error to give a value with
539 /// an incompatible type. See the section on
540 /// [GVariant format strings](gvariant-format-strings.html).
541 /// Please note that the syntax of the format string is very likely to be
542 /// extended in the future.
543 ///
544 /// @format_string determines the C types that are used for unpacking
545 /// the values and also determines if the values are copied or borrowed,
546 /// see the section on
547 /// [`GVariant` format strings](gvariant-format-strings.html#pointers).
548 /// ## `format_string`
549 /// a #GVariant format string
550 #[inline]
551 pub fn get<T: FromVariant>(&self) -> Option<T> {
552 T::from_variant(self)
553 }
554
555 // rustdoc-stripper-ignore-next
556 /// Tries to extract a value of type `T`.
557 pub fn try_get<T: FromVariant>(&self) -> Result<T, VariantTypeMismatchError> {
558 self.get().ok_or_else(|| {
559 VariantTypeMismatchError::new(
560 self.type_().to_owned(),
561 T::static_variant_type().into_owned(),
562 )
563 })
564 }
565
566 // rustdoc-stripper-ignore-next
567 /// Boxes value.
568 #[inline]
569 pub fn from_variant(value: &Variant) -> Self {
570 unsafe { from_glib_none(ffi::g_variant_new_variant(value.to_glib_none().0)) }
571 }
572
573 // rustdoc-stripper-ignore-next
574 /// Unboxes self.
575 ///
576 /// Returns `Some` if self contains a `Variant`.
577 #[inline]
578 #[doc(alias = "get_variant")]
579 pub fn as_variant(&self) -> Option<Variant> {
580 unsafe { from_glib_full(ffi::g_variant_get_variant(self.to_glib_none().0)) }
581 }
582
583 // rustdoc-stripper-ignore-next
584 /// Reads a child item out of a container `Variant` instance.
585 ///
586 /// # Panics
587 ///
588 /// * if `self` is not a container type.
589 /// * if given `index` is larger than number of children.
590 // rustdoc-stripper-ignore-next-stop
591 /// Reads a child item out of a container #GVariant instance. This
592 /// includes variants, maybes, arrays, tuples and dictionary
593 /// entries. It is an error to call this function on any other type of
594 /// #GVariant.
595 ///
596 /// It is an error if @index_ is greater than the number of child items
597 /// in the container. See g_variant_n_children().
598 ///
599 /// The returned value is never floating. You should free it with
600 /// g_variant_unref() when you're done with it.
601 ///
602 /// Note that values borrowed from the returned child are not guaranteed to
603 /// still be valid after the child is freed even if you still hold a reference
604 /// to @self, if @self has not been serialized at the time this function is
605 /// called. To avoid this, you can serialize @self by calling
606 /// g_variant_get_data() and optionally ignoring the return value.
607 ///
608 /// There may be implementation specific restrictions on deeply nested values,
609 /// which would result in the unit tuple being returned as the child value,
610 /// instead of further nested children. #GVariant is guaranteed to handle
611 /// nesting up to at least 64 levels.
612 ///
613 /// This function is O(1).
614 /// ## `index_`
615 /// the index of the child to fetch
616 ///
617 /// # Returns
618 ///
619 /// the child at the specified index
620 #[doc(alias = "get_child_value")]
621 #[doc(alias = "g_variant_get_child_value")]
622 #[must_use]
623 pub fn child_value(&self, index: usize) -> Variant {
624 assert!(self.is_container());
625 assert!(index < self.n_children());
626
627 unsafe { from_glib_full(ffi::g_variant_get_child_value(self.to_glib_none().0, index)) }
628 }
629
630 // rustdoc-stripper-ignore-next
631 /// Try to read a child item out of a container `Variant` instance.
632 ///
633 /// It returns `None` if `self` is not a container type or if the given
634 /// `index` is larger than number of children.
635 pub fn try_child_value(&self, index: usize) -> Option<Variant> {
636 if !(self.is_container() && index < self.n_children()) {
637 return None;
638 }
639
640 let v =
641 unsafe { from_glib_full(ffi::g_variant_get_child_value(self.to_glib_none().0, index)) };
642 Some(v)
643 }
644
645 // rustdoc-stripper-ignore-next
646 /// Try to read a child item out of a container `Variant` instance.
647 ///
648 /// It returns `Ok(None)` if `self` is not a container type or if the given
649 /// `index` is larger than number of children. An error is thrown if the
650 /// type does not match.
651 pub fn try_child_get<T: StaticVariantType + FromVariant>(
652 &self,
653 index: usize,
654 ) -> Result<Option<T>, VariantTypeMismatchError> {
655 // TODO: In the future optimize this by using g_variant_get_child()
656 // directly to avoid allocating a GVariant.
657 self.try_child_value(index).map(|v| v.try_get()).transpose()
658 }
659
660 // rustdoc-stripper-ignore-next
661 /// Read a child item out of a container `Variant` instance.
662 ///
663 /// # Panics
664 ///
665 /// * if `self` is not a container type.
666 /// * if given `index` is larger than number of children.
667 /// * if the expected variant type does not match
668 pub fn child_get<T: StaticVariantType + FromVariant>(&self, index: usize) -> T {
669 // TODO: In the future optimize this by using g_variant_get_child()
670 // directly to avoid allocating a GVariant.
671 self.child_value(index).get().unwrap()
672 }
673
674 // rustdoc-stripper-ignore-next
675 /// Tries to extract a `&str`.
676 ///
677 /// Returns `Some` if the variant has a string type (`s`, `o` or `g` type
678 /// strings).
679 #[doc(alias = "get_str")]
680 #[doc(alias = "g_variant_get_string")]
681 pub fn str(&self) -> Option<&str> {
682 unsafe {
683 match self.type_().as_str() {
684 "s" | "o" | "g" => {
685 let mut len = 0;
686 let ptr = ffi::g_variant_get_string(self.to_glib_none().0, &mut len);
687 if len == 0 {
688 Some("")
689 } else {
690 let ret = str::from_utf8_unchecked(slice::from_raw_parts(
691 ptr as *const u8,
692 len as _,
693 ));
694 Some(ret)
695 }
696 }
697 _ => None,
698 }
699 }
700 }
701
702 // rustdoc-stripper-ignore-next
703 /// Tries to extract a `&[T]` from a variant of array type with a suitable element type.
704 ///
705 /// Returns an error if the type is wrong.
706 // rustdoc-stripper-ignore-next-stop
707 /// Provides access to the serialized data for an array of fixed-sized
708 /// items.
709 ///
710 /// @self must be an array with fixed-sized elements. Numeric types are
711 /// fixed-size, as are tuples containing only other fixed-sized types.
712 ///
713 /// @element_size must be the size of a single element in the array,
714 /// as given by the section on
715 /// [serialized data memory](struct.Variant.html#serialized-data-memory).
716 ///
717 /// In particular, arrays of these fixed-sized types can be interpreted
718 /// as an array of the given C type, with @element_size set to the size
719 /// the appropriate type:
720 ///
721 /// - `G_VARIANT_TYPE_INT16` (etc.): #gint16 (etc.)
722 /// - `G_VARIANT_TYPE_BOOLEAN`: #guchar (not #gboolean!)
723 /// - `G_VARIANT_TYPE_BYTE`: #guint8
724 /// - `G_VARIANT_TYPE_HANDLE`: #guint32
725 /// - `G_VARIANT_TYPE_DOUBLE`: #gdouble
726 ///
727 /// For example, if calling this function for an array of 32-bit integers,
728 /// you might say `sizeof(gint32)`. This value isn't used except for the purpose
729 /// of a double-check that the form of the serialized data matches the caller's
730 /// expectation.
731 ///
732 /// @n_elements, which must be non-[`None`], is set equal to the number of
733 /// items in the array.
734 /// ## `element_size`
735 /// the size of each element
736 ///
737 /// # Returns
738 ///
739 /// a pointer to
740 /// the fixed array
741 #[doc(alias = "g_variant_get_fixed_array")]
742 pub fn fixed_array<T: FixedSizeVariantType>(&self) -> Result<&[T], VariantTypeMismatchError> {
743 unsafe {
744 let expected_ty = T::static_variant_type().as_array();
745 if self.type_() != expected_ty {
746 return Err(VariantTypeMismatchError {
747 actual: self.type_().to_owned(),
748 expected: expected_ty.into_owned(),
749 });
750 }
751
752 let mut n_elements = mem::MaybeUninit::uninit();
753 let ptr = ffi::g_variant_get_fixed_array(
754 self.to_glib_none().0,
755 n_elements.as_mut_ptr(),
756 mem::size_of::<T>(),
757 );
758
759 let n_elements = n_elements.assume_init();
760 if n_elements == 0 {
761 Ok(&[])
762 } else {
763 debug_assert!(!ptr.is_null());
764 Ok(slice::from_raw_parts(ptr as *const T, n_elements))
765 }
766 }
767 }
768
769 // rustdoc-stripper-ignore-next
770 /// Creates a new Variant array from children.
771 ///
772 /// # Panics
773 ///
774 /// This function panics if not all variants are of type `T`.
775 #[doc(alias = "g_variant_new_array")]
776 pub fn array_from_iter<T: StaticVariantType>(
777 children: impl IntoIterator<Item = Variant>,
778 ) -> Self {
779 Self::array_from_iter_with_type(&T::static_variant_type(), children)
780 }
781
782 // rustdoc-stripper-ignore-next
783 /// Creates a new Variant array from children with the specified type.
784 ///
785 /// # Panics
786 ///
787 /// This function panics if not all variants are of type `type_`.
788 #[doc(alias = "g_variant_new_array")]
789 pub fn array_from_iter_with_type(
790 type_: &VariantTy,
791 children: impl IntoIterator<Item = impl AsRef<Variant>>,
792 ) -> Self {
793 unsafe {
794 let mut builder = mem::MaybeUninit::uninit();
795 ffi::g_variant_builder_init(builder.as_mut_ptr(), type_.as_array().to_glib_none().0);
796 let mut builder = builder.assume_init();
797 for value in children.into_iter() {
798 let value = value.as_ref();
799 if ffi::g_variant_is_of_type(value.to_glib_none().0, type_.to_glib_none().0)
800 == ffi::GFALSE
801 {
802 ffi::g_variant_builder_clear(&mut builder);
803 assert!(value.is_type(type_));
804 }
805
806 ffi::g_variant_builder_add_value(&mut builder, value.to_glib_none().0);
807 }
808 from_glib_none(ffi::g_variant_builder_end(&mut builder))
809 }
810 }
811
812 // rustdoc-stripper-ignore-next
813 /// Creates a new Variant array from a fixed array.
814 #[doc(alias = "g_variant_new_fixed_array")]
815 pub fn array_from_fixed_array<T: FixedSizeVariantType>(array: &[T]) -> Self {
816 let type_ = T::static_variant_type();
817
818 unsafe {
819 from_glib_none(ffi::g_variant_new_fixed_array(
820 type_.as_ptr(),
821 array.as_ptr() as ffi::gconstpointer,
822 array.len(),
823 mem::size_of::<T>(),
824 ))
825 }
826 }
827
828 // rustdoc-stripper-ignore-next
829 /// Creates a new Variant tuple from children.
830 #[doc(alias = "g_variant_new_tuple")]
831 pub fn tuple_from_iter(children: impl IntoIterator<Item = impl AsRef<Variant>>) -> Self {
832 unsafe {
833 let mut builder = mem::MaybeUninit::uninit();
834 ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::TUPLE.to_glib_none().0);
835 let mut builder = builder.assume_init();
836 for value in children.into_iter() {
837 ffi::g_variant_builder_add_value(&mut builder, value.as_ref().to_glib_none().0);
838 }
839 from_glib_none(ffi::g_variant_builder_end(&mut builder))
840 }
841 }
842
843 // rustdoc-stripper-ignore-next
844 /// Creates a new dictionary entry Variant.
845 ///
846 /// [DictEntry] should be preferred over this when the types are known statically.
847 #[doc(alias = "g_variant_new_dict_entry")]
848 pub fn from_dict_entry(key: &Variant, value: &Variant) -> Self {
849 unsafe {
850 from_glib_none(ffi::g_variant_new_dict_entry(
851 key.to_glib_none().0,
852 value.to_glib_none().0,
853 ))
854 }
855 }
856
857 // rustdoc-stripper-ignore-next
858 /// Creates a new maybe Variant.
859 #[doc(alias = "g_variant_new_maybe")]
860 pub fn from_maybe<T: StaticVariantType>(child: Option<&Variant>) -> Self {
861 let type_ = T::static_variant_type();
862 match child {
863 Some(child) => {
864 assert_eq!(type_, child.type_());
865
866 Self::from_some(child)
867 }
868 None => Self::from_none(&type_),
869 }
870 }
871
872 // rustdoc-stripper-ignore-next
873 /// Creates a new maybe Variant from a child.
874 #[doc(alias = "g_variant_new_maybe")]
875 pub fn from_some(child: &Variant) -> Self {
876 unsafe {
877 from_glib_none(ffi::g_variant_new_maybe(
878 ptr::null(),
879 child.to_glib_none().0,
880 ))
881 }
882 }
883
884 // rustdoc-stripper-ignore-next
885 /// Creates a new maybe Variant with Nothing.
886 #[doc(alias = "g_variant_new_maybe")]
887 pub fn from_none(type_: &VariantTy) -> Self {
888 unsafe {
889 from_glib_none(ffi::g_variant_new_maybe(
890 type_.to_glib_none().0,
891 ptr::null_mut(),
892 ))
893 }
894 }
895
896 // rustdoc-stripper-ignore-next
897 /// Extract the value of a maybe Variant.
898 ///
899 /// Returns the child value, or `None` if the value is Nothing.
900 ///
901 /// # Panics
902 ///
903 /// Panics if the variant is not maybe-typed.
904 #[inline]
905 pub fn as_maybe(&self) -> Option<Variant> {
906 assert!(self.type_().is_maybe());
907
908 unsafe { from_glib_full(ffi::g_variant_get_maybe(self.to_glib_none().0)) }
909 }
910
911 // rustdoc-stripper-ignore-next
912 /// Pretty-print the contents of this variant in a human-readable form.
913 ///
914 /// A variant can be recreated from this output via [`Variant::parse`].
915 // rustdoc-stripper-ignore-next-stop
916 /// Pretty-prints @self in the format understood by g_variant_parse().
917 ///
918 /// The format is described [here](gvariant-text-format.html).
919 ///
920 /// If @type_annotate is [`true`], then type information is included in
921 /// the output.
922 /// ## `type_annotate`
923 /// [`true`] if type information should be included in
924 /// the output
925 ///
926 /// # Returns
927 ///
928 /// a newly-allocated string holding the result.
929 #[doc(alias = "g_variant_print")]
930 pub fn print(&self, type_annotate: bool) -> crate::GString {
931 unsafe {
932 from_glib_full(ffi::g_variant_print(
933 self.to_glib_none().0,
934 type_annotate.into_glib(),
935 ))
936 }
937 }
938
939 // rustdoc-stripper-ignore-next
940 /// Parses a GVariant from the text representation produced by [`print()`](Self::print).
941 #[doc(alias = "g_variant_parse")]
942 pub fn parse(type_: Option<&VariantTy>, text: &str) -> Result<Self, crate::Error> {
943 unsafe {
944 let mut error = ptr::null_mut();
945 let text = text.as_bytes().as_ptr_range();
946 let variant = ffi::g_variant_parse(
947 type_.to_glib_none().0,
948 text.start as *const _,
949 text.end as *const _,
950 ptr::null_mut(),
951 &mut error,
952 );
953 if variant.is_null() {
954 debug_assert!(!error.is_null());
955 Err(from_glib_full(error))
956 } else {
957 debug_assert!(error.is_null());
958 Ok(from_glib_full(variant))
959 }
960 }
961 }
962
963 // rustdoc-stripper-ignore-next
964 /// Constructs a new serialized-mode GVariant instance.
965 // rustdoc-stripper-ignore-next-stop
966 /// Constructs a new serialized-mode #GVariant instance. This is the
967 /// inner interface for creation of new serialized values that gets
968 /// called from various functions in gvariant.c.
969 ///
970 /// A reference is taken on @bytes.
971 ///
972 /// The data in @bytes must be aligned appropriately for the @type_ being loaded.
973 /// Otherwise this function will internally create a copy of the memory (since
974 /// GLib 2.60) or (in older versions) fail and exit the process.
975 /// ## `type_`
976 /// a #GVariantType
977 /// ## `bytes`
978 /// a #GBytes
979 /// ## `trusted`
980 /// if the contents of @bytes are trusted
981 ///
982 /// # Returns
983 ///
984 /// a new #GVariant with a floating reference
985 #[doc(alias = "g_variant_new_from_bytes")]
986 pub fn from_bytes<T: StaticVariantType>(bytes: &Bytes) -> Self {
987 Variant::from_bytes_with_type(bytes, &T::static_variant_type())
988 }
989
990 // rustdoc-stripper-ignore-next
991 /// Constructs a new serialized-mode GVariant instance.
992 ///
993 /// This is the same as `from_bytes`, except that checks on the passed
994 /// data are skipped.
995 ///
996 /// You should not use this function on data from external sources.
997 ///
998 /// # Safety
999 ///
1000 /// Since the data is not validated, this is potentially dangerous if called
1001 /// on bytes which are not guaranteed to have come from serialising another
1002 /// Variant. The caller is responsible for ensuring bad data is not passed in.
1003 pub unsafe fn from_bytes_trusted<T: StaticVariantType>(bytes: &Bytes) -> Self {
1004 unsafe { Variant::from_bytes_with_type_trusted(bytes, &T::static_variant_type()) }
1005 }
1006
1007 // rustdoc-stripper-ignore-next
1008 /// Constructs a new serialized-mode GVariant instance.
1009 // rustdoc-stripper-ignore-next-stop
1010 /// Creates a new #GVariant instance from serialized data.
1011 ///
1012 /// @type_ is the type of #GVariant instance that will be constructed.
1013 /// The interpretation of @data depends on knowing the type.
1014 ///
1015 /// @data is not modified by this function and must remain valid with an
1016 /// unchanging value until such a time as @notify is called with
1017 /// @user_data. If the contents of @data change before that time then
1018 /// the result is undefined.
1019 ///
1020 /// If @data is trusted to be serialized data in normal form then
1021 /// @trusted should be [`true`]. This applies to serialized data created
1022 /// within this process or read from a trusted location on the disk (such
1023 /// as a file installed in /usr/lib alongside your application). You
1024 /// should set trusted to [`false`] if @data is read from the network, a
1025 /// file in the user's home directory, etc.
1026 ///
1027 /// If @data was not stored in this machine's native endianness, any multi-byte
1028 /// numeric values in the returned variant will also be in non-native
1029 /// endianness. g_variant_byteswap() can be used to recover the original values.
1030 ///
1031 /// @notify will be called with @user_data when @data is no longer
1032 /// needed. The exact time of this call is unspecified and might even be
1033 /// before this function returns.
1034 ///
1035 /// Note: @data must be backed by memory that is aligned appropriately for the
1036 /// @type_ being loaded. Otherwise this function will internally create a copy of
1037 /// the memory (since GLib 2.60) or (in older versions) fail and exit the
1038 /// process.
1039 /// ## `type_`
1040 /// a definite #GVariantType
1041 /// ## `data`
1042 /// the serialized data
1043 /// ## `trusted`
1044 /// [`true`] if @data is definitely in normal form
1045 /// ## `notify`
1046 /// function to call when @data is no longer needed
1047 ///
1048 /// # Returns
1049 ///
1050 /// a new floating #GVariant of type @type_
1051 #[doc(alias = "g_variant_new_from_data")]
1052 pub fn from_data<T: StaticVariantType, A: AsRef<[u8]>>(data: A) -> Self {
1053 Variant::from_data_with_type(data, &T::static_variant_type())
1054 }
1055
1056 // rustdoc-stripper-ignore-next
1057 /// Constructs a new serialized-mode GVariant instance.
1058 ///
1059 /// This is the same as `from_data`, except that checks on the passed
1060 /// data are skipped.
1061 ///
1062 /// You should not use this function on data from external sources.
1063 ///
1064 /// # Safety
1065 ///
1066 /// Since the data is not validated, this is potentially dangerous if called
1067 /// on bytes which are not guaranteed to have come from serialising another
1068 /// Variant. The caller is responsible for ensuring bad data is not passed in.
1069 pub unsafe fn from_data_trusted<T: StaticVariantType, A: AsRef<[u8]>>(data: A) -> Self {
1070 unsafe { Variant::from_data_with_type_trusted(data, &T::static_variant_type()) }
1071 }
1072
1073 // rustdoc-stripper-ignore-next
1074 /// Constructs a new serialized-mode GVariant instance with a given type.
1075 #[doc(alias = "g_variant_new_from_bytes")]
1076 pub fn from_bytes_with_type(bytes: &Bytes, type_: &VariantTy) -> Self {
1077 unsafe {
1078 from_glib_none(ffi::g_variant_new_from_bytes(
1079 type_.as_ptr() as *const _,
1080 bytes.to_glib_none().0,
1081 false.into_glib(),
1082 ))
1083 }
1084 }
1085
1086 // rustdoc-stripper-ignore-next
1087 /// Constructs a new serialized-mode GVariant instance with a given type.
1088 ///
1089 /// This is the same as `from_bytes`, except that checks on the passed
1090 /// data are skipped.
1091 ///
1092 /// You should not use this function on data from external sources.
1093 ///
1094 /// # Safety
1095 ///
1096 /// Since the data is not validated, this is potentially dangerous if called
1097 /// on bytes which are not guaranteed to have come from serialising another
1098 /// Variant. The caller is responsible for ensuring bad data is not passed in.
1099 pub unsafe fn from_bytes_with_type_trusted(bytes: &Bytes, type_: &VariantTy) -> Self {
1100 unsafe {
1101 from_glib_none(ffi::g_variant_new_from_bytes(
1102 type_.as_ptr() as *const _,
1103 bytes.to_glib_none().0,
1104 true.into_glib(),
1105 ))
1106 }
1107 }
1108
1109 // rustdoc-stripper-ignore-next
1110 /// Constructs a new serialized-mode GVariant instance with a given type.
1111 #[doc(alias = "g_variant_new_from_data")]
1112 pub fn from_data_with_type<A: AsRef<[u8]>>(data: A, type_: &VariantTy) -> Self {
1113 unsafe {
1114 let data = Box::new(data);
1115 let (data_ptr, len) = {
1116 let data = (*data).as_ref();
1117 (data.as_ptr(), data.len())
1118 };
1119
1120 unsafe extern "C" fn free_data<A: AsRef<[u8]>>(ptr: ffi::gpointer) {
1121 unsafe {
1122 let _ = Box::from_raw(ptr as *mut A);
1123 }
1124 }
1125
1126 from_glib_none(ffi::g_variant_new_from_data(
1127 type_.as_ptr() as *const _,
1128 data_ptr as ffi::gconstpointer,
1129 len,
1130 false.into_glib(),
1131 Some(free_data::<A>),
1132 Box::into_raw(data) as ffi::gpointer,
1133 ))
1134 }
1135 }
1136
1137 // rustdoc-stripper-ignore-next
1138 /// Constructs a new serialized-mode GVariant instance with a given type.
1139 ///
1140 /// This is the same as `from_data`, except that checks on the passed
1141 /// data are skipped.
1142 ///
1143 /// You should not use this function on data from external sources.
1144 ///
1145 /// # Safety
1146 ///
1147 /// Since the data is not validated, this is potentially dangerous if called
1148 /// on bytes which are not guaranteed to have come from serialising another
1149 /// Variant. The caller is responsible for ensuring bad data is not passed in.
1150 pub unsafe fn from_data_with_type_trusted<A: AsRef<[u8]>>(data: A, type_: &VariantTy) -> Self {
1151 unsafe {
1152 let data = Box::new(data);
1153 let (data_ptr, len) = {
1154 let data = (*data).as_ref();
1155 (data.as_ptr(), data.len())
1156 };
1157
1158 unsafe extern "C" fn free_data<A: AsRef<[u8]>>(ptr: ffi::gpointer) {
1159 unsafe {
1160 let _ = Box::from_raw(ptr as *mut A);
1161 }
1162 }
1163
1164 from_glib_none(ffi::g_variant_new_from_data(
1165 type_.as_ptr() as *const _,
1166 data_ptr as ffi::gconstpointer,
1167 len,
1168 true.into_glib(),
1169 Some(free_data::<A>),
1170 Box::into_raw(data) as ffi::gpointer,
1171 ))
1172 }
1173 }
1174
1175 // rustdoc-stripper-ignore-next
1176 /// Returns the serialized form of a GVariant instance.
1177 // rustdoc-stripper-ignore-next-stop
1178 /// Returns a pointer to the serialized form of a #GVariant instance.
1179 /// The semantics of this function are exactly the same as
1180 /// g_variant_get_data(), except that the returned #GBytes holds
1181 /// a reference to the variant data.
1182 ///
1183 /// # Returns
1184 ///
1185 /// A new #GBytes representing the variant data
1186 #[doc(alias = "get_data_as_bytes")]
1187 #[doc(alias = "g_variant_get_data_as_bytes")]
1188 pub fn data_as_bytes(&self) -> Bytes {
1189 unsafe { from_glib_full(ffi::g_variant_get_data_as_bytes(self.to_glib_none().0)) }
1190 }
1191
1192 // rustdoc-stripper-ignore-next
1193 /// Returns the serialized form of a GVariant instance.
1194 // rustdoc-stripper-ignore-next-stop
1195 /// Returns a pointer to the serialized form of a #GVariant instance.
1196 /// The returned data may not be in fully-normalised form if read from an
1197 /// untrusted source. The returned data must not be freed; it remains
1198 /// valid for as long as @self exists.
1199 ///
1200 /// If @self is a fixed-sized value that was deserialized from a
1201 /// corrupted serialized container then [`None`] may be returned. In this
1202 /// case, the proper thing to do is typically to use the appropriate
1203 /// number of nul bytes in place of @self. If @self is not fixed-sized
1204 /// then [`None`] is never returned.
1205 ///
1206 /// In the case that @self is already in serialized form, this function
1207 /// is O(1). If the value is not already in serialized form,
1208 /// serialization occurs implicitly and is approximately O(n) in the size
1209 /// of the result.
1210 ///
1211 /// To deserialize the data returned by this function, in addition to the
1212 /// serialized data, you must know the type of the #GVariant, and (if the
1213 /// machine might be different) the endianness of the machine that stored
1214 /// it. As a result, file formats or network messages that incorporate
1215 /// serialized #GVariants must include this information either
1216 /// implicitly (for instance "the file always contains a
1217 /// `G_VARIANT_TYPE_VARIANT` and it is always in little-endian order") or
1218 /// explicitly (by storing the type and/or endianness in addition to the
1219 /// serialized data).
1220 ///
1221 /// # Returns
1222 ///
1223 /// the serialized form of @self, or [`None`]
1224 #[doc(alias = "g_variant_get_data")]
1225 pub fn data(&self) -> &[u8] {
1226 unsafe {
1227 let selfv = self.to_glib_none();
1228 let len = ffi::g_variant_get_size(selfv.0);
1229 if len == 0 {
1230 return &[];
1231 }
1232 let ptr = ffi::g_variant_get_data(selfv.0);
1233 slice::from_raw_parts(ptr as *const _, len as _)
1234 }
1235 }
1236
1237 // rustdoc-stripper-ignore-next
1238 /// Returns the size of serialized form of a GVariant instance.
1239 // rustdoc-stripper-ignore-next-stop
1240 /// Determines the number of bytes that would be required to store @self
1241 /// with g_variant_store().
1242 ///
1243 /// If @self has a fixed-sized type then this function always returned
1244 /// that fixed size.
1245 ///
1246 /// In the case that @self is already in serialized form or the size has
1247 /// already been calculated (ie: this function has been called before)
1248 /// then this function is O(1). Otherwise, the size is calculated, an
1249 /// operation which is approximately O(n) in the number of values
1250 /// involved.
1251 ///
1252 /// # Returns
1253 ///
1254 /// the serialized size of @self
1255 #[doc(alias = "g_variant_get_size")]
1256 pub fn size(&self) -> usize {
1257 unsafe { ffi::g_variant_get_size(self.to_glib_none().0) }
1258 }
1259
1260 // rustdoc-stripper-ignore-next
1261 /// Stores the serialized form of a GVariant instance into the given slice.
1262 ///
1263 /// The slice needs to be big enough.
1264 // rustdoc-stripper-ignore-next-stop
1265 /// Stores the serialized form of @self at @data. @data should be
1266 /// large enough. See g_variant_get_size().
1267 ///
1268 /// The stored data is in machine native byte order but may not be in
1269 /// fully-normalised form if read from an untrusted source. See
1270 /// g_variant_get_normal_form() for a solution.
1271 ///
1272 /// As with g_variant_get_data(), to be able to deserialize the
1273 /// serialized variant successfully, its type and (if the destination
1274 /// machine might be different) its endianness must also be available.
1275 ///
1276 /// This function is approximately O(n) in the size of @data.
1277 #[doc(alias = "g_variant_store")]
1278 pub fn store(&self, data: &mut [u8]) -> Result<usize, crate::BoolError> {
1279 unsafe {
1280 let size = ffi::g_variant_get_size(self.to_glib_none().0);
1281 if data.len() < size {
1282 return Err(bool_error!("Provided slice is too small"));
1283 }
1284
1285 ffi::g_variant_store(self.to_glib_none().0, data.as_mut_ptr() as ffi::gpointer);
1286
1287 Ok(size)
1288 }
1289 }
1290
1291 // rustdoc-stripper-ignore-next
1292 /// Returns a copy of the variant in normal form.
1293 // rustdoc-stripper-ignore-next-stop
1294 /// Gets a #GVariant instance that has the same value as @self and is
1295 /// trusted to be in normal form.
1296 ///
1297 /// If @self is already trusted to be in normal form then a new
1298 /// reference to @self is returned.
1299 ///
1300 /// If @self is not already trusted, then it is scanned to check if it
1301 /// is in normal form. If it is found to be in normal form then it is
1302 /// marked as trusted and a new reference to it is returned.
1303 ///
1304 /// If @self is found not to be in normal form then a new trusted
1305 /// #GVariant is created with the same value as @self. The non-normal parts of
1306 /// @self will be replaced with default values which are guaranteed to be in
1307 /// normal form.
1308 ///
1309 /// It makes sense to call this function if you've received #GVariant
1310 /// data from untrusted sources and you want to ensure your serialized
1311 /// output is definitely in normal form.
1312 ///
1313 /// If @self is already in normal form, a new reference will be returned
1314 /// (which will be floating if @self is floating). If it is not in normal form,
1315 /// the newly created #GVariant will be returned with a single non-floating
1316 /// reference. Typically, g_variant_take_ref() should be called on the return
1317 /// value from this function to guarantee ownership of a single non-floating
1318 /// reference to it.
1319 ///
1320 /// # Returns
1321 ///
1322 /// a trusted #GVariant
1323 #[doc(alias = "g_variant_get_normal_form")]
1324 #[must_use]
1325 pub fn normal_form(&self) -> Self {
1326 unsafe { from_glib_full(ffi::g_variant_get_normal_form(self.to_glib_none().0)) }
1327 }
1328
1329 // rustdoc-stripper-ignore-next
1330 /// Returns a copy of the variant in the opposite endianness.
1331 // rustdoc-stripper-ignore-next-stop
1332 /// Performs a byteswapping operation on the contents of @self. The
1333 /// result is that all multi-byte numeric data contained in @self is
1334 /// byteswapped. That includes 16, 32, and 64bit signed and unsigned
1335 /// integers as well as file handles and double precision floating point
1336 /// values.
1337 ///
1338 /// This function is an identity mapping on any value that does not
1339 /// contain multi-byte numeric data. That include strings, booleans,
1340 /// bytes and containers containing only these things (recursively).
1341 ///
1342 /// While this function can safely handle untrusted, non-normal data, it is
1343 /// recommended to check whether the input is in normal form beforehand, using
1344 /// g_variant_is_normal_form(), and to reject non-normal inputs if your
1345 /// application can be strict about what inputs it rejects.
1346 ///
1347 /// The returned value is always in normal form and is marked as trusted.
1348 /// A full, not floating, reference is returned.
1349 ///
1350 /// # Returns
1351 ///
1352 /// the byteswapped form of @self
1353 #[doc(alias = "g_variant_byteswap")]
1354 #[must_use]
1355 pub fn byteswap(&self) -> Self {
1356 unsafe { from_glib_full(ffi::g_variant_byteswap(self.to_glib_none().0)) }
1357 }
1358
1359 // rustdoc-stripper-ignore-next
1360 /// Determines the number of children in a container GVariant instance.
1361 // rustdoc-stripper-ignore-next-stop
1362 /// Determines the number of children in a container #GVariant instance.
1363 /// This includes variants, maybes, arrays, tuples and dictionary
1364 /// entries. It is an error to call this function on any other type of
1365 /// #GVariant.
1366 ///
1367 /// For variants, the return value is always 1. For values with maybe
1368 /// types, it is always zero or one. For arrays, it is the length of the
1369 /// array. For tuples it is the number of tuple items (which depends
1370 /// only on the type). For dictionary entries, it is always 2
1371 ///
1372 /// This function is O(1).
1373 ///
1374 /// # Returns
1375 ///
1376 /// the number of children in the container
1377 #[doc(alias = "g_variant_n_children")]
1378 pub fn n_children(&self) -> usize {
1379 assert!(self.is_container());
1380
1381 unsafe { ffi::g_variant_n_children(self.to_glib_none().0) }
1382 }
1383
1384 // rustdoc-stripper-ignore-next
1385 /// Create an iterator over items in the variant.
1386 ///
1387 /// Note that this heap allocates a variant for each element,
1388 /// which can be particularly expensive for large arrays.
1389 pub fn iter(&self) -> VariantIter {
1390 assert!(self.is_container());
1391
1392 VariantIter::new(self.clone())
1393 }
1394
1395 // rustdoc-stripper-ignore-next
1396 /// Create an iterator over borrowed strings from a GVariant of type `as` (array of string).
1397 ///
1398 /// This will fail if the variant is not an array of with
1399 /// the expected child type.
1400 ///
1401 /// A benefit of this API over [`Self::iter()`] is that it
1402 /// minimizes allocation, and provides strongly typed access.
1403 ///
1404 /// ```
1405 /// # use glib::prelude::*;
1406 /// let strs = &["foo", "bar"];
1407 /// let strs_variant: glib::Variant = strs.to_variant();
1408 /// for s in strs_variant.array_iter_str()? {
1409 /// println!("{}", s);
1410 /// }
1411 /// # Ok::<(), Box<dyn std::error::Error>>(())
1412 /// ```
1413 pub fn array_iter_str(&self) -> Result<VariantStrIter<'_>, VariantTypeMismatchError> {
1414 let child_ty = String::static_variant_type();
1415 let actual_ty = self.type_();
1416 let expected_ty = child_ty.as_array();
1417 if actual_ty != expected_ty {
1418 return Err(VariantTypeMismatchError {
1419 actual: actual_ty.to_owned(),
1420 expected: expected_ty.into_owned(),
1421 });
1422 }
1423
1424 Ok(VariantStrIter::new(self))
1425 }
1426
1427 // rustdoc-stripper-ignore-next
1428 /// Return whether this Variant is a container type.
1429 // rustdoc-stripper-ignore-next-stop
1430 /// Checks if @self is a container.
1431 ///
1432 /// # Returns
1433 ///
1434 /// [`true`] if @self is a container
1435 #[doc(alias = "g_variant_is_container")]
1436 pub fn is_container(&self) -> bool {
1437 unsafe { from_glib(ffi::g_variant_is_container(self.to_glib_none().0)) }
1438 }
1439
1440 // rustdoc-stripper-ignore-next
1441 /// Return whether this Variant is in normal form.
1442 // rustdoc-stripper-ignore-next-stop
1443 /// Checks if @self is in normal form.
1444 ///
1445 /// The main reason to do this is to detect if a given chunk of
1446 /// serialized data is in normal form: load the data into a #GVariant
1447 /// using g_variant_new_from_data() and then use this function to
1448 /// check.
1449 ///
1450 /// If @self is found to be in normal form then it will be marked as
1451 /// being trusted. If the value was already marked as being trusted then
1452 /// this function will immediately return [`true`].
1453 ///
1454 /// There may be implementation specific restrictions on deeply nested values.
1455 /// GVariant is guaranteed to handle nesting up to at least 64 levels.
1456 ///
1457 /// # Returns
1458 ///
1459 /// [`true`] if @self is in normal form
1460 #[doc(alias = "g_variant_is_normal_form")]
1461 pub fn is_normal_form(&self) -> bool {
1462 unsafe { from_glib(ffi::g_variant_is_normal_form(self.to_glib_none().0)) }
1463 }
1464
1465 // rustdoc-stripper-ignore-next
1466 /// Return whether input string is a valid `VariantClass::ObjectPath`.
1467 // rustdoc-stripper-ignore-next-stop
1468 /// Determines if a given string is a valid D-Bus object path. You
1469 /// should ensure that a string is a valid D-Bus object path before
1470 /// passing it to g_variant_new_object_path().
1471 ///
1472 /// A valid object path starts with `/` followed by zero or more
1473 /// sequences of characters separated by `/` characters. Each sequence
1474 /// must contain only the characters `[A-Z][a-z][0-9]_`. No sequence
1475 /// (including the one following the final `/` character) may be empty.
1476 /// ## `string`
1477 /// a normal C nul-terminated string
1478 ///
1479 /// # Returns
1480 ///
1481 /// [`true`] if @string is a D-Bus object path
1482 #[doc(alias = "g_variant_is_object_path")]
1483 pub fn is_object_path(string: &str) -> bool {
1484 unsafe { from_glib(ffi::g_variant_is_object_path(string.to_glib_none().0)) }
1485 }
1486
1487 // rustdoc-stripper-ignore-next
1488 /// Return whether input string is a valid `VariantClass::Signature`.
1489 // rustdoc-stripper-ignore-next-stop
1490 /// Determines if a given string is a valid D-Bus type signature. You
1491 /// should ensure that a string is a valid D-Bus type signature before
1492 /// passing it to g_variant_new_signature().
1493 ///
1494 /// D-Bus type signatures consist of zero or more definite #GVariantType
1495 /// strings in sequence.
1496 /// ## `string`
1497 /// a normal C nul-terminated string
1498 ///
1499 /// # Returns
1500 ///
1501 /// [`true`] if @string is a D-Bus type signature
1502 #[doc(alias = "g_variant_is_signature")]
1503 pub fn is_signature(string: &str) -> bool {
1504 unsafe { from_glib(ffi::g_variant_is_signature(string.to_glib_none().0)) }
1505 }
1506}
1507
1508unsafe impl Send for Variant {}
1509unsafe impl Sync for Variant {}
1510
1511impl fmt::Debug for Variant {
1512 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1513 f.debug_struct("Variant")
1514 .field("ptr", &ToGlibPtr::<*const _>::to_glib_none(self).0)
1515 .field("type", &self.type_())
1516 .field("value", &self.to_string())
1517 .finish()
1518 }
1519}
1520
1521impl fmt::Display for Variant {
1522 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1523 f.write_str(&self.print(true))
1524 }
1525}
1526
1527impl str::FromStr for Variant {
1528 type Err = crate::Error;
1529
1530 fn from_str(s: &str) -> Result<Self, Self::Err> {
1531 Self::parse(None, s)
1532 }
1533}
1534
1535impl PartialEq for Variant {
1536 #[doc(alias = "g_variant_equal")]
1537 fn eq(&self, other: &Self) -> bool {
1538 unsafe {
1539 from_glib(ffi::g_variant_equal(
1540 ToGlibPtr::<*const _>::to_glib_none(self).0 as *const _,
1541 ToGlibPtr::<*const _>::to_glib_none(other).0 as *const _,
1542 ))
1543 }
1544 }
1545}
1546
1547impl Eq for Variant {}
1548
1549impl PartialOrd for Variant {
1550 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1551 unsafe {
1552 if ffi::g_variant_classify(self.to_glib_none().0)
1553 != ffi::g_variant_classify(other.to_glib_none().0)
1554 {
1555 return None;
1556 }
1557
1558 if self.is_container() {
1559 return None;
1560 }
1561
1562 let res = ffi::g_variant_compare(
1563 ToGlibPtr::<*const _>::to_glib_none(self).0 as *const _,
1564 ToGlibPtr::<*const _>::to_glib_none(other).0 as *const _,
1565 );
1566
1567 Some(res.cmp(&0))
1568 }
1569 }
1570}
1571
1572impl Hash for Variant {
1573 #[doc(alias = "g_variant_hash")]
1574 fn hash<H: Hasher>(&self, state: &mut H) {
1575 unsafe {
1576 state.write_u32(ffi::g_variant_hash(
1577 ToGlibPtr::<*const _>::to_glib_none(self).0 as *const _,
1578 ))
1579 }
1580 }
1581}
1582
1583impl AsRef<Variant> for Variant {
1584 #[inline]
1585 fn as_ref(&self) -> &Self {
1586 self
1587 }
1588}
1589
1590// rustdoc-stripper-ignore-next
1591/// Converts to `Variant`.
1592pub trait ToVariant {
1593 // rustdoc-stripper-ignore-next
1594 /// Returns a `Variant` clone of `self`.
1595 fn to_variant(&self) -> Variant;
1596}
1597
1598// rustdoc-stripper-ignore-next
1599/// Extracts a value.
1600pub trait FromVariant: Sized + StaticVariantType {
1601 // rustdoc-stripper-ignore-next
1602 /// Tries to extract a value.
1603 ///
1604 /// Returns `Some` if the variant's type matches `Self`.
1605 fn from_variant(variant: &Variant) -> Option<Self>;
1606}
1607
1608// rustdoc-stripper-ignore-next
1609/// Returns `VariantType` of `Self`.
1610pub trait StaticVariantType {
1611 // rustdoc-stripper-ignore-next
1612 /// Returns the `VariantType` corresponding to `Self`.
1613 fn static_variant_type() -> Cow<'static, VariantTy>;
1614}
1615
1616impl StaticVariantType for Variant {
1617 fn static_variant_type() -> Cow<'static, VariantTy> {
1618 Cow::Borrowed(VariantTy::VARIANT)
1619 }
1620}
1621
1622impl<T: ?Sized + ToVariant> ToVariant for &T {
1623 fn to_variant(&self) -> Variant {
1624 <T as ToVariant>::to_variant(self)
1625 }
1626}
1627
1628impl<'a, T: Into<Variant> + Clone> From<&'a T> for Variant {
1629 #[inline]
1630 fn from(v: &'a T) -> Self {
1631 v.clone().into()
1632 }
1633}
1634
1635impl<T: ?Sized + StaticVariantType> StaticVariantType for &T {
1636 fn static_variant_type() -> Cow<'static, VariantTy> {
1637 <T as StaticVariantType>::static_variant_type()
1638 }
1639}
1640
1641macro_rules! impl_numeric {
1642 ($name:ty, $typ:expr, $new_fn:ident, $get_fn:ident) => {
1643 impl StaticVariantType for $name {
1644 fn static_variant_type() -> Cow<'static, VariantTy> {
1645 Cow::Borrowed($typ)
1646 }
1647 }
1648
1649 impl ToVariant for $name {
1650 fn to_variant(&self) -> Variant {
1651 unsafe { from_glib_none(ffi::$new_fn(*self)) }
1652 }
1653 }
1654
1655 impl From<$name> for Variant {
1656 #[inline]
1657 fn from(v: $name) -> Self {
1658 v.to_variant()
1659 }
1660 }
1661
1662 impl FromVariant for $name {
1663 fn from_variant(variant: &Variant) -> Option<Self> {
1664 unsafe {
1665 if variant.is::<Self>() {
1666 Some(ffi::$get_fn(variant.to_glib_none().0))
1667 } else {
1668 None
1669 }
1670 }
1671 }
1672 }
1673 };
1674}
1675
1676impl_numeric!(u8, VariantTy::BYTE, g_variant_new_byte, g_variant_get_byte);
1677impl_numeric!(
1678 i16,
1679 VariantTy::INT16,
1680 g_variant_new_int16,
1681 g_variant_get_int16
1682);
1683impl_numeric!(
1684 u16,
1685 VariantTy::UINT16,
1686 g_variant_new_uint16,
1687 g_variant_get_uint16
1688);
1689impl_numeric!(
1690 i32,
1691 VariantTy::INT32,
1692 g_variant_new_int32,
1693 g_variant_get_int32
1694);
1695impl_numeric!(
1696 u32,
1697 VariantTy::UINT32,
1698 g_variant_new_uint32,
1699 g_variant_get_uint32
1700);
1701impl_numeric!(
1702 i64,
1703 VariantTy::INT64,
1704 g_variant_new_int64,
1705 g_variant_get_int64
1706);
1707impl_numeric!(
1708 u64,
1709 VariantTy::UINT64,
1710 g_variant_new_uint64,
1711 g_variant_get_uint64
1712);
1713impl_numeric!(
1714 f64,
1715 VariantTy::DOUBLE,
1716 g_variant_new_double,
1717 g_variant_get_double
1718);
1719
1720impl StaticVariantType for () {
1721 fn static_variant_type() -> Cow<'static, VariantTy> {
1722 Cow::Borrowed(VariantTy::UNIT)
1723 }
1724}
1725
1726impl ToVariant for () {
1727 fn to_variant(&self) -> Variant {
1728 unsafe { from_glib_none(ffi::g_variant_new_tuple(ptr::null(), 0)) }
1729 }
1730}
1731
1732impl From<()> for Variant {
1733 #[inline]
1734 fn from(_: ()) -> Self {
1735 ().to_variant()
1736 }
1737}
1738
1739impl FromVariant for () {
1740 fn from_variant(variant: &Variant) -> Option<Self> {
1741 if variant.is::<Self>() { Some(()) } else { None }
1742 }
1743}
1744
1745impl StaticVariantType for bool {
1746 fn static_variant_type() -> Cow<'static, VariantTy> {
1747 Cow::Borrowed(VariantTy::BOOLEAN)
1748 }
1749}
1750
1751impl ToVariant for bool {
1752 fn to_variant(&self) -> Variant {
1753 unsafe { from_glib_none(ffi::g_variant_new_boolean(self.into_glib())) }
1754 }
1755}
1756
1757impl From<bool> for Variant {
1758 #[inline]
1759 fn from(v: bool) -> Self {
1760 v.to_variant()
1761 }
1762}
1763
1764impl FromVariant for bool {
1765 fn from_variant(variant: &Variant) -> Option<Self> {
1766 unsafe {
1767 if variant.is::<Self>() {
1768 Some(from_glib(ffi::g_variant_get_boolean(
1769 variant.to_glib_none().0,
1770 )))
1771 } else {
1772 None
1773 }
1774 }
1775 }
1776}
1777
1778impl StaticVariantType for String {
1779 fn static_variant_type() -> Cow<'static, VariantTy> {
1780 Cow::Borrowed(VariantTy::STRING)
1781 }
1782}
1783
1784impl ToVariant for String {
1785 fn to_variant(&self) -> Variant {
1786 self[..].to_variant()
1787 }
1788}
1789
1790impl From<String> for Variant {
1791 #[inline]
1792 fn from(s: String) -> Self {
1793 s.to_variant()
1794 }
1795}
1796
1797impl FromVariant for String {
1798 fn from_variant(variant: &Variant) -> Option<Self> {
1799 variant.str().map(String::from)
1800 }
1801}
1802
1803impl StaticVariantType for str {
1804 fn static_variant_type() -> Cow<'static, VariantTy> {
1805 String::static_variant_type()
1806 }
1807}
1808
1809impl ToVariant for str {
1810 fn to_variant(&self) -> Variant {
1811 unsafe { from_glib_none(ffi::g_variant_new_take_string(self.to_glib_full())) }
1812 }
1813}
1814
1815impl From<&str> for Variant {
1816 #[inline]
1817 fn from(s: &str) -> Self {
1818 s.to_variant()
1819 }
1820}
1821
1822impl StaticVariantType for std::path::PathBuf {
1823 fn static_variant_type() -> Cow<'static, VariantTy> {
1824 std::path::Path::static_variant_type()
1825 }
1826}
1827
1828impl ToVariant for std::path::PathBuf {
1829 fn to_variant(&self) -> Variant {
1830 self.as_path().to_variant()
1831 }
1832}
1833
1834impl From<std::path::PathBuf> for Variant {
1835 #[inline]
1836 fn from(p: std::path::PathBuf) -> Self {
1837 p.to_variant()
1838 }
1839}
1840
1841impl FromVariant for std::path::PathBuf {
1842 fn from_variant(variant: &Variant) -> Option<Self> {
1843 unsafe {
1844 let ptr = ffi::g_variant_get_bytestring(variant.to_glib_none().0);
1845 Some(crate::translate::c_to_path_buf(ptr as *const _))
1846 }
1847 }
1848}
1849
1850impl StaticVariantType for std::path::Path {
1851 fn static_variant_type() -> Cow<'static, VariantTy> {
1852 <&[u8]>::static_variant_type()
1853 }
1854}
1855
1856impl ToVariant for std::path::Path {
1857 fn to_variant(&self) -> Variant {
1858 let tmp = crate::translate::path_to_c(self);
1859 unsafe { from_glib_none(ffi::g_variant_new_bytestring(tmp.as_ptr() as *const u8)) }
1860 }
1861}
1862
1863impl From<&std::path::Path> for Variant {
1864 #[inline]
1865 fn from(p: &std::path::Path) -> Self {
1866 p.to_variant()
1867 }
1868}
1869
1870impl StaticVariantType for std::ffi::OsString {
1871 fn static_variant_type() -> Cow<'static, VariantTy> {
1872 std::ffi::OsStr::static_variant_type()
1873 }
1874}
1875
1876impl ToVariant for std::ffi::OsString {
1877 fn to_variant(&self) -> Variant {
1878 self.as_os_str().to_variant()
1879 }
1880}
1881
1882impl From<std::ffi::OsString> for Variant {
1883 #[inline]
1884 fn from(s: std::ffi::OsString) -> Self {
1885 s.to_variant()
1886 }
1887}
1888
1889impl FromVariant for std::ffi::OsString {
1890 fn from_variant(variant: &Variant) -> Option<Self> {
1891 unsafe {
1892 let ptr = ffi::g_variant_get_bytestring(variant.to_glib_none().0);
1893 Some(crate::translate::c_to_os_string(ptr as *const _))
1894 }
1895 }
1896}
1897
1898impl StaticVariantType for std::ffi::OsStr {
1899 fn static_variant_type() -> Cow<'static, VariantTy> {
1900 <&[u8]>::static_variant_type()
1901 }
1902}
1903
1904impl ToVariant for std::ffi::OsStr {
1905 fn to_variant(&self) -> Variant {
1906 let tmp = crate::translate::os_str_to_c(self);
1907 unsafe { from_glib_none(ffi::g_variant_new_bytestring(tmp.as_ptr() as *const u8)) }
1908 }
1909}
1910
1911impl From<&std::ffi::OsStr> for Variant {
1912 #[inline]
1913 fn from(s: &std::ffi::OsStr) -> Self {
1914 s.to_variant()
1915 }
1916}
1917
1918impl<T: StaticVariantType> StaticVariantType for Option<T> {
1919 fn static_variant_type() -> Cow<'static, VariantTy> {
1920 Cow::Owned(VariantType::new_maybe(&T::static_variant_type()))
1921 }
1922}
1923
1924impl<T: StaticVariantType + ToVariant> ToVariant for Option<T> {
1925 fn to_variant(&self) -> Variant {
1926 Variant::from_maybe::<T>(self.as_ref().map(|m| m.to_variant()).as_ref())
1927 }
1928}
1929
1930impl<T: StaticVariantType + Into<Variant>> From<Option<T>> for Variant {
1931 #[inline]
1932 fn from(v: Option<T>) -> Self {
1933 Variant::from_maybe::<T>(v.map(|v| v.into()).as_ref())
1934 }
1935}
1936
1937impl<T: StaticVariantType + FromVariant> FromVariant for Option<T> {
1938 fn from_variant(variant: &Variant) -> Option<Self> {
1939 unsafe {
1940 if variant.is::<Self>() {
1941 let c_child = ffi::g_variant_get_maybe(variant.to_glib_none().0);
1942 if !c_child.is_null() {
1943 let child: Variant = from_glib_full(c_child);
1944
1945 Some(T::from_variant(&child))
1946 } else {
1947 Some(None)
1948 }
1949 } else {
1950 None
1951 }
1952 }
1953 }
1954}
1955
1956impl<T: StaticVariantType> StaticVariantType for [T] {
1957 fn static_variant_type() -> Cow<'static, VariantTy> {
1958 T::static_variant_type().as_array()
1959 }
1960}
1961
1962impl<T: StaticVariantType + ToVariant> ToVariant for [T] {
1963 fn to_variant(&self) -> Variant {
1964 unsafe {
1965 if self.is_empty() {
1966 return from_glib_none(ffi::g_variant_new_array(
1967 T::static_variant_type().to_glib_none().0,
1968 ptr::null(),
1969 0,
1970 ));
1971 }
1972
1973 let mut builder = mem::MaybeUninit::uninit();
1974 ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
1975 let mut builder = builder.assume_init();
1976 for value in self {
1977 let value = value.to_variant();
1978 ffi::g_variant_builder_add_value(&mut builder, value.to_glib_none().0);
1979 }
1980 from_glib_none(ffi::g_variant_builder_end(&mut builder))
1981 }
1982 }
1983}
1984
1985impl<T: StaticVariantType + ToVariant> From<&[T]> for Variant {
1986 #[inline]
1987 fn from(s: &[T]) -> Self {
1988 s.to_variant()
1989 }
1990}
1991
1992impl<T: FromVariant> FromVariant for Vec<T> {
1993 fn from_variant(variant: &Variant) -> Option<Self> {
1994 if !variant.is_container() {
1995 return None;
1996 }
1997
1998 let mut vec = Vec::with_capacity(variant.n_children());
1999
2000 for i in 0..variant.n_children() {
2001 match variant.child_value(i).get() {
2002 Some(child) => vec.push(child),
2003 None => return None,
2004 }
2005 }
2006
2007 Some(vec)
2008 }
2009}
2010
2011impl<T: StaticVariantType + ToVariant> ToVariant for Vec<T> {
2012 fn to_variant(&self) -> Variant {
2013 self.as_slice().to_variant()
2014 }
2015}
2016
2017impl<T: StaticVariantType + Into<Variant>> From<Vec<T>> for Variant {
2018 fn from(v: Vec<T>) -> Self {
2019 unsafe {
2020 if v.is_empty() {
2021 return from_glib_none(ffi::g_variant_new_array(
2022 T::static_variant_type().to_glib_none().0,
2023 ptr::null(),
2024 0,
2025 ));
2026 }
2027
2028 let mut builder = mem::MaybeUninit::uninit();
2029 ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
2030 let mut builder = builder.assume_init();
2031 for value in v {
2032 let value = value.into();
2033 ffi::g_variant_builder_add_value(&mut builder, value.to_glib_none().0);
2034 }
2035 from_glib_none(ffi::g_variant_builder_end(&mut builder))
2036 }
2037 }
2038}
2039
2040impl<T: StaticVariantType> StaticVariantType for Vec<T> {
2041 fn static_variant_type() -> Cow<'static, VariantTy> {
2042 <[T]>::static_variant_type()
2043 }
2044}
2045
2046impl<K, V, H> FromVariant for HashMap<K, V, H>
2047where
2048 K: FromVariant + Eq + Hash,
2049 V: FromVariant,
2050 H: BuildHasher + Default,
2051{
2052 fn from_variant(variant: &Variant) -> Option<Self> {
2053 if !variant.is_container() {
2054 return None;
2055 }
2056
2057 let mut map = HashMap::default();
2058
2059 for i in 0..variant.n_children() {
2060 let entry = variant.child_value(i);
2061 let key = entry.child_value(0).get()?;
2062 let val = entry.child_value(1).get()?;
2063
2064 map.insert(key, val);
2065 }
2066
2067 Some(map)
2068 }
2069}
2070
2071impl<K, V> FromVariant for BTreeMap<K, V>
2072where
2073 K: FromVariant + Eq + Ord,
2074 V: FromVariant,
2075{
2076 fn from_variant(variant: &Variant) -> Option<Self> {
2077 if !variant.is_container() {
2078 return None;
2079 }
2080
2081 let mut map = BTreeMap::default();
2082
2083 for i in 0..variant.n_children() {
2084 let entry = variant.child_value(i);
2085 let key = entry.child_value(0).get()?;
2086 let val = entry.child_value(1).get()?;
2087
2088 map.insert(key, val);
2089 }
2090
2091 Some(map)
2092 }
2093}
2094
2095impl<K, V> ToVariant for HashMap<K, V>
2096where
2097 K: StaticVariantType + ToVariant + Eq + Hash,
2098 V: StaticVariantType + ToVariant,
2099{
2100 fn to_variant(&self) -> Variant {
2101 unsafe {
2102 if self.is_empty() {
2103 return from_glib_none(ffi::g_variant_new_array(
2104 DictEntry::<K, V>::static_variant_type().to_glib_none().0,
2105 ptr::null(),
2106 0,
2107 ));
2108 }
2109
2110 let mut builder = mem::MaybeUninit::uninit();
2111 ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
2112 let mut builder = builder.assume_init();
2113 for (key, value) in self {
2114 let entry = DictEntry::new(key, value).to_variant();
2115 ffi::g_variant_builder_add_value(&mut builder, entry.to_glib_none().0);
2116 }
2117 from_glib_none(ffi::g_variant_builder_end(&mut builder))
2118 }
2119 }
2120}
2121
2122impl<K, V> From<HashMap<K, V>> for Variant
2123where
2124 K: StaticVariantType + Into<Variant> + Eq + Hash,
2125 V: StaticVariantType + Into<Variant>,
2126{
2127 fn from(m: HashMap<K, V>) -> Self {
2128 unsafe {
2129 if m.is_empty() {
2130 return from_glib_none(ffi::g_variant_new_array(
2131 DictEntry::<K, V>::static_variant_type().to_glib_none().0,
2132 ptr::null(),
2133 0,
2134 ));
2135 }
2136
2137 let mut builder = mem::MaybeUninit::uninit();
2138 ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
2139 let mut builder = builder.assume_init();
2140 for (key, value) in m {
2141 let entry = Variant::from(DictEntry::new(key, value));
2142 ffi::g_variant_builder_add_value(&mut builder, entry.to_glib_none().0);
2143 }
2144 from_glib_none(ffi::g_variant_builder_end(&mut builder))
2145 }
2146 }
2147}
2148
2149impl<K, V> ToVariant for BTreeMap<K, V>
2150where
2151 K: StaticVariantType + ToVariant + Eq + Hash,
2152 V: StaticVariantType + ToVariant,
2153{
2154 fn to_variant(&self) -> Variant {
2155 unsafe {
2156 if self.is_empty() {
2157 return from_glib_none(ffi::g_variant_new_array(
2158 DictEntry::<K, V>::static_variant_type().to_glib_none().0,
2159 ptr::null(),
2160 0,
2161 ));
2162 }
2163
2164 let mut builder = mem::MaybeUninit::uninit();
2165 ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
2166 let mut builder = builder.assume_init();
2167 for (key, value) in self {
2168 let entry = DictEntry::new(key, value).to_variant();
2169 ffi::g_variant_builder_add_value(&mut builder, entry.to_glib_none().0);
2170 }
2171 from_glib_none(ffi::g_variant_builder_end(&mut builder))
2172 }
2173 }
2174}
2175
2176impl<K, V> From<BTreeMap<K, V>> for Variant
2177where
2178 K: StaticVariantType + Into<Variant> + Eq + Hash,
2179 V: StaticVariantType + Into<Variant>,
2180{
2181 fn from(m: BTreeMap<K, V>) -> Self {
2182 unsafe {
2183 if m.is_empty() {
2184 return from_glib_none(ffi::g_variant_new_array(
2185 DictEntry::<K, V>::static_variant_type().to_glib_none().0,
2186 ptr::null(),
2187 0,
2188 ));
2189 }
2190
2191 let mut builder = mem::MaybeUninit::uninit();
2192 ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
2193 let mut builder = builder.assume_init();
2194 for (key, value) in m {
2195 let entry = Variant::from(DictEntry::new(key, value));
2196 ffi::g_variant_builder_add_value(&mut builder, entry.to_glib_none().0);
2197 }
2198 from_glib_none(ffi::g_variant_builder_end(&mut builder))
2199 }
2200 }
2201}
2202
2203/// A Dictionary entry.
2204///
2205/// While GVariant format allows a dictionary entry to be an independent type, typically you'll need
2206/// to use this in a dictionary, which is simply an array of dictionary entries. The following code
2207/// creates a dictionary:
2208///
2209/// ```
2210///# use glib::prelude::*; // or `use gtk::prelude::*;`
2211/// use glib::variant::{Variant, FromVariant, DictEntry};
2212///
2213/// let entries = [
2214/// DictEntry::new("uuid", 1000u32),
2215/// DictEntry::new("guid", 1001u32),
2216/// ];
2217/// let dict = entries.into_iter().collect::<Variant>();
2218/// assert_eq!(dict.n_children(), 2);
2219/// assert_eq!(dict.type_().as_str(), "a{su}");
2220/// ```
2221#[derive(Debug, Clone)]
2222pub struct DictEntry<K, V> {
2223 key: K,
2224 value: V,
2225}
2226
2227impl<K, V> DictEntry<K, V>
2228where
2229 K: StaticVariantType,
2230 V: StaticVariantType,
2231{
2232 pub fn new(key: K, value: V) -> Self {
2233 Self { key, value }
2234 }
2235
2236 pub fn key(&self) -> &K {
2237 &self.key
2238 }
2239
2240 pub fn value(&self) -> &V {
2241 &self.value
2242 }
2243}
2244
2245impl<K, V> FromVariant for DictEntry<K, V>
2246where
2247 K: FromVariant,
2248 V: FromVariant,
2249{
2250 fn from_variant(variant: &Variant) -> Option<Self> {
2251 if !variant.type_().is_subtype_of(VariantTy::DICT_ENTRY) {
2252 return None;
2253 }
2254
2255 let key = variant.child_value(0).get()?;
2256 let value = variant.child_value(1).get()?;
2257
2258 Some(Self { key, value })
2259 }
2260}
2261
2262impl<K, V> ToVariant for DictEntry<K, V>
2263where
2264 K: StaticVariantType + ToVariant,
2265 V: StaticVariantType + ToVariant,
2266{
2267 fn to_variant(&self) -> Variant {
2268 Variant::from_dict_entry(&self.key.to_variant(), &self.value.to_variant())
2269 }
2270}
2271
2272impl<K, V> From<DictEntry<K, V>> for Variant
2273where
2274 K: StaticVariantType + Into<Variant>,
2275 V: StaticVariantType + Into<Variant>,
2276{
2277 fn from(e: DictEntry<K, V>) -> Self {
2278 Variant::from_dict_entry(&e.key.into(), &e.value.into())
2279 }
2280}
2281
2282impl ToVariant for Variant {
2283 fn to_variant(&self) -> Variant {
2284 Variant::from_variant(self)
2285 }
2286}
2287
2288impl FromVariant for Variant {
2289 fn from_variant(variant: &Variant) -> Option<Self> {
2290 variant.as_variant()
2291 }
2292}
2293
2294impl<K: StaticVariantType, V: StaticVariantType> StaticVariantType for DictEntry<K, V> {
2295 fn static_variant_type() -> Cow<'static, VariantTy> {
2296 Cow::Owned(VariantType::new_dict_entry(
2297 &K::static_variant_type(),
2298 &V::static_variant_type(),
2299 ))
2300 }
2301}
2302
2303fn static_variant_mapping<K, V>() -> Cow<'static, VariantTy>
2304where
2305 K: StaticVariantType,
2306 V: StaticVariantType,
2307{
2308 use std::fmt::Write;
2309
2310 let key_type = K::static_variant_type();
2311 let value_type = V::static_variant_type();
2312
2313 if key_type == VariantTy::STRING && value_type == VariantTy::VARIANT {
2314 return Cow::Borrowed(VariantTy::VARDICT);
2315 }
2316
2317 let mut builder = crate::GStringBuilder::default();
2318 write!(builder, "a{{{}{}}}", key_type.as_str(), value_type.as_str()).unwrap();
2319
2320 Cow::Owned(VariantType::from_string(builder.into_string()).unwrap())
2321}
2322
2323impl<K, V, H> StaticVariantType for HashMap<K, V, H>
2324where
2325 K: StaticVariantType,
2326 V: StaticVariantType,
2327 H: BuildHasher + Default,
2328{
2329 fn static_variant_type() -> Cow<'static, VariantTy> {
2330 static_variant_mapping::<K, V>()
2331 }
2332}
2333
2334impl<K, V> StaticVariantType for BTreeMap<K, V>
2335where
2336 K: StaticVariantType,
2337 V: StaticVariantType,
2338{
2339 fn static_variant_type() -> Cow<'static, VariantTy> {
2340 static_variant_mapping::<K, V>()
2341 }
2342}
2343
2344macro_rules! tuple_impls {
2345 ($($len:expr => ($($n:tt $name:ident)+))+) => {
2346 $(
2347 impl<$($name),+> StaticVariantType for ($($name,)+)
2348 where
2349 $($name: StaticVariantType,)+
2350 {
2351 fn static_variant_type() -> Cow<'static, VariantTy> {
2352 Cow::Owned(VariantType::new_tuple(&[
2353 $(
2354 $name::static_variant_type(),
2355 )+
2356 ]))
2357 }
2358 }
2359
2360 impl<$($name),+> FromVariant for ($($name,)+)
2361 where
2362 $($name: FromVariant,)+
2363 {
2364 fn from_variant(variant: &Variant) -> Option<Self> {
2365 if !variant.type_().is_subtype_of(VariantTy::TUPLE) {
2366 return None;
2367 }
2368
2369 Some((
2370 $(
2371 match variant.try_child_get::<$name>($n) {
2372 Ok(Some(field)) => field,
2373 _ => return None,
2374 },
2375 )+
2376 ))
2377 }
2378 }
2379
2380 impl<$($name),+> ToVariant for ($($name,)+)
2381 where
2382 $($name: ToVariant,)+
2383 {
2384 fn to_variant(&self) -> Variant {
2385 unsafe {
2386 let mut builder = mem::MaybeUninit::uninit();
2387 ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::TUPLE.to_glib_none().0);
2388 let mut builder = builder.assume_init();
2389
2390 $(
2391 let field = self.$n.to_variant();
2392 ffi::g_variant_builder_add_value(&mut builder, field.to_glib_none().0);
2393 )+
2394
2395 from_glib_none(ffi::g_variant_builder_end(&mut builder))
2396 }
2397 }
2398 }
2399
2400 impl<$($name),+> From<($($name,)+)> for Variant
2401 where
2402 $($name: Into<Variant>,)+
2403 {
2404 fn from(t: ($($name,)+)) -> Self {
2405 unsafe {
2406 let mut builder = mem::MaybeUninit::uninit();
2407 ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::TUPLE.to_glib_none().0);
2408 let mut builder = builder.assume_init();
2409
2410 $(
2411 let field = t.$n.into();
2412 ffi::g_variant_builder_add_value(&mut builder, field.to_glib_none().0);
2413 )+
2414
2415 from_glib_none(ffi::g_variant_builder_end(&mut builder))
2416 }
2417 }
2418 }
2419 )+
2420 }
2421}
2422
2423tuple_impls! {
2424 1 => (0 T0)
2425 2 => (0 T0 1 T1)
2426 3 => (0 T0 1 T1 2 T2)
2427 4 => (0 T0 1 T1 2 T2 3 T3)
2428 5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
2429 6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
2430 7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
2431 8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
2432 9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
2433 10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
2434 11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
2435 12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
2436 13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
2437 14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
2438 15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
2439 16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
2440}
2441
2442impl<T: Into<Variant> + StaticVariantType> FromIterator<T> for Variant {
2443 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
2444 Variant::array_from_iter::<T>(iter.into_iter().map(|v| v.into()))
2445 }
2446}
2447
2448/// Trait for fixed size variant types.
2449pub unsafe trait FixedSizeVariantType: StaticVariantType + Sized + Copy {}
2450unsafe impl FixedSizeVariantType for u8 {}
2451unsafe impl FixedSizeVariantType for i16 {}
2452unsafe impl FixedSizeVariantType for u16 {}
2453unsafe impl FixedSizeVariantType for i32 {}
2454unsafe impl FixedSizeVariantType for u32 {}
2455unsafe impl FixedSizeVariantType for i64 {}
2456unsafe impl FixedSizeVariantType for u64 {}
2457unsafe impl FixedSizeVariantType for f64 {}
2458unsafe impl FixedSizeVariantType for bool {}
2459
2460/// Wrapper type for fixed size type arrays.
2461///
2462/// Converting this from/to a `Variant` is generally more efficient than working on the type
2463/// directly. This is especially important when deriving `Variant` trait implementations on custom
2464/// types.
2465///
2466/// This wrapper type can hold for example `Vec<u8>`, `Box<[u8]>` and similar types.
2467#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
2468pub struct FixedSizeVariantArray<A, T>(A, std::marker::PhantomData<T>)
2469where
2470 A: AsRef<[T]>,
2471 T: FixedSizeVariantType;
2472
2473impl<A: AsRef<[T]>, T: FixedSizeVariantType> From<A> for FixedSizeVariantArray<A, T> {
2474 fn from(array: A) -> Self {
2475 FixedSizeVariantArray(array, std::marker::PhantomData)
2476 }
2477}
2478
2479impl<A: AsRef<[T]>, T: FixedSizeVariantType> FixedSizeVariantArray<A, T> {
2480 pub fn into_inner(self) -> A {
2481 self.0
2482 }
2483}
2484
2485impl<A: AsRef<[T]>, T: FixedSizeVariantType> std::ops::Deref for FixedSizeVariantArray<A, T> {
2486 type Target = A;
2487
2488 #[inline]
2489 fn deref(&self) -> &Self::Target {
2490 &self.0
2491 }
2492}
2493
2494impl<A: AsRef<[T]>, T: FixedSizeVariantType> std::ops::DerefMut for FixedSizeVariantArray<A, T> {
2495 #[inline]
2496 fn deref_mut(&mut self) -> &mut Self::Target {
2497 &mut self.0
2498 }
2499}
2500
2501impl<A: AsRef<[T]>, T: FixedSizeVariantType> AsRef<A> for FixedSizeVariantArray<A, T> {
2502 #[inline]
2503 fn as_ref(&self) -> &A {
2504 &self.0
2505 }
2506}
2507
2508impl<A: AsRef<[T]>, T: FixedSizeVariantType> AsMut<A> for FixedSizeVariantArray<A, T> {
2509 #[inline]
2510 fn as_mut(&mut self) -> &mut A {
2511 &mut self.0
2512 }
2513}
2514
2515impl<A: AsRef<[T]>, T: FixedSizeVariantType> AsRef<[T]> for FixedSizeVariantArray<A, T> {
2516 #[inline]
2517 fn as_ref(&self) -> &[T] {
2518 self.0.as_ref()
2519 }
2520}
2521
2522impl<A: AsRef<[T]> + AsMut<[T]>, T: FixedSizeVariantType> AsMut<[T]>
2523 for FixedSizeVariantArray<A, T>
2524{
2525 #[inline]
2526 fn as_mut(&mut self) -> &mut [T] {
2527 self.0.as_mut()
2528 }
2529}
2530
2531impl<A: AsRef<[T]>, T: FixedSizeVariantType> StaticVariantType for FixedSizeVariantArray<A, T> {
2532 fn static_variant_type() -> Cow<'static, VariantTy> {
2533 <[T]>::static_variant_type()
2534 }
2535}
2536
2537impl<A: AsRef<[T]> + for<'a> From<&'a [T]>, T: FixedSizeVariantType> FromVariant
2538 for FixedSizeVariantArray<A, T>
2539{
2540 fn from_variant(variant: &Variant) -> Option<Self> {
2541 Some(FixedSizeVariantArray(
2542 A::from(variant.fixed_array::<T>().ok()?),
2543 std::marker::PhantomData,
2544 ))
2545 }
2546}
2547
2548impl<A: AsRef<[T]>, T: FixedSizeVariantType> ToVariant for FixedSizeVariantArray<A, T> {
2549 fn to_variant(&self) -> Variant {
2550 Variant::array_from_fixed_array(self.0.as_ref())
2551 }
2552}
2553
2554impl<A: AsRef<[T]>, T: FixedSizeVariantType> From<FixedSizeVariantArray<A, T>> for Variant {
2555 #[doc(alias = "g_variant_new_from_data")]
2556 fn from(a: FixedSizeVariantArray<A, T>) -> Self {
2557 unsafe {
2558 let data = Box::new(a.0);
2559 let (data_ptr, len) = {
2560 let data = (*data).as_ref();
2561 (data.as_ptr(), mem::size_of_val(data))
2562 };
2563
2564 unsafe extern "C" fn free_data<A: AsRef<[T]>, T: FixedSizeVariantType>(
2565 ptr: ffi::gpointer,
2566 ) {
2567 unsafe {
2568 let _ = Box::from_raw(ptr as *mut A);
2569 }
2570 }
2571
2572 from_glib_none(ffi::g_variant_new_from_data(
2573 T::static_variant_type().to_glib_none().0,
2574 data_ptr as ffi::gconstpointer,
2575 len,
2576 false.into_glib(),
2577 Some(free_data::<A, T>),
2578 Box::into_raw(data) as ffi::gpointer,
2579 ))
2580 }
2581 }
2582}
2583
2584/// A wrapper type around `Variant` handles.
2585#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2586pub struct Handle(pub i32);
2587
2588impl From<i32> for Handle {
2589 fn from(v: i32) -> Self {
2590 Handle(v)
2591 }
2592}
2593
2594impl From<Handle> for i32 {
2595 fn from(v: Handle) -> Self {
2596 v.0
2597 }
2598}
2599
2600impl StaticVariantType for Handle {
2601 fn static_variant_type() -> Cow<'static, VariantTy> {
2602 Cow::Borrowed(VariantTy::HANDLE)
2603 }
2604}
2605
2606impl ToVariant for Handle {
2607 fn to_variant(&self) -> Variant {
2608 unsafe { from_glib_none(ffi::g_variant_new_handle(self.0)) }
2609 }
2610}
2611
2612impl From<Handle> for Variant {
2613 #[inline]
2614 fn from(h: Handle) -> Self {
2615 h.to_variant()
2616 }
2617}
2618
2619impl FromVariant for Handle {
2620 fn from_variant(variant: &Variant) -> Option<Self> {
2621 unsafe {
2622 if variant.is::<Self>() {
2623 Some(Handle(ffi::g_variant_get_handle(variant.to_glib_none().0)))
2624 } else {
2625 None
2626 }
2627 }
2628 }
2629}
2630
2631/// A wrapper type around `Variant` object paths.
2632///
2633/// Values of these type are guaranteed to be valid object paths.
2634#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2635pub struct ObjectPath(String);
2636
2637impl ObjectPath {
2638 pub fn as_str(&self) -> &str {
2639 &self.0
2640 }
2641}
2642
2643impl Display for ObjectPath {
2644 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2645 self.0.fmt(f)
2646 }
2647}
2648
2649impl std::ops::Deref for ObjectPath {
2650 type Target = str;
2651
2652 #[inline]
2653 fn deref(&self) -> &Self::Target {
2654 &self.0
2655 }
2656}
2657
2658impl TryFrom<String> for ObjectPath {
2659 type Error = crate::BoolError;
2660
2661 fn try_from(v: String) -> Result<Self, Self::Error> {
2662 if !Variant::is_object_path(&v) {
2663 return Err(bool_error!("Invalid object path"));
2664 }
2665
2666 Ok(ObjectPath(v))
2667 }
2668}
2669
2670impl<'a> TryFrom<&'a str> for ObjectPath {
2671 type Error = crate::BoolError;
2672
2673 fn try_from(v: &'a str) -> Result<Self, Self::Error> {
2674 ObjectPath::try_from(String::from(v))
2675 }
2676}
2677
2678impl From<ObjectPath> for String {
2679 fn from(v: ObjectPath) -> Self {
2680 v.0
2681 }
2682}
2683
2684impl StaticVariantType for ObjectPath {
2685 fn static_variant_type() -> Cow<'static, VariantTy> {
2686 Cow::Borrowed(VariantTy::OBJECT_PATH)
2687 }
2688}
2689
2690impl ToVariant for ObjectPath {
2691 fn to_variant(&self) -> Variant {
2692 unsafe { from_glib_none(ffi::g_variant_new_object_path(self.0.to_glib_none().0)) }
2693 }
2694}
2695
2696impl From<ObjectPath> for Variant {
2697 #[inline]
2698 fn from(p: ObjectPath) -> Self {
2699 let mut s = p.0;
2700 s.push('\0');
2701 unsafe { Self::from_data_trusted::<ObjectPath, _>(s) }
2702 }
2703}
2704
2705impl FromVariant for ObjectPath {
2706 #[allow(unused_unsafe)]
2707 fn from_variant(variant: &Variant) -> Option<Self> {
2708 unsafe {
2709 if variant.is::<Self>() {
2710 Some(ObjectPath(String::from(variant.str().unwrap())))
2711 } else {
2712 None
2713 }
2714 }
2715 }
2716}
2717
2718/// A wrapper type around `Variant` signatures.
2719///
2720/// Values of these type are guaranteed to be valid signatures.
2721#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2722pub struct Signature(String);
2723
2724impl Signature {
2725 pub fn as_str(&self) -> &str {
2726 &self.0
2727 }
2728}
2729
2730impl std::ops::Deref for Signature {
2731 type Target = str;
2732
2733 #[inline]
2734 fn deref(&self) -> &Self::Target {
2735 &self.0
2736 }
2737}
2738
2739impl TryFrom<String> for Signature {
2740 type Error = crate::BoolError;
2741
2742 fn try_from(v: String) -> Result<Self, Self::Error> {
2743 if !Variant::is_signature(&v) {
2744 return Err(bool_error!("Invalid signature"));
2745 }
2746
2747 Ok(Signature(v))
2748 }
2749}
2750
2751impl<'a> TryFrom<&'a str> for Signature {
2752 type Error = crate::BoolError;
2753
2754 fn try_from(v: &'a str) -> Result<Self, Self::Error> {
2755 Signature::try_from(String::from(v))
2756 }
2757}
2758
2759impl From<Signature> for String {
2760 fn from(v: Signature) -> Self {
2761 v.0
2762 }
2763}
2764
2765impl StaticVariantType for Signature {
2766 fn static_variant_type() -> Cow<'static, VariantTy> {
2767 Cow::Borrowed(VariantTy::SIGNATURE)
2768 }
2769}
2770
2771impl ToVariant for Signature {
2772 fn to_variant(&self) -> Variant {
2773 unsafe { from_glib_none(ffi::g_variant_new_signature(self.0.to_glib_none().0)) }
2774 }
2775}
2776
2777impl From<Signature> for Variant {
2778 #[inline]
2779 fn from(s: Signature) -> Self {
2780 let mut s = s.0;
2781 s.push('\0');
2782 unsafe { Self::from_data_trusted::<Signature, _>(s) }
2783 }
2784}
2785
2786impl FromVariant for Signature {
2787 #[allow(unused_unsafe)]
2788 fn from_variant(variant: &Variant) -> Option<Self> {
2789 unsafe {
2790 if variant.is::<Self>() {
2791 Some(Signature(String::from(variant.str().unwrap())))
2792 } else {
2793 None
2794 }
2795 }
2796 }
2797}
2798
2799#[cfg(test)]
2800mod tests {
2801 use std::collections::{HashMap, HashSet};
2802
2803 use super::*;
2804
2805 macro_rules! unsigned {
2806 ($name:ident, $ty:ident) => {
2807 #[test]
2808 fn $name() {
2809 let mut n = $ty::MAX;
2810 while n > 0 {
2811 let v = n.to_variant();
2812 assert_eq!(v.get(), Some(n));
2813 n /= 2;
2814 }
2815 }
2816 };
2817 }
2818
2819 macro_rules! signed {
2820 ($name:ident, $ty:ident) => {
2821 #[test]
2822 fn $name() {
2823 let mut n = $ty::MAX;
2824 while n > 0 {
2825 let v = n.to_variant();
2826 assert_eq!(v.get(), Some(n));
2827 let v = (-n).to_variant();
2828 assert_eq!(v.get(), Some(-n));
2829 n /= 2;
2830 }
2831 }
2832 };
2833 }
2834
2835 unsigned!(test_u8, u8);
2836 unsigned!(test_u16, u16);
2837 unsigned!(test_u32, u32);
2838 unsigned!(test_u64, u64);
2839 signed!(test_i16, i16);
2840 signed!(test_i32, i32);
2841 signed!(test_i64, i64);
2842
2843 #[test]
2844 fn test_str() {
2845 let s = "this is a test";
2846 let v = s.to_variant();
2847 assert_eq!(v.str(), Some(s));
2848 assert_eq!(42u32.to_variant().str(), None);
2849 }
2850
2851 #[test]
2852 fn test_fixed_array() {
2853 let b = b"this is a test";
2854 let v = Variant::array_from_fixed_array(&b[..]);
2855 assert_eq!(v.type_().as_str(), "ay");
2856 assert_eq!(v.fixed_array::<u8>().unwrap(), b);
2857 assert!(42u32.to_variant().fixed_array::<u8>().is_err());
2858
2859 let b = [1u32, 10u32, 100u32];
2860 let v = Variant::array_from_fixed_array(&b);
2861 assert_eq!(v.type_().as_str(), "au");
2862 assert_eq!(v.fixed_array::<u32>().unwrap(), b);
2863 assert!(v.fixed_array::<u8>().is_err());
2864
2865 let b = [true, false, true];
2866 let v = Variant::array_from_fixed_array(&b);
2867 assert_eq!(v.type_().as_str(), "ab");
2868 assert_eq!(v.fixed_array::<bool>().unwrap(), b);
2869 assert!(v.fixed_array::<u8>().is_err());
2870
2871 let b = [1.0f64, 2.0f64, 3.0f64];
2872 let v = Variant::array_from_fixed_array(&b);
2873 assert_eq!(v.type_().as_str(), "ad");
2874 #[allow(clippy::float_cmp)]
2875 {
2876 assert_eq!(v.fixed_array::<f64>().unwrap(), b);
2877 }
2878 assert!(v.fixed_array::<u64>().is_err());
2879 }
2880
2881 #[test]
2882 fn test_fixed_variant_array() {
2883 let b = FixedSizeVariantArray::from(&b"this is a test"[..]);
2884 let v = b.to_variant();
2885 assert_eq!(v.type_().as_str(), "ay");
2886 assert_eq!(
2887 &*v.get::<FixedSizeVariantArray<Vec<u8>, u8>>().unwrap(),
2888 &*b
2889 );
2890
2891 let b = FixedSizeVariantArray::from(vec![1i32, 2, 3]);
2892 let v = b.to_variant();
2893 assert_eq!(v.type_().as_str(), "ai");
2894 assert_eq!(v.get::<FixedSizeVariantArray<Vec<i32>, i32>>().unwrap(), b);
2895 }
2896
2897 #[test]
2898 fn test_string() {
2899 let s = String::from("this is a test");
2900 let v = s.to_variant();
2901 assert_eq!(v.get(), Some(s));
2902 assert_eq!(v.normal_form(), v);
2903 }
2904
2905 #[test]
2906 fn test_eq() {
2907 let v1 = "this is a test".to_variant();
2908 let v2 = "this is a test".to_variant();
2909 let v3 = "test".to_variant();
2910 assert_eq!(v1, v2);
2911 assert_ne!(v1, v3);
2912 }
2913
2914 #[test]
2915 fn test_hash() {
2916 let v1 = "this is a test".to_variant();
2917 let v2 = "this is a test".to_variant();
2918 let v3 = "test".to_variant();
2919 let mut set = HashSet::new();
2920 set.insert(v1);
2921 assert!(set.contains(&v2));
2922 assert!(!set.contains(&v3));
2923
2924 assert_eq!(
2925 <HashMap<&str, (&str, u8, u32)>>::static_variant_type().as_str(),
2926 "a{s(syu)}"
2927 );
2928 }
2929
2930 #[test]
2931 fn test_array() {
2932 assert_eq!(<Vec<&str>>::static_variant_type().as_str(), "as");
2933 assert_eq!(
2934 <Vec<(&str, u8, u32)>>::static_variant_type().as_str(),
2935 "a(syu)"
2936 );
2937 let a = ["foo", "bar", "baz"].to_variant();
2938 assert_eq!(a.normal_form(), a);
2939 assert_eq!(a.array_iter_str().unwrap().len(), 3);
2940 let o = 0u32.to_variant();
2941 assert!(o.array_iter_str().is_err());
2942 }
2943
2944 #[test]
2945 fn test_array_from_iter() {
2946 let a = Variant::array_from_iter::<String>(
2947 ["foo", "bar", "baz"].into_iter().map(|s| s.to_variant()),
2948 );
2949 assert_eq!(a.type_().as_str(), "as");
2950 assert_eq!(a.n_children(), 3);
2951
2952 assert_eq!(a.try_child_get::<String>(0), Ok(Some(String::from("foo"))));
2953 assert_eq!(a.try_child_get::<String>(1), Ok(Some(String::from("bar"))));
2954 assert_eq!(a.try_child_get::<String>(2), Ok(Some(String::from("baz"))));
2955 }
2956
2957 #[test]
2958 fn test_array_collect() {
2959 let a = ["foo", "bar", "baz"].into_iter().collect::<Variant>();
2960 assert_eq!(a.type_().as_str(), "as");
2961 assert_eq!(a.n_children(), 3);
2962
2963 assert_eq!(a.try_child_get::<String>(0), Ok(Some(String::from("foo"))));
2964 assert_eq!(a.try_child_get::<String>(1), Ok(Some(String::from("bar"))));
2965 assert_eq!(a.try_child_get::<String>(2), Ok(Some(String::from("baz"))));
2966 }
2967
2968 #[test]
2969 fn test_tuple() {
2970 assert_eq!(<(&str, u32)>::static_variant_type().as_str(), "(su)");
2971 assert_eq!(<(&str, u8, u32)>::static_variant_type().as_str(), "(syu)");
2972 let a = ("test", 1u8, 2u32).to_variant();
2973 assert_eq!(a.normal_form(), a);
2974 assert_eq!(a.try_child_get::<String>(0), Ok(Some(String::from("test"))));
2975 assert_eq!(a.try_child_get::<u8>(1), Ok(Some(1u8)));
2976 assert_eq!(a.try_child_get::<u32>(2), Ok(Some(2u32)));
2977 assert_eq!(
2978 a.try_get::<(String, u8, u32)>(),
2979 Ok((String::from("test"), 1u8, 2u32))
2980 );
2981 }
2982
2983 #[test]
2984 fn test_tuple_from_iter() {
2985 let a = Variant::tuple_from_iter(["foo".to_variant(), 1u8.to_variant(), 2i32.to_variant()]);
2986 assert_eq!(a.type_().as_str(), "(syi)");
2987 assert_eq!(a.n_children(), 3);
2988
2989 assert_eq!(a.try_child_get::<String>(0), Ok(Some(String::from("foo"))));
2990 assert_eq!(a.try_child_get::<u8>(1), Ok(Some(1u8)));
2991 assert_eq!(a.try_child_get::<i32>(2), Ok(Some(2i32)));
2992 }
2993
2994 #[test]
2995 fn test_empty() {
2996 assert_eq!(<()>::static_variant_type().as_str(), "()");
2997 let a = ().to_variant();
2998 assert_eq!(a.type_().as_str(), "()");
2999 assert_eq!(a.get::<()>(), Some(()));
3000 }
3001
3002 #[test]
3003 fn test_maybe() {
3004 assert!(<Option<()>>::static_variant_type().is_maybe());
3005 let m1 = Some(()).to_variant();
3006 assert_eq!(m1.type_().as_str(), "m()");
3007
3008 assert_eq!(m1.get::<Option<()>>(), Some(Some(())));
3009 assert!(m1.as_maybe().is_some());
3010
3011 let m2 = None::<()>.to_variant();
3012 assert!(m2.as_maybe().is_none());
3013 }
3014
3015 #[test]
3016 fn test_btreemap() {
3017 assert_eq!(
3018 <BTreeMap<String, u32>>::static_variant_type().as_str(),
3019 "a{su}"
3020 );
3021 // Validate that BTreeMap adds entries to dict in sorted order
3022 let mut m = BTreeMap::new();
3023 let total = 20;
3024 for n in 0..total {
3025 let k = format!("v{n:04}");
3026 m.insert(k, n as u32);
3027 }
3028 let v = m.to_variant();
3029 let n = v.n_children();
3030 assert_eq!(total, n);
3031 for n in 0..total {
3032 let child = v
3033 .try_child_get::<DictEntry<String, u32>>(n)
3034 .unwrap()
3035 .unwrap();
3036 assert_eq!(*child.value(), n as u32);
3037 }
3038
3039 assert_eq!(BTreeMap::from_variant(&v).unwrap(), m);
3040 }
3041
3042 #[test]
3043 fn test_get() -> Result<(), Box<dyn std::error::Error>> {
3044 let u = 42u32.to_variant();
3045 assert!(u.get::<i32>().is_none());
3046 assert_eq!(u.get::<u32>().unwrap(), 42);
3047 assert!(u.try_get::<i32>().is_err());
3048 // Test ? conversion
3049 assert_eq!(u.try_get::<u32>()?, 42);
3050 Ok(())
3051 }
3052
3053 #[test]
3054 fn test_byteswap() {
3055 let u = 42u32.to_variant();
3056 assert_eq!(u.byteswap().get::<u32>().unwrap(), 704643072u32);
3057 assert_eq!(u.byteswap().byteswap().get::<u32>().unwrap(), 42u32);
3058 }
3059
3060 #[test]
3061 fn test_try_child() {
3062 let a = ["foo"].to_variant();
3063 assert!(a.try_child_value(0).is_some());
3064 assert_eq!(a.try_child_get::<String>(0).unwrap().unwrap(), "foo");
3065 assert_eq!(a.child_get::<String>(0), "foo");
3066 assert!(a.try_child_get::<u32>(0).is_err());
3067 assert!(a.try_child_value(1).is_none());
3068 assert!(a.try_child_get::<String>(1).unwrap().is_none());
3069 let u = 42u32.to_variant();
3070 assert!(u.try_child_value(0).is_none());
3071 assert!(u.try_child_get::<String>(0).unwrap().is_none());
3072 }
3073
3074 #[test]
3075 fn test_serialize() {
3076 let a = ("test", 1u8, 2u32).to_variant();
3077
3078 let bytes = a.data_as_bytes();
3079 let data = a.data();
3080 let len = a.size();
3081 assert_eq!(bytes.len(), len);
3082 assert_eq!(data.len(), len);
3083
3084 let mut store_data = vec![0u8; len];
3085 assert_eq!(a.store(&mut store_data).unwrap(), len);
3086
3087 assert_eq!(&bytes, data);
3088 assert_eq!(&store_data, data);
3089
3090 let b = Variant::from_data::<(String, u8, u32), _>(store_data);
3091 assert_eq!(a, b);
3092
3093 let c = Variant::from_bytes::<(String, u8, u32)>(&bytes);
3094 assert_eq!(a, c);
3095 }
3096
3097 #[test]
3098 fn test_print_parse() {
3099 let a = ("test", 1u8, 2u32).to_variant();
3100
3101 let a2 = Variant::parse(Some(a.type_()), &a.print(false)).unwrap();
3102 assert_eq!(a, a2);
3103
3104 let a3: Variant = a.to_string().parse().unwrap();
3105 assert_eq!(a, a3);
3106 }
3107
3108 #[cfg(any(unix, windows))]
3109 #[test]
3110 fn test_paths() {
3111 use std::path::PathBuf;
3112
3113 let path = PathBuf::from("foo");
3114 let v = path.to_variant();
3115 assert_eq!(PathBuf::from_variant(&v), Some(path));
3116 }
3117
3118 #[test]
3119 fn test_regression_from_variant_panics() {
3120 let variant = "text".to_variant();
3121 let hashmap: Option<HashMap<u64, u64>> = FromVariant::from_variant(&variant);
3122 assert!(hashmap.is_none());
3123
3124 let variant = HashMap::<u64, u64>::new().to_variant();
3125 let hashmap: Option<HashMap<u64, u64>> = FromVariant::from_variant(&variant);
3126 assert!(hashmap.is_some());
3127 }
3128}