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    /// - `G_VARIANT_TYPE_INT16` (etc.): #gint16 (etc.)
721    /// - `G_VARIANT_TYPE_BOOLEAN`: #guchar (not #gboolean!)
722    /// - `G_VARIANT_TYPE_BYTE`: #guint8
723    /// - `G_VARIANT_TYPE_HANDLE`: #guint32
724    /// - `G_VARIANT_TYPE_DOUBLE`: #gdouble
725    ///
726    /// For example, if calling this function for an array of 32-bit integers,
727    /// you might say `sizeof(gint32)`. This value isn't used except for the purpose
728    /// of a double-check that the form of the serialized data matches the caller's
729    /// expectation.
730    ///
731    /// @n_elements, which must be non-[`None`], is set equal to the number of
732    /// items in the array.
733    /// ## `element_size`
734    /// the size of each element
735    ///
736    /// # Returns
737    ///
738    /// a pointer to
739    ///     the fixed array
740    #[doc(alias = "g_variant_get_fixed_array")]
741    pub fn fixed_array<T: FixedSizeVariantType>(&self) -> Result<&[T], VariantTypeMismatchError> {
742        unsafe {
743            let expected_ty = T::static_variant_type().as_array();
744            if self.type_() != expected_ty {
745                return Err(VariantTypeMismatchError {
746                    actual: self.type_().to_owned(),
747                    expected: expected_ty.into_owned(),
748                });
749            }
750
751            let mut n_elements = mem::MaybeUninit::uninit();
752            let ptr = ffi::g_variant_get_fixed_array(
753                self.to_glib_none().0,
754                n_elements.as_mut_ptr(),
755                mem::size_of::<T>(),
756            );
757
758            let n_elements = n_elements.assume_init();
759            if n_elements == 0 {
760                Ok(&[])
761            } else {
762                debug_assert!(!ptr.is_null());
763                Ok(slice::from_raw_parts(ptr as *const T, n_elements))
764            }
765        }
766    }
767
768    // rustdoc-stripper-ignore-next
769    /// Creates a new Variant array from children.
770    ///
771    /// # Panics
772    ///
773    /// This function panics if not all variants are of type `T`.
774    #[doc(alias = "g_variant_new_array")]
775    pub fn array_from_iter<T: StaticVariantType>(
776        children: impl IntoIterator<Item = Variant>,
777    ) -> Self {
778        Self::array_from_iter_with_type(&T::static_variant_type(), children)
779    }
780
781    // rustdoc-stripper-ignore-next
782    /// Creates a new Variant array from children with the specified type.
783    ///
784    /// # Panics
785    ///
786    /// This function panics if not all variants are of type `type_`.
787    #[doc(alias = "g_variant_new_array")]
788    pub fn array_from_iter_with_type(
789        type_: &VariantTy,
790        children: impl IntoIterator<Item = impl AsRef<Variant>>,
791    ) -> Self {
792        unsafe {
793            let mut builder = mem::MaybeUninit::uninit();
794            ffi::g_variant_builder_init(builder.as_mut_ptr(), type_.as_array().to_glib_none().0);
795            let mut builder = builder.assume_init();
796            for value in children.into_iter() {
797                let value = value.as_ref();
798                if ffi::g_variant_is_of_type(value.to_glib_none().0, type_.to_glib_none().0)
799                    == ffi::GFALSE
800                {
801                    ffi::g_variant_builder_clear(&mut builder);
802                    assert!(value.is_type(type_));
803                }
804
805                ffi::g_variant_builder_add_value(&mut builder, value.to_glib_none().0);
806            }
807            from_glib_none(ffi::g_variant_builder_end(&mut builder))
808        }
809    }
810
811    // rustdoc-stripper-ignore-next
812    /// Creates a new Variant array from a fixed array.
813    #[doc(alias = "g_variant_new_fixed_array")]
814    pub fn array_from_fixed_array<T: FixedSizeVariantType>(array: &[T]) -> Self {
815        let type_ = T::static_variant_type();
816
817        unsafe {
818            from_glib_none(ffi::g_variant_new_fixed_array(
819                type_.as_ptr(),
820                array.as_ptr() as ffi::gconstpointer,
821                array.len(),
822                mem::size_of::<T>(),
823            ))
824        }
825    }
826
827    // rustdoc-stripper-ignore-next
828    /// Creates a new Variant tuple from children.
829    #[doc(alias = "g_variant_new_tuple")]
830    pub fn tuple_from_iter(children: impl IntoIterator<Item = impl AsRef<Variant>>) -> Self {
831        unsafe {
832            let mut builder = mem::MaybeUninit::uninit();
833            ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::TUPLE.to_glib_none().0);
834            let mut builder = builder.assume_init();
835            for value in children.into_iter() {
836                ffi::g_variant_builder_add_value(&mut builder, value.as_ref().to_glib_none().0);
837            }
838            from_glib_none(ffi::g_variant_builder_end(&mut builder))
839        }
840    }
841
842    // rustdoc-stripper-ignore-next
843    /// Creates a new dictionary entry Variant.
844    ///
845    /// [DictEntry] should be preferred over this when the types are known statically.
846    #[doc(alias = "g_variant_new_dict_entry")]
847    pub fn from_dict_entry(key: &Variant, value: &Variant) -> Self {
848        unsafe {
849            from_glib_none(ffi::g_variant_new_dict_entry(
850                key.to_glib_none().0,
851                value.to_glib_none().0,
852            ))
853        }
854    }
855
856    // rustdoc-stripper-ignore-next
857    /// Creates a new maybe Variant.
858    #[doc(alias = "g_variant_new_maybe")]
859    pub fn from_maybe<T: StaticVariantType>(child: Option<&Variant>) -> Self {
860        let type_ = T::static_variant_type();
861        match child {
862            Some(child) => {
863                assert_eq!(type_, child.type_());
864
865                Self::from_some(child)
866            }
867            None => Self::from_none(&type_),
868        }
869    }
870
871    // rustdoc-stripper-ignore-next
872    /// Creates a new maybe Variant from a child.
873    #[doc(alias = "g_variant_new_maybe")]
874    pub fn from_some(child: &Variant) -> Self {
875        unsafe {
876            from_glib_none(ffi::g_variant_new_maybe(
877                ptr::null(),
878                child.to_glib_none().0,
879            ))
880        }
881    }
882
883    // rustdoc-stripper-ignore-next
884    /// Creates a new maybe Variant with Nothing.
885    #[doc(alias = "g_variant_new_maybe")]
886    pub fn from_none(type_: &VariantTy) -> Self {
887        unsafe {
888            from_glib_none(ffi::g_variant_new_maybe(
889                type_.to_glib_none().0,
890                ptr::null_mut(),
891            ))
892        }
893    }
894
895    // rustdoc-stripper-ignore-next
896    /// Extract the value of a maybe Variant.
897    ///
898    /// Returns the child value, or `None` if the value is Nothing.
899    ///
900    /// # Panics
901    ///
902    /// Panics if the variant is not maybe-typed.
903    #[inline]
904    pub fn as_maybe(&self) -> Option<Variant> {
905        assert!(self.type_().is_maybe());
906
907        unsafe { from_glib_full(ffi::g_variant_get_maybe(self.to_glib_none().0)) }
908    }
909
910    // rustdoc-stripper-ignore-next
911    /// Pretty-print the contents of this variant in a human-readable form.
912    ///
913    /// A variant can be recreated from this output via [`Variant::parse`].
914    // rustdoc-stripper-ignore-next-stop
915    /// Pretty-prints @self in the format understood by g_variant_parse().
916    ///
917    /// The format is described [here](gvariant-text-format.html).
918    ///
919    /// If @type_annotate is [`true`], then type information is included in
920    /// the output.
921    /// ## `type_annotate`
922    /// [`true`] if type information should be included in
923    ///                 the output
924    ///
925    /// # Returns
926    ///
927    /// a newly-allocated string holding the result.
928    #[doc(alias = "g_variant_print")]
929    pub fn print(&self, type_annotate: bool) -> crate::GString {
930        unsafe {
931            from_glib_full(ffi::g_variant_print(
932                self.to_glib_none().0,
933                type_annotate.into_glib(),
934            ))
935        }
936    }
937
938    // rustdoc-stripper-ignore-next
939    /// Parses a GVariant from the text representation produced by [`print()`](Self::print).
940    #[doc(alias = "g_variant_parse")]
941    pub fn parse(type_: Option<&VariantTy>, text: &str) -> Result<Self, crate::Error> {
942        unsafe {
943            let mut error = ptr::null_mut();
944            let text = text.as_bytes().as_ptr_range();
945            let variant = ffi::g_variant_parse(
946                type_.to_glib_none().0,
947                text.start as *const _,
948                text.end as *const _,
949                ptr::null_mut(),
950                &mut error,
951            );
952            if variant.is_null() {
953                debug_assert!(!error.is_null());
954                Err(from_glib_full(error))
955            } else {
956                debug_assert!(error.is_null());
957                Ok(from_glib_full(variant))
958            }
959        }
960    }
961
962    // rustdoc-stripper-ignore-next
963    /// Constructs a new serialized-mode GVariant instance.
964    // rustdoc-stripper-ignore-next-stop
965    /// Constructs a new serialized-mode #GVariant instance.  This is the
966    /// inner interface for creation of new serialized values that gets
967    /// called from various functions in gvariant.c.
968    ///
969    /// A reference is taken on @bytes.
970    ///
971    /// The data in @bytes must be aligned appropriately for the @type_ being loaded.
972    /// Otherwise this function will internally create a copy of the memory (since
973    /// GLib 2.60) or (in older versions) fail and exit the process.
974    /// ## `type_`
975    /// a #GVariantType
976    /// ## `bytes`
977    /// a #GBytes
978    /// ## `trusted`
979    /// if the contents of @bytes are trusted
980    ///
981    /// # Returns
982    ///
983    /// a new #GVariant with a floating reference
984    #[doc(alias = "g_variant_new_from_bytes")]
985    pub fn from_bytes<T: StaticVariantType>(bytes: &Bytes) -> Self {
986        Variant::from_bytes_with_type(bytes, &T::static_variant_type())
987    }
988
989    // rustdoc-stripper-ignore-next
990    /// Constructs a new serialized-mode GVariant instance.
991    ///
992    /// This is the same as `from_bytes`, except that checks on the passed
993    /// data are skipped.
994    ///
995    /// You should not use this function on data from external sources.
996    ///
997    /// # Safety
998    ///
999    /// Since the data is not validated, this is potentially dangerous if called
1000    /// on bytes which are not guaranteed to have come from serialising another
1001    /// Variant.  The caller is responsible for ensuring bad data is not passed in.
1002    pub unsafe fn from_bytes_trusted<T: StaticVariantType>(bytes: &Bytes) -> Self {
1003        unsafe { Variant::from_bytes_with_type_trusted(bytes, &T::static_variant_type()) }
1004    }
1005
1006    // rustdoc-stripper-ignore-next
1007    /// Constructs a new serialized-mode GVariant instance.
1008    // rustdoc-stripper-ignore-next-stop
1009    /// Creates a new #GVariant instance from serialized data.
1010    ///
1011    /// @type_ is the type of #GVariant instance that will be constructed.
1012    /// The interpretation of @data depends on knowing the type.
1013    ///
1014    /// @data is not modified by this function and must remain valid with an
1015    /// unchanging value until such a time as @notify is called with
1016    /// @user_data.  If the contents of @data change before that time then
1017    /// the result is undefined.
1018    ///
1019    /// If @data is trusted to be serialized data in normal form then
1020    /// @trusted should be [`true`].  This applies to serialized data created
1021    /// within this process or read from a trusted location on the disk (such
1022    /// as a file installed in /usr/lib alongside your application).  You
1023    /// should set trusted to [`false`] if @data is read from the network, a
1024    /// file in the user's home directory, etc.
1025    ///
1026    /// If @data was not stored in this machine's native endianness, any multi-byte
1027    /// numeric values in the returned variant will also be in non-native
1028    /// endianness. g_variant_byteswap() can be used to recover the original values.
1029    ///
1030    /// @notify will be called with @user_data when @data is no longer
1031    /// needed.  The exact time of this call is unspecified and might even be
1032    /// before this function returns.
1033    ///
1034    /// Note: @data must be backed by memory that is aligned appropriately for the
1035    /// @type_ being loaded. Otherwise this function will internally create a copy of
1036    /// the memory (since GLib 2.60) or (in older versions) fail and exit the
1037    /// process.
1038    /// ## `type_`
1039    /// a definite #GVariantType
1040    /// ## `data`
1041    /// the serialized data
1042    /// ## `trusted`
1043    /// [`true`] if @data is definitely in normal form
1044    /// ## `notify`
1045    /// function to call when @data is no longer needed
1046    ///
1047    /// # Returns
1048    ///
1049    /// a new floating #GVariant of type @type_
1050    #[doc(alias = "g_variant_new_from_data")]
1051    pub fn from_data<T: StaticVariantType, A: AsRef<[u8]>>(data: A) -> Self {
1052        Variant::from_data_with_type(data, &T::static_variant_type())
1053    }
1054
1055    // rustdoc-stripper-ignore-next
1056    /// Constructs a new serialized-mode GVariant instance.
1057    ///
1058    /// This is the same as `from_data`, except that checks on the passed
1059    /// data are skipped.
1060    ///
1061    /// You should not use this function on data from external sources.
1062    ///
1063    /// # Safety
1064    ///
1065    /// Since the data is not validated, this is potentially dangerous if called
1066    /// on bytes which are not guaranteed to have come from serialising another
1067    /// Variant.  The caller is responsible for ensuring bad data is not passed in.
1068    pub unsafe fn from_data_trusted<T: StaticVariantType, A: AsRef<[u8]>>(data: A) -> Self {
1069        unsafe { Variant::from_data_with_type_trusted(data, &T::static_variant_type()) }
1070    }
1071
1072    // rustdoc-stripper-ignore-next
1073    /// Constructs a new serialized-mode GVariant instance with a given type.
1074    #[doc(alias = "g_variant_new_from_bytes")]
1075    pub fn from_bytes_with_type(bytes: &Bytes, type_: &VariantTy) -> Self {
1076        unsafe {
1077            from_glib_none(ffi::g_variant_new_from_bytes(
1078                type_.as_ptr() as *const _,
1079                bytes.to_glib_none().0,
1080                false.into_glib(),
1081            ))
1082        }
1083    }
1084
1085    // rustdoc-stripper-ignore-next
1086    /// Constructs a new serialized-mode GVariant instance with a given type.
1087    ///
1088    /// This is the same as `from_bytes`, except that checks on the passed
1089    /// data are skipped.
1090    ///
1091    /// You should not use this function on data from external sources.
1092    ///
1093    /// # Safety
1094    ///
1095    /// Since the data is not validated, this is potentially dangerous if called
1096    /// on bytes which are not guaranteed to have come from serialising another
1097    /// Variant.  The caller is responsible for ensuring bad data is not passed in.
1098    pub unsafe fn from_bytes_with_type_trusted(bytes: &Bytes, type_: &VariantTy) -> Self {
1099        unsafe {
1100            from_glib_none(ffi::g_variant_new_from_bytes(
1101                type_.as_ptr() as *const _,
1102                bytes.to_glib_none().0,
1103                true.into_glib(),
1104            ))
1105        }
1106    }
1107
1108    // rustdoc-stripper-ignore-next
1109    /// Constructs a new serialized-mode GVariant instance with a given type.
1110    #[doc(alias = "g_variant_new_from_data")]
1111    pub fn from_data_with_type<A: AsRef<[u8]>>(data: A, type_: &VariantTy) -> Self {
1112        unsafe {
1113            let data = Box::new(data);
1114            let (data_ptr, len) = {
1115                let data = (*data).as_ref();
1116                (data.as_ptr(), data.len())
1117            };
1118
1119            unsafe extern "C" fn free_data<A: AsRef<[u8]>>(ptr: ffi::gpointer) {
1120                unsafe {
1121                    let _ = Box::from_raw(ptr as *mut A);
1122                }
1123            }
1124
1125            from_glib_none(ffi::g_variant_new_from_data(
1126                type_.as_ptr() as *const _,
1127                data_ptr as ffi::gconstpointer,
1128                len,
1129                false.into_glib(),
1130                Some(free_data::<A>),
1131                Box::into_raw(data) as ffi::gpointer,
1132            ))
1133        }
1134    }
1135
1136    // rustdoc-stripper-ignore-next
1137    /// Constructs a new serialized-mode GVariant instance with a given type.
1138    ///
1139    /// This is the same as `from_data`, except that checks on the passed
1140    /// data are skipped.
1141    ///
1142    /// You should not use this function on data from external sources.
1143    ///
1144    /// # Safety
1145    ///
1146    /// Since the data is not validated, this is potentially dangerous if called
1147    /// on bytes which are not guaranteed to have come from serialising another
1148    /// Variant.  The caller is responsible for ensuring bad data is not passed in.
1149    pub unsafe fn from_data_with_type_trusted<A: AsRef<[u8]>>(data: A, type_: &VariantTy) -> Self {
1150        unsafe {
1151            let data = Box::new(data);
1152            let (data_ptr, len) = {
1153                let data = (*data).as_ref();
1154                (data.as_ptr(), data.len())
1155            };
1156
1157            unsafe extern "C" fn free_data<A: AsRef<[u8]>>(ptr: ffi::gpointer) {
1158                unsafe {
1159                    let _ = Box::from_raw(ptr as *mut A);
1160                }
1161            }
1162
1163            from_glib_none(ffi::g_variant_new_from_data(
1164                type_.as_ptr() as *const _,
1165                data_ptr as ffi::gconstpointer,
1166                len,
1167                true.into_glib(),
1168                Some(free_data::<A>),
1169                Box::into_raw(data) as ffi::gpointer,
1170            ))
1171        }
1172    }
1173
1174    // rustdoc-stripper-ignore-next
1175    /// Returns the serialized form of a GVariant instance.
1176    // rustdoc-stripper-ignore-next-stop
1177    /// Returns a pointer to the serialized form of a #GVariant instance.
1178    /// The semantics of this function are exactly the same as
1179    /// g_variant_get_data(), except that the returned #GBytes holds
1180    /// a reference to the variant data.
1181    ///
1182    /// # Returns
1183    ///
1184    /// A new #GBytes representing the variant data
1185    #[doc(alias = "get_data_as_bytes")]
1186    #[doc(alias = "g_variant_get_data_as_bytes")]
1187    pub fn data_as_bytes(&self) -> Bytes {
1188        unsafe { from_glib_full(ffi::g_variant_get_data_as_bytes(self.to_glib_none().0)) }
1189    }
1190
1191    // rustdoc-stripper-ignore-next
1192    /// Returns the serialized form of a GVariant instance.
1193    // rustdoc-stripper-ignore-next-stop
1194    /// Returns a pointer to the serialized form of a #GVariant instance.
1195    /// The returned data may not be in fully-normalised form if read from an
1196    /// untrusted source.  The returned data must not be freed; it remains
1197    /// valid for as long as @self exists.
1198    ///
1199    /// If @self is a fixed-sized value that was deserialized from a
1200    /// corrupted serialized container then [`None`] may be returned.  In this
1201    /// case, the proper thing to do is typically to use the appropriate
1202    /// number of nul bytes in place of @self.  If @self is not fixed-sized
1203    /// then [`None`] is never returned.
1204    ///
1205    /// In the case that @self is already in serialized form, this function
1206    /// is O(1).  If the value is not already in serialized form,
1207    /// serialization occurs implicitly and is approximately O(n) in the size
1208    /// of the result.
1209    ///
1210    /// To deserialize the data returned by this function, in addition to the
1211    /// serialized data, you must know the type of the #GVariant, and (if the
1212    /// machine might be different) the endianness of the machine that stored
1213    /// it. As a result, file formats or network messages that incorporate
1214    /// serialized #GVariants must include this information either
1215    /// implicitly (for instance "the file always contains a
1216    /// `G_VARIANT_TYPE_VARIANT` and it is always in little-endian order") or
1217    /// explicitly (by storing the type and/or endianness in addition to the
1218    /// serialized data).
1219    ///
1220    /// # Returns
1221    ///
1222    /// the serialized form of @self, or [`None`]
1223    #[doc(alias = "g_variant_get_data")]
1224    pub fn data(&self) -> &[u8] {
1225        unsafe {
1226            let selfv = self.to_glib_none();
1227            let len = ffi::g_variant_get_size(selfv.0);
1228            if len == 0 {
1229                return &[];
1230            }
1231            let ptr = ffi::g_variant_get_data(selfv.0);
1232            slice::from_raw_parts(ptr as *const _, len as _)
1233        }
1234    }
1235
1236    // rustdoc-stripper-ignore-next
1237    /// Returns the size of serialized form of a GVariant instance.
1238    // rustdoc-stripper-ignore-next-stop
1239    /// Determines the number of bytes that would be required to store @self
1240    /// with g_variant_store().
1241    ///
1242    /// If @self has a fixed-sized type then this function always returned
1243    /// that fixed size.
1244    ///
1245    /// In the case that @self is already in serialized form or the size has
1246    /// already been calculated (ie: this function has been called before)
1247    /// then this function is O(1).  Otherwise, the size is calculated, an
1248    /// operation which is approximately O(n) in the number of values
1249    /// involved.
1250    ///
1251    /// # Returns
1252    ///
1253    /// the serialized size of @self
1254    #[doc(alias = "g_variant_get_size")]
1255    pub fn size(&self) -> usize {
1256        unsafe { ffi::g_variant_get_size(self.to_glib_none().0) }
1257    }
1258
1259    // rustdoc-stripper-ignore-next
1260    /// Stores the serialized form of a GVariant instance into the given slice.
1261    ///
1262    /// The slice needs to be big enough.
1263    // rustdoc-stripper-ignore-next-stop
1264    /// Stores the serialized form of @self at @data.  @data should be
1265    /// large enough.  See g_variant_get_size().
1266    ///
1267    /// The stored data is in machine native byte order but may not be in
1268    /// fully-normalised form if read from an untrusted source.  See
1269    /// g_variant_get_normal_form() for a solution.
1270    ///
1271    /// As with g_variant_get_data(), to be able to deserialize the
1272    /// serialized variant successfully, its type and (if the destination
1273    /// machine might be different) its endianness must also be available.
1274    ///
1275    /// This function is approximately O(n) in the size of @data.
1276    #[doc(alias = "g_variant_store")]
1277    pub fn store(&self, data: &mut [u8]) -> Result<usize, crate::BoolError> {
1278        unsafe {
1279            let size = ffi::g_variant_get_size(self.to_glib_none().0);
1280            if data.len() < size {
1281                return Err(bool_error!("Provided slice is too small"));
1282            }
1283
1284            ffi::g_variant_store(self.to_glib_none().0, data.as_mut_ptr() as ffi::gpointer);
1285
1286            Ok(size)
1287        }
1288    }
1289
1290    // rustdoc-stripper-ignore-next
1291    /// Returns a copy of the variant in normal form.
1292    // rustdoc-stripper-ignore-next-stop
1293    /// Gets a #GVariant instance that has the same value as @self and is
1294    /// trusted to be in normal form.
1295    ///
1296    /// If @self is already trusted to be in normal form then a new
1297    /// reference to @self is returned.
1298    ///
1299    /// If @self is not already trusted, then it is scanned to check if it
1300    /// is in normal form.  If it is found to be in normal form then it is
1301    /// marked as trusted and a new reference to it is returned.
1302    ///
1303    /// If @self is found not to be in normal form then a new trusted
1304    /// #GVariant is created with the same value as @self. The non-normal parts of
1305    /// @self will be replaced with default values which are guaranteed to be in
1306    /// normal form.
1307    ///
1308    /// It makes sense to call this function if you've received #GVariant
1309    /// data from untrusted sources and you want to ensure your serialized
1310    /// output is definitely in normal form.
1311    ///
1312    /// If @self is already in normal form, a new reference will be returned
1313    /// (which will be floating if @self is floating). If it is not in normal form,
1314    /// the newly created #GVariant will be returned with a single non-floating
1315    /// reference. Typically, g_variant_take_ref() should be called on the return
1316    /// value from this function to guarantee ownership of a single non-floating
1317    /// reference to it.
1318    ///
1319    /// # Returns
1320    ///
1321    /// a trusted #GVariant
1322    #[doc(alias = "g_variant_get_normal_form")]
1323    #[must_use]
1324    pub fn normal_form(&self) -> Self {
1325        unsafe { from_glib_full(ffi::g_variant_get_normal_form(self.to_glib_none().0)) }
1326    }
1327
1328    // rustdoc-stripper-ignore-next
1329    /// Returns a copy of the variant in the opposite endianness.
1330    // rustdoc-stripper-ignore-next-stop
1331    /// Performs a byteswapping operation on the contents of @self.  The
1332    /// result is that all multi-byte numeric data contained in @self is
1333    /// byteswapped.  That includes 16, 32, and 64bit signed and unsigned
1334    /// integers as well as file handles and double precision floating point
1335    /// values.
1336    ///
1337    /// This function is an identity mapping on any value that does not
1338    /// contain multi-byte numeric data.  That include strings, booleans,
1339    /// bytes and containers containing only these things (recursively).
1340    ///
1341    /// While this function can safely handle untrusted, non-normal data, it is
1342    /// recommended to check whether the input is in normal form beforehand, using
1343    /// g_variant_is_normal_form(), and to reject non-normal inputs if your
1344    /// application can be strict about what inputs it rejects.
1345    ///
1346    /// The returned value is always in normal form and is marked as trusted.
1347    /// A full, not floating, reference is returned.
1348    ///
1349    /// # Returns
1350    ///
1351    /// the byteswapped form of @self
1352    #[doc(alias = "g_variant_byteswap")]
1353    #[must_use]
1354    pub fn byteswap(&self) -> Self {
1355        unsafe { from_glib_full(ffi::g_variant_byteswap(self.to_glib_none().0)) }
1356    }
1357
1358    // rustdoc-stripper-ignore-next
1359    /// Determines the number of children in a container GVariant instance.
1360    // rustdoc-stripper-ignore-next-stop
1361    /// Determines the number of children in a container #GVariant instance.
1362    /// This includes variants, maybes, arrays, tuples and dictionary
1363    /// entries.  It is an error to call this function on any other type of
1364    /// #GVariant.
1365    ///
1366    /// For variants, the return value is always 1.  For values with maybe
1367    /// types, it is always zero or one.  For arrays, it is the length of the
1368    /// array.  For tuples it is the number of tuple items (which depends
1369    /// only on the type).  For dictionary entries, it is always 2
1370    ///
1371    /// This function is O(1).
1372    ///
1373    /// # Returns
1374    ///
1375    /// the number of children in the container
1376    #[doc(alias = "g_variant_n_children")]
1377    pub fn n_children(&self) -> usize {
1378        assert!(self.is_container());
1379
1380        unsafe { ffi::g_variant_n_children(self.to_glib_none().0) }
1381    }
1382
1383    // rustdoc-stripper-ignore-next
1384    /// Create an iterator over items in the variant.
1385    ///
1386    /// Note that this heap allocates a variant for each element,
1387    /// which can be particularly expensive for large arrays.
1388    pub fn iter(&self) -> VariantIter {
1389        assert!(self.is_container());
1390
1391        VariantIter::new(self.clone())
1392    }
1393
1394    // rustdoc-stripper-ignore-next
1395    /// Create an iterator over borrowed strings from a GVariant of type `as` (array of string).
1396    ///
1397    /// This will fail if the variant is not an array of with
1398    /// the expected child type.
1399    ///
1400    /// A benefit of this API over [`Self::iter()`] is that it
1401    /// minimizes allocation, and provides strongly typed access.
1402    ///
1403    /// ```
1404    /// # use glib::prelude::*;
1405    /// let strs = &["foo", "bar"];
1406    /// let strs_variant: glib::Variant = strs.to_variant();
1407    /// for s in strs_variant.array_iter_str()? {
1408    ///     println!("{}", s);
1409    /// }
1410    /// # Ok::<(), Box<dyn std::error::Error>>(())
1411    /// ```
1412    pub fn array_iter_str(&self) -> Result<VariantStrIter<'_>, VariantTypeMismatchError> {
1413        let child_ty = String::static_variant_type();
1414        let actual_ty = self.type_();
1415        let expected_ty = child_ty.as_array();
1416        if actual_ty != expected_ty {
1417            return Err(VariantTypeMismatchError {
1418                actual: actual_ty.to_owned(),
1419                expected: expected_ty.into_owned(),
1420            });
1421        }
1422
1423        Ok(VariantStrIter::new(self))
1424    }
1425
1426    // rustdoc-stripper-ignore-next
1427    /// Return whether this Variant is a container type.
1428    // rustdoc-stripper-ignore-next-stop
1429    /// Checks if @self is a container.
1430    ///
1431    /// # Returns
1432    ///
1433    /// [`true`] if @self is a container
1434    #[doc(alias = "g_variant_is_container")]
1435    pub fn is_container(&self) -> bool {
1436        unsafe { from_glib(ffi::g_variant_is_container(self.to_glib_none().0)) }
1437    }
1438
1439    // rustdoc-stripper-ignore-next
1440    /// Return whether this Variant is in normal form.
1441    // rustdoc-stripper-ignore-next-stop
1442    /// Checks if @self is in normal form.
1443    ///
1444    /// The main reason to do this is to detect if a given chunk of
1445    /// serialized data is in normal form: load the data into a #GVariant
1446    /// using g_variant_new_from_data() and then use this function to
1447    /// check.
1448    ///
1449    /// If @self is found to be in normal form then it will be marked as
1450    /// being trusted.  If the value was already marked as being trusted then
1451    /// this function will immediately return [`true`].
1452    ///
1453    /// There may be implementation specific restrictions on deeply nested values.
1454    /// GVariant is guaranteed to handle nesting up to at least 64 levels.
1455    ///
1456    /// # Returns
1457    ///
1458    /// [`true`] if @self is in normal form
1459    #[doc(alias = "g_variant_is_normal_form")]
1460    pub fn is_normal_form(&self) -> bool {
1461        unsafe { from_glib(ffi::g_variant_is_normal_form(self.to_glib_none().0)) }
1462    }
1463
1464    // rustdoc-stripper-ignore-next
1465    /// Return whether input string is a valid `VariantClass::ObjectPath`.
1466    // rustdoc-stripper-ignore-next-stop
1467    /// Determines if a given string is a valid D-Bus object path.  You
1468    /// should ensure that a string is a valid D-Bus object path before
1469    /// passing it to g_variant_new_object_path().
1470    ///
1471    /// A valid object path starts with `/` followed by zero or more
1472    /// sequences of characters separated by `/` characters.  Each sequence
1473    /// must contain only the characters `[A-Z][a-z][0-9]_`.  No sequence
1474    /// (including the one following the final `/` character) may be empty.
1475    /// ## `string`
1476    /// a normal C nul-terminated string
1477    ///
1478    /// # Returns
1479    ///
1480    /// [`true`] if @string is a D-Bus object path
1481    #[doc(alias = "g_variant_is_object_path")]
1482    pub fn is_object_path(string: &str) -> bool {
1483        unsafe { from_glib(ffi::g_variant_is_object_path(string.to_glib_none().0)) }
1484    }
1485
1486    // rustdoc-stripper-ignore-next
1487    /// Return whether input string is a valid `VariantClass::Signature`.
1488    // rustdoc-stripper-ignore-next-stop
1489    /// Determines if a given string is a valid D-Bus type signature.  You
1490    /// should ensure that a string is a valid D-Bus type signature before
1491    /// passing it to g_variant_new_signature().
1492    ///
1493    /// D-Bus type signatures consist of zero or more definite #GVariantType
1494    /// strings in sequence.
1495    /// ## `string`
1496    /// a normal C nul-terminated string
1497    ///
1498    /// # Returns
1499    ///
1500    /// [`true`] if @string is a D-Bus type signature
1501    #[doc(alias = "g_variant_is_signature")]
1502    pub fn is_signature(string: &str) -> bool {
1503        unsafe { from_glib(ffi::g_variant_is_signature(string.to_glib_none().0)) }
1504    }
1505}
1506
1507unsafe impl Send for Variant {}
1508unsafe impl Sync for Variant {}
1509
1510impl fmt::Debug for Variant {
1511    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1512        f.debug_struct("Variant")
1513            .field("ptr", &ToGlibPtr::<*const _>::to_glib_none(self).0)
1514            .field("type", &self.type_())
1515            .field("value", &self.to_string())
1516            .finish()
1517    }
1518}
1519
1520impl fmt::Display for Variant {
1521    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1522        f.write_str(&self.print(true))
1523    }
1524}
1525
1526impl str::FromStr for Variant {
1527    type Err = crate::Error;
1528
1529    fn from_str(s: &str) -> Result<Self, Self::Err> {
1530        Self::parse(None, s)
1531    }
1532}
1533
1534impl PartialEq for Variant {
1535    #[doc(alias = "g_variant_equal")]
1536    fn eq(&self, other: &Self) -> bool {
1537        unsafe {
1538            from_glib(ffi::g_variant_equal(
1539                ToGlibPtr::<*const _>::to_glib_none(self).0 as *const _,
1540                ToGlibPtr::<*const _>::to_glib_none(other).0 as *const _,
1541            ))
1542        }
1543    }
1544}
1545
1546impl Eq for Variant {}
1547
1548impl PartialOrd for Variant {
1549    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1550        unsafe {
1551            if ffi::g_variant_classify(self.to_glib_none().0)
1552                != ffi::g_variant_classify(other.to_glib_none().0)
1553            {
1554                return None;
1555            }
1556
1557            if self.is_container() {
1558                return None;
1559            }
1560
1561            let res = ffi::g_variant_compare(
1562                ToGlibPtr::<*const _>::to_glib_none(self).0 as *const _,
1563                ToGlibPtr::<*const _>::to_glib_none(other).0 as *const _,
1564            );
1565
1566            Some(res.cmp(&0))
1567        }
1568    }
1569}
1570
1571impl Hash for Variant {
1572    #[doc(alias = "g_variant_hash")]
1573    fn hash<H: Hasher>(&self, state: &mut H) {
1574        unsafe {
1575            state.write_u32(ffi::g_variant_hash(
1576                ToGlibPtr::<*const _>::to_glib_none(self).0 as *const _,
1577            ))
1578        }
1579    }
1580}
1581
1582impl AsRef<Variant> for Variant {
1583    #[inline]
1584    fn as_ref(&self) -> &Self {
1585        self
1586    }
1587}
1588
1589// rustdoc-stripper-ignore-next
1590/// Converts to `Variant`.
1591pub trait ToVariant {
1592    // rustdoc-stripper-ignore-next
1593    /// Returns a `Variant` clone of `self`.
1594    fn to_variant(&self) -> Variant;
1595}
1596
1597// rustdoc-stripper-ignore-next
1598/// Extracts a value.
1599pub trait FromVariant: Sized + StaticVariantType {
1600    // rustdoc-stripper-ignore-next
1601    /// Tries to extract a value.
1602    ///
1603    /// Returns `Some` if the variant's type matches `Self`.
1604    fn from_variant(variant: &Variant) -> Option<Self>;
1605}
1606
1607// rustdoc-stripper-ignore-next
1608/// Returns `VariantType` of `Self`.
1609pub trait StaticVariantType {
1610    // rustdoc-stripper-ignore-next
1611    /// Returns the `VariantType` corresponding to `Self`.
1612    fn static_variant_type() -> Cow<'static, VariantTy>;
1613}
1614
1615impl StaticVariantType for Variant {
1616    fn static_variant_type() -> Cow<'static, VariantTy> {
1617        Cow::Borrowed(VariantTy::VARIANT)
1618    }
1619}
1620
1621impl<T: ?Sized + ToVariant> ToVariant for &T {
1622    fn to_variant(&self) -> Variant {
1623        <T as ToVariant>::to_variant(self)
1624    }
1625}
1626
1627impl<'a, T: Into<Variant> + Clone> From<&'a T> for Variant {
1628    #[inline]
1629    fn from(v: &'a T) -> Self {
1630        v.clone().into()
1631    }
1632}
1633
1634impl<T: ?Sized + StaticVariantType> StaticVariantType for &T {
1635    fn static_variant_type() -> Cow<'static, VariantTy> {
1636        <T as StaticVariantType>::static_variant_type()
1637    }
1638}
1639
1640macro_rules! impl_numeric {
1641    ($name:ty, $typ:expr, $new_fn:ident, $get_fn:ident) => {
1642        impl StaticVariantType for $name {
1643            fn static_variant_type() -> Cow<'static, VariantTy> {
1644                Cow::Borrowed($typ)
1645            }
1646        }
1647
1648        impl ToVariant for $name {
1649            fn to_variant(&self) -> Variant {
1650                unsafe { from_glib_none(ffi::$new_fn(*self)) }
1651            }
1652        }
1653
1654        impl From<$name> for Variant {
1655            #[inline]
1656            fn from(v: $name) -> Self {
1657                v.to_variant()
1658            }
1659        }
1660
1661        impl FromVariant for $name {
1662            fn from_variant(variant: &Variant) -> Option<Self> {
1663                unsafe {
1664                    if variant.is::<Self>() {
1665                        Some(ffi::$get_fn(variant.to_glib_none().0))
1666                    } else {
1667                        None
1668                    }
1669                }
1670            }
1671        }
1672    };
1673}
1674
1675impl_numeric!(u8, VariantTy::BYTE, g_variant_new_byte, g_variant_get_byte);
1676impl_numeric!(
1677    i16,
1678    VariantTy::INT16,
1679    g_variant_new_int16,
1680    g_variant_get_int16
1681);
1682impl_numeric!(
1683    u16,
1684    VariantTy::UINT16,
1685    g_variant_new_uint16,
1686    g_variant_get_uint16
1687);
1688impl_numeric!(
1689    i32,
1690    VariantTy::INT32,
1691    g_variant_new_int32,
1692    g_variant_get_int32
1693);
1694impl_numeric!(
1695    u32,
1696    VariantTy::UINT32,
1697    g_variant_new_uint32,
1698    g_variant_get_uint32
1699);
1700impl_numeric!(
1701    i64,
1702    VariantTy::INT64,
1703    g_variant_new_int64,
1704    g_variant_get_int64
1705);
1706impl_numeric!(
1707    u64,
1708    VariantTy::UINT64,
1709    g_variant_new_uint64,
1710    g_variant_get_uint64
1711);
1712impl_numeric!(
1713    f64,
1714    VariantTy::DOUBLE,
1715    g_variant_new_double,
1716    g_variant_get_double
1717);
1718
1719impl StaticVariantType for () {
1720    fn static_variant_type() -> Cow<'static, VariantTy> {
1721        Cow::Borrowed(VariantTy::UNIT)
1722    }
1723}
1724
1725impl ToVariant for () {
1726    fn to_variant(&self) -> Variant {
1727        unsafe { from_glib_none(ffi::g_variant_new_tuple(ptr::null(), 0)) }
1728    }
1729}
1730
1731impl From<()> for Variant {
1732    #[inline]
1733    fn from(_: ()) -> Self {
1734        ().to_variant()
1735    }
1736}
1737
1738impl FromVariant for () {
1739    fn from_variant(variant: &Variant) -> Option<Self> {
1740        if variant.is::<Self>() { Some(()) } else { None }
1741    }
1742}
1743
1744impl StaticVariantType for bool {
1745    fn static_variant_type() -> Cow<'static, VariantTy> {
1746        Cow::Borrowed(VariantTy::BOOLEAN)
1747    }
1748}
1749
1750impl ToVariant for bool {
1751    fn to_variant(&self) -> Variant {
1752        unsafe { from_glib_none(ffi::g_variant_new_boolean(self.into_glib())) }
1753    }
1754}
1755
1756impl From<bool> for Variant {
1757    #[inline]
1758    fn from(v: bool) -> Self {
1759        v.to_variant()
1760    }
1761}
1762
1763impl FromVariant for bool {
1764    fn from_variant(variant: &Variant) -> Option<Self> {
1765        unsafe {
1766            if variant.is::<Self>() {
1767                Some(from_glib(ffi::g_variant_get_boolean(
1768                    variant.to_glib_none().0,
1769                )))
1770            } else {
1771                None
1772            }
1773        }
1774    }
1775}
1776
1777impl StaticVariantType for String {
1778    fn static_variant_type() -> Cow<'static, VariantTy> {
1779        Cow::Borrowed(VariantTy::STRING)
1780    }
1781}
1782
1783impl ToVariant for String {
1784    fn to_variant(&self) -> Variant {
1785        self[..].to_variant()
1786    }
1787}
1788
1789impl From<String> for Variant {
1790    #[inline]
1791    fn from(s: String) -> Self {
1792        s.to_variant()
1793    }
1794}
1795
1796impl FromVariant for String {
1797    fn from_variant(variant: &Variant) -> Option<Self> {
1798        variant.str().map(String::from)
1799    }
1800}
1801
1802impl StaticVariantType for str {
1803    fn static_variant_type() -> Cow<'static, VariantTy> {
1804        String::static_variant_type()
1805    }
1806}
1807
1808impl ToVariant for str {
1809    fn to_variant(&self) -> Variant {
1810        unsafe { from_glib_none(ffi::g_variant_new_take_string(self.to_glib_full())) }
1811    }
1812}
1813
1814impl From<&str> for Variant {
1815    #[inline]
1816    fn from(s: &str) -> Self {
1817        s.to_variant()
1818    }
1819}
1820
1821impl StaticVariantType for std::path::PathBuf {
1822    fn static_variant_type() -> Cow<'static, VariantTy> {
1823        std::path::Path::static_variant_type()
1824    }
1825}
1826
1827impl ToVariant for std::path::PathBuf {
1828    fn to_variant(&self) -> Variant {
1829        self.as_path().to_variant()
1830    }
1831}
1832
1833impl From<std::path::PathBuf> for Variant {
1834    #[inline]
1835    fn from(p: std::path::PathBuf) -> Self {
1836        p.to_variant()
1837    }
1838}
1839
1840impl FromVariant for std::path::PathBuf {
1841    fn from_variant(variant: &Variant) -> Option<Self> {
1842        unsafe {
1843            let ptr = ffi::g_variant_get_bytestring(variant.to_glib_none().0);
1844            Some(crate::translate::c_to_path_buf(ptr as *const _))
1845        }
1846    }
1847}
1848
1849impl StaticVariantType for std::path::Path {
1850    fn static_variant_type() -> Cow<'static, VariantTy> {
1851        <&[u8]>::static_variant_type()
1852    }
1853}
1854
1855impl ToVariant for std::path::Path {
1856    fn to_variant(&self) -> Variant {
1857        let tmp = crate::translate::path_to_c(self);
1858        unsafe { from_glib_none(ffi::g_variant_new_bytestring(tmp.as_ptr() as *const u8)) }
1859    }
1860}
1861
1862impl From<&std::path::Path> for Variant {
1863    #[inline]
1864    fn from(p: &std::path::Path) -> Self {
1865        p.to_variant()
1866    }
1867}
1868
1869impl StaticVariantType for std::ffi::OsString {
1870    fn static_variant_type() -> Cow<'static, VariantTy> {
1871        std::ffi::OsStr::static_variant_type()
1872    }
1873}
1874
1875impl ToVariant for std::ffi::OsString {
1876    fn to_variant(&self) -> Variant {
1877        self.as_os_str().to_variant()
1878    }
1879}
1880
1881impl From<std::ffi::OsString> for Variant {
1882    #[inline]
1883    fn from(s: std::ffi::OsString) -> Self {
1884        s.to_variant()
1885    }
1886}
1887
1888impl FromVariant for std::ffi::OsString {
1889    fn from_variant(variant: &Variant) -> Option<Self> {
1890        unsafe {
1891            let ptr = ffi::g_variant_get_bytestring(variant.to_glib_none().0);
1892            Some(crate::translate::c_to_os_string(ptr as *const _))
1893        }
1894    }
1895}
1896
1897impl StaticVariantType for std::ffi::OsStr {
1898    fn static_variant_type() -> Cow<'static, VariantTy> {
1899        <&[u8]>::static_variant_type()
1900    }
1901}
1902
1903impl ToVariant for std::ffi::OsStr {
1904    fn to_variant(&self) -> Variant {
1905        let tmp = crate::translate::os_str_to_c(self);
1906        unsafe { from_glib_none(ffi::g_variant_new_bytestring(tmp.as_ptr() as *const u8)) }
1907    }
1908}
1909
1910impl From<&std::ffi::OsStr> for Variant {
1911    #[inline]
1912    fn from(s: &std::ffi::OsStr) -> Self {
1913        s.to_variant()
1914    }
1915}
1916
1917impl<T: StaticVariantType> StaticVariantType for Option<T> {
1918    fn static_variant_type() -> Cow<'static, VariantTy> {
1919        Cow::Owned(VariantType::new_maybe(&T::static_variant_type()))
1920    }
1921}
1922
1923impl<T: StaticVariantType + ToVariant> ToVariant for Option<T> {
1924    fn to_variant(&self) -> Variant {
1925        Variant::from_maybe::<T>(self.as_ref().map(|m| m.to_variant()).as_ref())
1926    }
1927}
1928
1929impl<T: StaticVariantType + Into<Variant>> From<Option<T>> for Variant {
1930    #[inline]
1931    fn from(v: Option<T>) -> Self {
1932        Variant::from_maybe::<T>(v.map(|v| v.into()).as_ref())
1933    }
1934}
1935
1936impl<T: StaticVariantType + FromVariant> FromVariant for Option<T> {
1937    fn from_variant(variant: &Variant) -> Option<Self> {
1938        unsafe {
1939            if variant.is::<Self>() {
1940                let c_child = ffi::g_variant_get_maybe(variant.to_glib_none().0);
1941                if !c_child.is_null() {
1942                    let child: Variant = from_glib_full(c_child);
1943
1944                    Some(T::from_variant(&child))
1945                } else {
1946                    Some(None)
1947                }
1948            } else {
1949                None
1950            }
1951        }
1952    }
1953}
1954
1955impl<T: StaticVariantType> StaticVariantType for [T] {
1956    fn static_variant_type() -> Cow<'static, VariantTy> {
1957        T::static_variant_type().as_array()
1958    }
1959}
1960
1961impl<T: StaticVariantType + ToVariant> ToVariant for [T] {
1962    fn to_variant(&self) -> Variant {
1963        unsafe {
1964            if self.is_empty() {
1965                return from_glib_none(ffi::g_variant_new_array(
1966                    T::static_variant_type().to_glib_none().0,
1967                    ptr::null(),
1968                    0,
1969                ));
1970            }
1971
1972            let mut builder = mem::MaybeUninit::uninit();
1973            ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
1974            let mut builder = builder.assume_init();
1975            for value in self {
1976                let value = value.to_variant();
1977                ffi::g_variant_builder_add_value(&mut builder, value.to_glib_none().0);
1978            }
1979            from_glib_none(ffi::g_variant_builder_end(&mut builder))
1980        }
1981    }
1982}
1983
1984impl<T: StaticVariantType + ToVariant> From<&[T]> for Variant {
1985    #[inline]
1986    fn from(s: &[T]) -> Self {
1987        s.to_variant()
1988    }
1989}
1990
1991impl<T: FromVariant> FromVariant for Vec<T> {
1992    fn from_variant(variant: &Variant) -> Option<Self> {
1993        if !variant.is_container() {
1994            return None;
1995        }
1996
1997        let mut vec = Vec::with_capacity(variant.n_children());
1998
1999        for i in 0..variant.n_children() {
2000            match variant.child_value(i).get() {
2001                Some(child) => vec.push(child),
2002                None => return None,
2003            }
2004        }
2005
2006        Some(vec)
2007    }
2008}
2009
2010impl<T: StaticVariantType + ToVariant> ToVariant for Vec<T> {
2011    fn to_variant(&self) -> Variant {
2012        self.as_slice().to_variant()
2013    }
2014}
2015
2016impl<T: StaticVariantType + Into<Variant>> From<Vec<T>> for Variant {
2017    fn from(v: Vec<T>) -> Self {
2018        unsafe {
2019            if v.is_empty() {
2020                return from_glib_none(ffi::g_variant_new_array(
2021                    T::static_variant_type().to_glib_none().0,
2022                    ptr::null(),
2023                    0,
2024                ));
2025            }
2026
2027            let mut builder = mem::MaybeUninit::uninit();
2028            ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
2029            let mut builder = builder.assume_init();
2030            for value in v {
2031                let value = value.into();
2032                ffi::g_variant_builder_add_value(&mut builder, value.to_glib_none().0);
2033            }
2034            from_glib_none(ffi::g_variant_builder_end(&mut builder))
2035        }
2036    }
2037}
2038
2039impl<T: StaticVariantType> StaticVariantType for Vec<T> {
2040    fn static_variant_type() -> Cow<'static, VariantTy> {
2041        <[T]>::static_variant_type()
2042    }
2043}
2044
2045impl<K, V, H> FromVariant for HashMap<K, V, H>
2046where
2047    K: FromVariant + Eq + Hash,
2048    V: FromVariant,
2049    H: BuildHasher + Default,
2050{
2051    fn from_variant(variant: &Variant) -> Option<Self> {
2052        if !variant.is_container() {
2053            return None;
2054        }
2055
2056        let mut map = HashMap::default();
2057
2058        for i in 0..variant.n_children() {
2059            let entry = variant.child_value(i);
2060            let key = entry.child_value(0).get()?;
2061            let val = entry.child_value(1).get()?;
2062
2063            map.insert(key, val);
2064        }
2065
2066        Some(map)
2067    }
2068}
2069
2070impl<K, V> FromVariant for BTreeMap<K, V>
2071where
2072    K: FromVariant + Eq + Ord,
2073    V: FromVariant,
2074{
2075    fn from_variant(variant: &Variant) -> Option<Self> {
2076        if !variant.is_container() {
2077            return None;
2078        }
2079
2080        let mut map = BTreeMap::default();
2081
2082        for i in 0..variant.n_children() {
2083            let entry = variant.child_value(i);
2084            let key = entry.child_value(0).get()?;
2085            let val = entry.child_value(1).get()?;
2086
2087            map.insert(key, val);
2088        }
2089
2090        Some(map)
2091    }
2092}
2093
2094impl<K, V> ToVariant for HashMap<K, V>
2095where
2096    K: StaticVariantType + ToVariant + Eq + Hash,
2097    V: StaticVariantType + ToVariant,
2098{
2099    fn to_variant(&self) -> Variant {
2100        unsafe {
2101            if self.is_empty() {
2102                return from_glib_none(ffi::g_variant_new_array(
2103                    DictEntry::<K, V>::static_variant_type().to_glib_none().0,
2104                    ptr::null(),
2105                    0,
2106                ));
2107            }
2108
2109            let mut builder = mem::MaybeUninit::uninit();
2110            ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
2111            let mut builder = builder.assume_init();
2112            for (key, value) in self {
2113                let entry = DictEntry::new(key, value).to_variant();
2114                ffi::g_variant_builder_add_value(&mut builder, entry.to_glib_none().0);
2115            }
2116            from_glib_none(ffi::g_variant_builder_end(&mut builder))
2117        }
2118    }
2119}
2120
2121impl<K, V> From<HashMap<K, V>> for Variant
2122where
2123    K: StaticVariantType + Into<Variant> + Eq + Hash,
2124    V: StaticVariantType + Into<Variant>,
2125{
2126    fn from(m: HashMap<K, V>) -> Self {
2127        unsafe {
2128            if m.is_empty() {
2129                return from_glib_none(ffi::g_variant_new_array(
2130                    DictEntry::<K, V>::static_variant_type().to_glib_none().0,
2131                    ptr::null(),
2132                    0,
2133                ));
2134            }
2135
2136            let mut builder = mem::MaybeUninit::uninit();
2137            ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
2138            let mut builder = builder.assume_init();
2139            for (key, value) in m {
2140                let entry = Variant::from(DictEntry::new(key, value));
2141                ffi::g_variant_builder_add_value(&mut builder, entry.to_glib_none().0);
2142            }
2143            from_glib_none(ffi::g_variant_builder_end(&mut builder))
2144        }
2145    }
2146}
2147
2148impl<K, V> ToVariant for BTreeMap<K, V>
2149where
2150    K: StaticVariantType + ToVariant + Eq + Hash,
2151    V: StaticVariantType + ToVariant,
2152{
2153    fn to_variant(&self) -> Variant {
2154        unsafe {
2155            if self.is_empty() {
2156                return from_glib_none(ffi::g_variant_new_array(
2157                    DictEntry::<K, V>::static_variant_type().to_glib_none().0,
2158                    ptr::null(),
2159                    0,
2160                ));
2161            }
2162
2163            let mut builder = mem::MaybeUninit::uninit();
2164            ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
2165            let mut builder = builder.assume_init();
2166            for (key, value) in self {
2167                let entry = DictEntry::new(key, value).to_variant();
2168                ffi::g_variant_builder_add_value(&mut builder, entry.to_glib_none().0);
2169            }
2170            from_glib_none(ffi::g_variant_builder_end(&mut builder))
2171        }
2172    }
2173}
2174
2175impl<K, V> From<BTreeMap<K, V>> for Variant
2176where
2177    K: StaticVariantType + Into<Variant> + Eq + Hash,
2178    V: StaticVariantType + Into<Variant>,
2179{
2180    fn from(m: BTreeMap<K, V>) -> Self {
2181        unsafe {
2182            if m.is_empty() {
2183                return from_glib_none(ffi::g_variant_new_array(
2184                    DictEntry::<K, V>::static_variant_type().to_glib_none().0,
2185                    ptr::null(),
2186                    0,
2187                ));
2188            }
2189
2190            let mut builder = mem::MaybeUninit::uninit();
2191            ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::ARRAY.to_glib_none().0);
2192            let mut builder = builder.assume_init();
2193            for (key, value) in m {
2194                let entry = Variant::from(DictEntry::new(key, value));
2195                ffi::g_variant_builder_add_value(&mut builder, entry.to_glib_none().0);
2196            }
2197            from_glib_none(ffi::g_variant_builder_end(&mut builder))
2198        }
2199    }
2200}
2201
2202/// A Dictionary entry.
2203///
2204/// While GVariant format allows a dictionary entry to be an independent type, typically you'll need
2205/// to use this in a dictionary, which is simply an array of dictionary entries. The following code
2206/// creates a dictionary:
2207///
2208/// ```
2209///# use glib::prelude::*; // or `use gtk::prelude::*;`
2210/// use glib::variant::{Variant, FromVariant, DictEntry};
2211///
2212/// let entries = [
2213///     DictEntry::new("uuid", 1000u32),
2214///     DictEntry::new("guid", 1001u32),
2215/// ];
2216/// let dict = entries.into_iter().collect::<Variant>();
2217/// assert_eq!(dict.n_children(), 2);
2218/// assert_eq!(dict.type_().as_str(), "a{su}");
2219/// ```
2220#[derive(Debug, Clone)]
2221pub struct DictEntry<K, V> {
2222    key: K,
2223    value: V,
2224}
2225
2226impl<K, V> DictEntry<K, V>
2227where
2228    K: StaticVariantType,
2229    V: StaticVariantType,
2230{
2231    pub fn new(key: K, value: V) -> Self {
2232        Self { key, value }
2233    }
2234
2235    pub fn key(&self) -> &K {
2236        &self.key
2237    }
2238
2239    pub fn value(&self) -> &V {
2240        &self.value
2241    }
2242}
2243
2244impl<K, V> FromVariant for DictEntry<K, V>
2245where
2246    K: FromVariant,
2247    V: FromVariant,
2248{
2249    fn from_variant(variant: &Variant) -> Option<Self> {
2250        if !variant.type_().is_subtype_of(VariantTy::DICT_ENTRY) {
2251            return None;
2252        }
2253
2254        let key = variant.child_value(0).get()?;
2255        let value = variant.child_value(1).get()?;
2256
2257        Some(Self { key, value })
2258    }
2259}
2260
2261impl<K, V> ToVariant for DictEntry<K, V>
2262where
2263    K: StaticVariantType + ToVariant,
2264    V: StaticVariantType + ToVariant,
2265{
2266    fn to_variant(&self) -> Variant {
2267        Variant::from_dict_entry(&self.key.to_variant(), &self.value.to_variant())
2268    }
2269}
2270
2271impl<K, V> From<DictEntry<K, V>> for Variant
2272where
2273    K: StaticVariantType + Into<Variant>,
2274    V: StaticVariantType + Into<Variant>,
2275{
2276    fn from(e: DictEntry<K, V>) -> Self {
2277        Variant::from_dict_entry(&e.key.into(), &e.value.into())
2278    }
2279}
2280
2281impl ToVariant for Variant {
2282    fn to_variant(&self) -> Variant {
2283        Variant::from_variant(self)
2284    }
2285}
2286
2287impl FromVariant for Variant {
2288    fn from_variant(variant: &Variant) -> Option<Self> {
2289        variant.as_variant()
2290    }
2291}
2292
2293impl<K: StaticVariantType, V: StaticVariantType> StaticVariantType for DictEntry<K, V> {
2294    fn static_variant_type() -> Cow<'static, VariantTy> {
2295        Cow::Owned(VariantType::new_dict_entry(
2296            &K::static_variant_type(),
2297            &V::static_variant_type(),
2298        ))
2299    }
2300}
2301
2302fn static_variant_mapping<K, V>() -> Cow<'static, VariantTy>
2303where
2304    K: StaticVariantType,
2305    V: StaticVariantType,
2306{
2307    use std::fmt::Write;
2308
2309    let key_type = K::static_variant_type();
2310    let value_type = V::static_variant_type();
2311
2312    if key_type == VariantTy::STRING && value_type == VariantTy::VARIANT {
2313        return Cow::Borrowed(VariantTy::VARDICT);
2314    }
2315
2316    let mut builder = crate::GStringBuilder::default();
2317    write!(builder, "a{{{}{}}}", key_type.as_str(), value_type.as_str()).unwrap();
2318
2319    Cow::Owned(VariantType::from_string(builder.into_string()).unwrap())
2320}
2321
2322impl<K, V, H> StaticVariantType for HashMap<K, V, H>
2323where
2324    K: StaticVariantType,
2325    V: StaticVariantType,
2326    H: BuildHasher + Default,
2327{
2328    fn static_variant_type() -> Cow<'static, VariantTy> {
2329        static_variant_mapping::<K, V>()
2330    }
2331}
2332
2333impl<K, V> StaticVariantType for BTreeMap<K, V>
2334where
2335    K: StaticVariantType,
2336    V: StaticVariantType,
2337{
2338    fn static_variant_type() -> Cow<'static, VariantTy> {
2339        static_variant_mapping::<K, V>()
2340    }
2341}
2342
2343macro_rules! tuple_impls {
2344    ($($len:expr => ($($n:tt $name:ident)+))+) => {
2345        $(
2346            impl<$($name),+> StaticVariantType for ($($name,)+)
2347            where
2348                $($name: StaticVariantType,)+
2349            {
2350                fn static_variant_type() -> Cow<'static, VariantTy> {
2351                    Cow::Owned(VariantType::new_tuple(&[
2352                        $(
2353                            $name::static_variant_type(),
2354                        )+
2355                    ]))
2356                }
2357            }
2358
2359            impl<$($name),+> FromVariant for ($($name,)+)
2360            where
2361                $($name: FromVariant,)+
2362            {
2363                fn from_variant(variant: &Variant) -> Option<Self> {
2364                    if !variant.type_().is_subtype_of(VariantTy::TUPLE) {
2365                        return None;
2366                    }
2367
2368                    Some((
2369                        $(
2370                            match variant.try_child_get::<$name>($n) {
2371                                Ok(Some(field)) => field,
2372                                _ => return None,
2373                            },
2374                        )+
2375                    ))
2376                }
2377            }
2378
2379            impl<$($name),+> ToVariant for ($($name,)+)
2380            where
2381                $($name: ToVariant,)+
2382            {
2383                fn to_variant(&self) -> Variant {
2384                    unsafe {
2385                        let mut builder = mem::MaybeUninit::uninit();
2386                        ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::TUPLE.to_glib_none().0);
2387                        let mut builder = builder.assume_init();
2388
2389                        $(
2390                            let field = self.$n.to_variant();
2391                            ffi::g_variant_builder_add_value(&mut builder, field.to_glib_none().0);
2392                        )+
2393
2394                        from_glib_none(ffi::g_variant_builder_end(&mut builder))
2395                    }
2396                }
2397            }
2398
2399            impl<$($name),+> From<($($name,)+)> for Variant
2400            where
2401                $($name: Into<Variant>,)+
2402            {
2403                fn from(t: ($($name,)+)) -> Self {
2404                    unsafe {
2405                        let mut builder = mem::MaybeUninit::uninit();
2406                        ffi::g_variant_builder_init(builder.as_mut_ptr(), VariantTy::TUPLE.to_glib_none().0);
2407                        let mut builder = builder.assume_init();
2408
2409                        $(
2410                            let field = t.$n.into();
2411                            ffi::g_variant_builder_add_value(&mut builder, field.to_glib_none().0);
2412                        )+
2413
2414                        from_glib_none(ffi::g_variant_builder_end(&mut builder))
2415                    }
2416                }
2417            }
2418        )+
2419    }
2420}
2421
2422tuple_impls! {
2423    1 => (0 T0)
2424    2 => (0 T0 1 T1)
2425    3 => (0 T0 1 T1 2 T2)
2426    4 => (0 T0 1 T1 2 T2 3 T3)
2427    5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
2428    6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
2429    7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
2430    8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
2431    9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
2432    10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
2433    11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
2434    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)
2435    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)
2436    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)
2437    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)
2438    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)
2439}
2440
2441impl<T: Into<Variant> + StaticVariantType> FromIterator<T> for Variant {
2442    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
2443        Variant::array_from_iter::<T>(iter.into_iter().map(|v| v.into()))
2444    }
2445}
2446
2447/// Trait for fixed size variant types.
2448pub unsafe trait FixedSizeVariantType: StaticVariantType + Sized + Copy {}
2449unsafe impl FixedSizeVariantType for u8 {}
2450unsafe impl FixedSizeVariantType for i16 {}
2451unsafe impl FixedSizeVariantType for u16 {}
2452unsafe impl FixedSizeVariantType for i32 {}
2453unsafe impl FixedSizeVariantType for u32 {}
2454unsafe impl FixedSizeVariantType for i64 {}
2455unsafe impl FixedSizeVariantType for u64 {}
2456unsafe impl FixedSizeVariantType for f64 {}
2457unsafe impl FixedSizeVariantType for bool {}
2458
2459/// Wrapper type for fixed size type arrays.
2460///
2461/// Converting this from/to a `Variant` is generally more efficient than working on the type
2462/// directly. This is especially important when deriving `Variant` trait implementations on custom
2463/// types.
2464///
2465/// This wrapper type can hold for example `Vec<u8>`, `Box<[u8]>` and similar types.
2466#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
2467pub struct FixedSizeVariantArray<A, T>(A, std::marker::PhantomData<T>)
2468where
2469    A: AsRef<[T]>,
2470    T: FixedSizeVariantType;
2471
2472impl<A: AsRef<[T]>, T: FixedSizeVariantType> From<A> for FixedSizeVariantArray<A, T> {
2473    fn from(array: A) -> Self {
2474        FixedSizeVariantArray(array, std::marker::PhantomData)
2475    }
2476}
2477
2478impl<A: AsRef<[T]>, T: FixedSizeVariantType> FixedSizeVariantArray<A, T> {
2479    pub fn into_inner(self) -> A {
2480        self.0
2481    }
2482}
2483
2484impl<A: AsRef<[T]>, T: FixedSizeVariantType> std::ops::Deref for FixedSizeVariantArray<A, T> {
2485    type Target = A;
2486
2487    #[inline]
2488    fn deref(&self) -> &Self::Target {
2489        &self.0
2490    }
2491}
2492
2493impl<A: AsRef<[T]>, T: FixedSizeVariantType> std::ops::DerefMut for FixedSizeVariantArray<A, T> {
2494    #[inline]
2495    fn deref_mut(&mut self) -> &mut Self::Target {
2496        &mut self.0
2497    }
2498}
2499
2500impl<A: AsRef<[T]>, T: FixedSizeVariantType> AsRef<A> for FixedSizeVariantArray<A, T> {
2501    #[inline]
2502    fn as_ref(&self) -> &A {
2503        &self.0
2504    }
2505}
2506
2507impl<A: AsRef<[T]>, T: FixedSizeVariantType> AsMut<A> for FixedSizeVariantArray<A, T> {
2508    #[inline]
2509    fn as_mut(&mut self) -> &mut A {
2510        &mut self.0
2511    }
2512}
2513
2514impl<A: AsRef<[T]>, T: FixedSizeVariantType> AsRef<[T]> for FixedSizeVariantArray<A, T> {
2515    #[inline]
2516    fn as_ref(&self) -> &[T] {
2517        self.0.as_ref()
2518    }
2519}
2520
2521impl<A: AsRef<[T]> + AsMut<[T]>, T: FixedSizeVariantType> AsMut<[T]>
2522    for FixedSizeVariantArray<A, T>
2523{
2524    #[inline]
2525    fn as_mut(&mut self) -> &mut [T] {
2526        self.0.as_mut()
2527    }
2528}
2529
2530impl<A: AsRef<[T]>, T: FixedSizeVariantType> StaticVariantType for FixedSizeVariantArray<A, T> {
2531    fn static_variant_type() -> Cow<'static, VariantTy> {
2532        <[T]>::static_variant_type()
2533    }
2534}
2535
2536impl<A: AsRef<[T]> + for<'a> From<&'a [T]>, T: FixedSizeVariantType> FromVariant
2537    for FixedSizeVariantArray<A, T>
2538{
2539    fn from_variant(variant: &Variant) -> Option<Self> {
2540        Some(FixedSizeVariantArray(
2541            A::from(variant.fixed_array::<T>().ok()?),
2542            std::marker::PhantomData,
2543        ))
2544    }
2545}
2546
2547impl<A: AsRef<[T]>, T: FixedSizeVariantType> ToVariant for FixedSizeVariantArray<A, T> {
2548    fn to_variant(&self) -> Variant {
2549        Variant::array_from_fixed_array(self.0.as_ref())
2550    }
2551}
2552
2553impl<A: AsRef<[T]>, T: FixedSizeVariantType> From<FixedSizeVariantArray<A, T>> for Variant {
2554    #[doc(alias = "g_variant_new_from_data")]
2555    fn from(a: FixedSizeVariantArray<A, T>) -> Self {
2556        unsafe {
2557            let data = Box::new(a.0);
2558            let (data_ptr, len) = {
2559                let data = (*data).as_ref();
2560                (data.as_ptr(), mem::size_of_val(data))
2561            };
2562
2563            unsafe extern "C" fn free_data<A: AsRef<[T]>, T: FixedSizeVariantType>(
2564                ptr: ffi::gpointer,
2565            ) {
2566                unsafe {
2567                    let _ = Box::from_raw(ptr as *mut A);
2568                }
2569            }
2570
2571            from_glib_none(ffi::g_variant_new_from_data(
2572                T::static_variant_type().to_glib_none().0,
2573                data_ptr as ffi::gconstpointer,
2574                len,
2575                false.into_glib(),
2576                Some(free_data::<A, T>),
2577                Box::into_raw(data) as ffi::gpointer,
2578            ))
2579        }
2580    }
2581}
2582
2583/// A wrapper type around `Variant` handles.
2584#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2585pub struct Handle(pub i32);
2586
2587impl From<i32> for Handle {
2588    fn from(v: i32) -> Self {
2589        Handle(v)
2590    }
2591}
2592
2593impl From<Handle> for i32 {
2594    fn from(v: Handle) -> Self {
2595        v.0
2596    }
2597}
2598
2599impl StaticVariantType for Handle {
2600    fn static_variant_type() -> Cow<'static, VariantTy> {
2601        Cow::Borrowed(VariantTy::HANDLE)
2602    }
2603}
2604
2605impl ToVariant for Handle {
2606    fn to_variant(&self) -> Variant {
2607        unsafe { from_glib_none(ffi::g_variant_new_handle(self.0)) }
2608    }
2609}
2610
2611impl From<Handle> for Variant {
2612    #[inline]
2613    fn from(h: Handle) -> Self {
2614        h.to_variant()
2615    }
2616}
2617
2618impl FromVariant for Handle {
2619    fn from_variant(variant: &Variant) -> Option<Self> {
2620        unsafe {
2621            if variant.is::<Self>() {
2622                Some(Handle(ffi::g_variant_get_handle(variant.to_glib_none().0)))
2623            } else {
2624                None
2625            }
2626        }
2627    }
2628}
2629
2630/// A wrapper type around `Variant` object paths.
2631///
2632/// Values of these type are guaranteed to be valid object paths.
2633#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2634pub struct ObjectPath(String);
2635
2636impl ObjectPath {
2637    pub fn as_str(&self) -> &str {
2638        &self.0
2639    }
2640}
2641
2642impl Display for ObjectPath {
2643    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2644        self.0.fmt(f)
2645    }
2646}
2647
2648impl std::ops::Deref for ObjectPath {
2649    type Target = str;
2650
2651    #[inline]
2652    fn deref(&self) -> &Self::Target {
2653        &self.0
2654    }
2655}
2656
2657impl TryFrom<String> for ObjectPath {
2658    type Error = crate::BoolError;
2659
2660    fn try_from(v: String) -> Result<Self, Self::Error> {
2661        if !Variant::is_object_path(&v) {
2662            return Err(bool_error!("Invalid object path"));
2663        }
2664
2665        Ok(ObjectPath(v))
2666    }
2667}
2668
2669impl<'a> TryFrom<&'a str> for ObjectPath {
2670    type Error = crate::BoolError;
2671
2672    fn try_from(v: &'a str) -> Result<Self, Self::Error> {
2673        ObjectPath::try_from(String::from(v))
2674    }
2675}
2676
2677impl From<ObjectPath> for String {
2678    fn from(v: ObjectPath) -> Self {
2679        v.0
2680    }
2681}
2682
2683impl StaticVariantType for ObjectPath {
2684    fn static_variant_type() -> Cow<'static, VariantTy> {
2685        Cow::Borrowed(VariantTy::OBJECT_PATH)
2686    }
2687}
2688
2689impl ToVariant for ObjectPath {
2690    fn to_variant(&self) -> Variant {
2691        unsafe { from_glib_none(ffi::g_variant_new_object_path(self.0.to_glib_none().0)) }
2692    }
2693}
2694
2695impl From<ObjectPath> for Variant {
2696    #[inline]
2697    fn from(p: ObjectPath) -> Self {
2698        let mut s = p.0;
2699        s.push('\0');
2700        unsafe { Self::from_data_trusted::<ObjectPath, _>(s) }
2701    }
2702}
2703
2704impl FromVariant for ObjectPath {
2705    #[allow(unused_unsafe)]
2706    fn from_variant(variant: &Variant) -> Option<Self> {
2707        unsafe {
2708            if variant.is::<Self>() {
2709                Some(ObjectPath(String::from(variant.str().unwrap())))
2710            } else {
2711                None
2712            }
2713        }
2714    }
2715}
2716
2717/// A wrapper type around `Variant` signatures.
2718///
2719/// Values of these type are guaranteed to be valid signatures.
2720#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2721pub struct Signature(String);
2722
2723impl Signature {
2724    pub fn as_str(&self) -> &str {
2725        &self.0
2726    }
2727}
2728
2729impl std::ops::Deref for Signature {
2730    type Target = str;
2731
2732    #[inline]
2733    fn deref(&self) -> &Self::Target {
2734        &self.0
2735    }
2736}
2737
2738impl TryFrom<String> for Signature {
2739    type Error = crate::BoolError;
2740
2741    fn try_from(v: String) -> Result<Self, Self::Error> {
2742        if !Variant::is_signature(&v) {
2743            return Err(bool_error!("Invalid signature"));
2744        }
2745
2746        Ok(Signature(v))
2747    }
2748}
2749
2750impl<'a> TryFrom<&'a str> for Signature {
2751    type Error = crate::BoolError;
2752
2753    fn try_from(v: &'a str) -> Result<Self, Self::Error> {
2754        Signature::try_from(String::from(v))
2755    }
2756}
2757
2758impl From<Signature> for String {
2759    fn from(v: Signature) -> Self {
2760        v.0
2761    }
2762}
2763
2764impl StaticVariantType for Signature {
2765    fn static_variant_type() -> Cow<'static, VariantTy> {
2766        Cow::Borrowed(VariantTy::SIGNATURE)
2767    }
2768}
2769
2770impl ToVariant for Signature {
2771    fn to_variant(&self) -> Variant {
2772        unsafe { from_glib_none(ffi::g_variant_new_signature(self.0.to_glib_none().0)) }
2773    }
2774}
2775
2776impl From<Signature> for Variant {
2777    #[inline]
2778    fn from(s: Signature) -> Self {
2779        let mut s = s.0;
2780        s.push('\0');
2781        unsafe { Self::from_data_trusted::<Signature, _>(s) }
2782    }
2783}
2784
2785impl FromVariant for Signature {
2786    #[allow(unused_unsafe)]
2787    fn from_variant(variant: &Variant) -> Option<Self> {
2788        unsafe {
2789            if variant.is::<Self>() {
2790                Some(Signature(String::from(variant.str().unwrap())))
2791            } else {
2792                None
2793            }
2794        }
2795    }
2796}
2797
2798#[cfg(test)]
2799mod tests {
2800    use std::collections::{HashMap, HashSet};
2801
2802    use super::*;
2803
2804    macro_rules! unsigned {
2805        ($name:ident, $ty:ident) => {
2806            #[test]
2807            fn $name() {
2808                let mut n = $ty::MAX;
2809                while n > 0 {
2810                    let v = n.to_variant();
2811                    assert_eq!(v.get(), Some(n));
2812                    n /= 2;
2813                }
2814            }
2815        };
2816    }
2817
2818    macro_rules! signed {
2819        ($name:ident, $ty:ident) => {
2820            #[test]
2821            fn $name() {
2822                let mut n = $ty::MAX;
2823                while n > 0 {
2824                    let v = n.to_variant();
2825                    assert_eq!(v.get(), Some(n));
2826                    let v = (-n).to_variant();
2827                    assert_eq!(v.get(), Some(-n));
2828                    n /= 2;
2829                }
2830            }
2831        };
2832    }
2833
2834    unsigned!(test_u8, u8);
2835    unsigned!(test_u16, u16);
2836    unsigned!(test_u32, u32);
2837    unsigned!(test_u64, u64);
2838    signed!(test_i16, i16);
2839    signed!(test_i32, i32);
2840    signed!(test_i64, i64);
2841
2842    #[test]
2843    fn test_str() {
2844        let s = "this is a test";
2845        let v = s.to_variant();
2846        assert_eq!(v.str(), Some(s));
2847        assert_eq!(42u32.to_variant().str(), None);
2848    }
2849
2850    #[test]
2851    fn test_fixed_array() {
2852        let b = b"this is a test";
2853        let v = Variant::array_from_fixed_array(&b[..]);
2854        assert_eq!(v.type_().as_str(), "ay");
2855        assert_eq!(v.fixed_array::<u8>().unwrap(), b);
2856        assert!(42u32.to_variant().fixed_array::<u8>().is_err());
2857
2858        let b = [1u32, 10u32, 100u32];
2859        let v = Variant::array_from_fixed_array(&b);
2860        assert_eq!(v.type_().as_str(), "au");
2861        assert_eq!(v.fixed_array::<u32>().unwrap(), b);
2862        assert!(v.fixed_array::<u8>().is_err());
2863
2864        let b = [true, false, true];
2865        let v = Variant::array_from_fixed_array(&b);
2866        assert_eq!(v.type_().as_str(), "ab");
2867        assert_eq!(v.fixed_array::<bool>().unwrap(), b);
2868        assert!(v.fixed_array::<u8>().is_err());
2869
2870        let b = [1.0f64, 2.0f64, 3.0f64];
2871        let v = Variant::array_from_fixed_array(&b);
2872        assert_eq!(v.type_().as_str(), "ad");
2873        #[allow(clippy::float_cmp)]
2874        {
2875            assert_eq!(v.fixed_array::<f64>().unwrap(), b);
2876        }
2877        assert!(v.fixed_array::<u64>().is_err());
2878    }
2879
2880    #[test]
2881    fn test_fixed_variant_array() {
2882        let b = FixedSizeVariantArray::from(&b"this is a test"[..]);
2883        let v = b.to_variant();
2884        assert_eq!(v.type_().as_str(), "ay");
2885        assert_eq!(
2886            &*v.get::<FixedSizeVariantArray<Vec<u8>, u8>>().unwrap(),
2887            &*b
2888        );
2889
2890        let b = FixedSizeVariantArray::from(vec![1i32, 2, 3]);
2891        let v = b.to_variant();
2892        assert_eq!(v.type_().as_str(), "ai");
2893        assert_eq!(v.get::<FixedSizeVariantArray<Vec<i32>, i32>>().unwrap(), b);
2894    }
2895
2896    #[test]
2897    fn test_string() {
2898        let s = String::from("this is a test");
2899        let v = s.to_variant();
2900        assert_eq!(v.get(), Some(s));
2901        assert_eq!(v.normal_form(), v);
2902    }
2903
2904    #[test]
2905    fn test_eq() {
2906        let v1 = "this is a test".to_variant();
2907        let v2 = "this is a test".to_variant();
2908        let v3 = "test".to_variant();
2909        assert_eq!(v1, v2);
2910        assert_ne!(v1, v3);
2911    }
2912
2913    #[test]
2914    fn test_hash() {
2915        let v1 = "this is a test".to_variant();
2916        let v2 = "this is a test".to_variant();
2917        let v3 = "test".to_variant();
2918        let mut set = HashSet::new();
2919        set.insert(v1);
2920        assert!(set.contains(&v2));
2921        assert!(!set.contains(&v3));
2922
2923        assert_eq!(
2924            <HashMap<&str, (&str, u8, u32)>>::static_variant_type().as_str(),
2925            "a{s(syu)}"
2926        );
2927    }
2928
2929    #[test]
2930    fn test_array() {
2931        assert_eq!(<Vec<&str>>::static_variant_type().as_str(), "as");
2932        assert_eq!(
2933            <Vec<(&str, u8, u32)>>::static_variant_type().as_str(),
2934            "a(syu)"
2935        );
2936        let a = ["foo", "bar", "baz"].to_variant();
2937        assert_eq!(a.normal_form(), a);
2938        assert_eq!(a.array_iter_str().unwrap().len(), 3);
2939        let o = 0u32.to_variant();
2940        assert!(o.array_iter_str().is_err());
2941    }
2942
2943    #[test]
2944    fn test_array_from_iter() {
2945        let a = Variant::array_from_iter::<String>(
2946            ["foo", "bar", "baz"].into_iter().map(|s| s.to_variant()),
2947        );
2948        assert_eq!(a.type_().as_str(), "as");
2949        assert_eq!(a.n_children(), 3);
2950
2951        assert_eq!(a.try_child_get::<String>(0), Ok(Some(String::from("foo"))));
2952        assert_eq!(a.try_child_get::<String>(1), Ok(Some(String::from("bar"))));
2953        assert_eq!(a.try_child_get::<String>(2), Ok(Some(String::from("baz"))));
2954    }
2955
2956    #[test]
2957    fn test_array_collect() {
2958        let a = ["foo", "bar", "baz"].into_iter().collect::<Variant>();
2959        assert_eq!(a.type_().as_str(), "as");
2960        assert_eq!(a.n_children(), 3);
2961
2962        assert_eq!(a.try_child_get::<String>(0), Ok(Some(String::from("foo"))));
2963        assert_eq!(a.try_child_get::<String>(1), Ok(Some(String::from("bar"))));
2964        assert_eq!(a.try_child_get::<String>(2), Ok(Some(String::from("baz"))));
2965    }
2966
2967    #[test]
2968    fn test_tuple() {
2969        assert_eq!(<(&str, u32)>::static_variant_type().as_str(), "(su)");
2970        assert_eq!(<(&str, u8, u32)>::static_variant_type().as_str(), "(syu)");
2971        let a = ("test", 1u8, 2u32).to_variant();
2972        assert_eq!(a.normal_form(), a);
2973        assert_eq!(a.try_child_get::<String>(0), Ok(Some(String::from("test"))));
2974        assert_eq!(a.try_child_get::<u8>(1), Ok(Some(1u8)));
2975        assert_eq!(a.try_child_get::<u32>(2), Ok(Some(2u32)));
2976        assert_eq!(
2977            a.try_get::<(String, u8, u32)>(),
2978            Ok((String::from("test"), 1u8, 2u32))
2979        );
2980    }
2981
2982    #[test]
2983    fn test_tuple_from_iter() {
2984        let a = Variant::tuple_from_iter(["foo".to_variant(), 1u8.to_variant(), 2i32.to_variant()]);
2985        assert_eq!(a.type_().as_str(), "(syi)");
2986        assert_eq!(a.n_children(), 3);
2987
2988        assert_eq!(a.try_child_get::<String>(0), Ok(Some(String::from("foo"))));
2989        assert_eq!(a.try_child_get::<u8>(1), Ok(Some(1u8)));
2990        assert_eq!(a.try_child_get::<i32>(2), Ok(Some(2i32)));
2991    }
2992
2993    #[test]
2994    fn test_empty() {
2995        assert_eq!(<()>::static_variant_type().as_str(), "()");
2996        let a = ().to_variant();
2997        assert_eq!(a.type_().as_str(), "()");
2998        assert_eq!(a.get::<()>(), Some(()));
2999    }
3000
3001    #[test]
3002    fn test_maybe() {
3003        assert!(<Option<()>>::static_variant_type().is_maybe());
3004        let m1 = Some(()).to_variant();
3005        assert_eq!(m1.type_().as_str(), "m()");
3006
3007        assert_eq!(m1.get::<Option<()>>(), Some(Some(())));
3008        assert!(m1.as_maybe().is_some());
3009
3010        let m2 = None::<()>.to_variant();
3011        assert!(m2.as_maybe().is_none());
3012    }
3013
3014    #[test]
3015    fn test_btreemap() {
3016        assert_eq!(
3017            <BTreeMap<String, u32>>::static_variant_type().as_str(),
3018            "a{su}"
3019        );
3020        // Validate that BTreeMap adds entries to dict in sorted order
3021        let mut m = BTreeMap::new();
3022        let total = 20;
3023        for n in 0..total {
3024            let k = format!("v{n:04}");
3025            m.insert(k, n as u32);
3026        }
3027        let v = m.to_variant();
3028        let n = v.n_children();
3029        assert_eq!(total, n);
3030        for n in 0..total {
3031            let child = v
3032                .try_child_get::<DictEntry<String, u32>>(n)
3033                .unwrap()
3034                .unwrap();
3035            assert_eq!(*child.value(), n as u32);
3036        }
3037
3038        assert_eq!(BTreeMap::from_variant(&v).unwrap(), m);
3039    }
3040
3041    #[test]
3042    fn test_get() -> Result<(), Box<dyn std::error::Error>> {
3043        let u = 42u32.to_variant();
3044        assert!(u.get::<i32>().is_none());
3045        assert_eq!(u.get::<u32>().unwrap(), 42);
3046        assert!(u.try_get::<i32>().is_err());
3047        // Test ? conversion
3048        assert_eq!(u.try_get::<u32>()?, 42);
3049        Ok(())
3050    }
3051
3052    #[test]
3053    fn test_byteswap() {
3054        let u = 42u32.to_variant();
3055        assert_eq!(u.byteswap().get::<u32>().unwrap(), 704643072u32);
3056        assert_eq!(u.byteswap().byteswap().get::<u32>().unwrap(), 42u32);
3057    }
3058
3059    #[test]
3060    fn test_try_child() {
3061        let a = ["foo"].to_variant();
3062        assert!(a.try_child_value(0).is_some());
3063        assert_eq!(a.try_child_get::<String>(0).unwrap().unwrap(), "foo");
3064        assert_eq!(a.child_get::<String>(0), "foo");
3065        assert!(a.try_child_get::<u32>(0).is_err());
3066        assert!(a.try_child_value(1).is_none());
3067        assert!(a.try_child_get::<String>(1).unwrap().is_none());
3068        let u = 42u32.to_variant();
3069        assert!(u.try_child_value(0).is_none());
3070        assert!(u.try_child_get::<String>(0).unwrap().is_none());
3071    }
3072
3073    #[test]
3074    fn test_serialize() {
3075        let a = ("test", 1u8, 2u32).to_variant();
3076
3077        let bytes = a.data_as_bytes();
3078        let data = a.data();
3079        let len = a.size();
3080        assert_eq!(bytes.len(), len);
3081        assert_eq!(data.len(), len);
3082
3083        let mut store_data = vec![0u8; len];
3084        assert_eq!(a.store(&mut store_data).unwrap(), len);
3085
3086        assert_eq!(&bytes, data);
3087        assert_eq!(&store_data, data);
3088
3089        let b = Variant::from_data::<(String, u8, u32), _>(store_data);
3090        assert_eq!(a, b);
3091
3092        let c = Variant::from_bytes::<(String, u8, u32)>(&bytes);
3093        assert_eq!(a, c);
3094    }
3095
3096    #[test]
3097    fn test_print_parse() {
3098        let a = ("test", 1u8, 2u32).to_variant();
3099
3100        let a2 = Variant::parse(Some(a.type_()), &a.print(false)).unwrap();
3101        assert_eq!(a, a2);
3102
3103        let a3: Variant = a.to_string().parse().unwrap();
3104        assert_eq!(a, a3);
3105    }
3106
3107    #[cfg(any(unix, windows))]
3108    #[test]
3109    fn test_paths() {
3110        use std::path::PathBuf;
3111
3112        let path = PathBuf::from("foo");
3113        let v = path.to_variant();
3114        assert_eq!(PathBuf::from_variant(&v), Some(path));
3115    }
3116
3117    #[test]
3118    fn test_regression_from_variant_panics() {
3119        let variant = "text".to_variant();
3120        let hashmap: Option<HashMap<u64, u64>> = FromVariant::from_variant(&variant);
3121        assert!(hashmap.is_none());
3122
3123        let variant = HashMap::<u64, u64>::new().to_variant();
3124        let hashmap: Option<HashMap<u64, u64>> = FromVariant::from_variant(&variant);
3125        assert!(hashmap.is_some());
3126    }
3127}