Module variant

Source
Expand description

Variant binding and helper traits.

Variant is an immutable dynamically-typed generic container. Its type and value are defined at construction and never change.

Variant types are described by VariantType “type strings”.

Although GVariant supports arbitrarily complex types, this binding is currently limited to the basic ones: bool, u8, i16, u16, i32, u32, i64, u64, f64 and &str/String.

§Examples

use glib::prelude::*; // or `use gtk::prelude::*;`
use glib::Variant;

// Using the `ToVariant` trait.
let num = 10.to_variant();

// `is` tests the type of the value.
assert!(num.is::<i32>());

// `get` tries to extract the value.
assert_eq!(num.get::<i32>(), Some(10));
assert_eq!(num.get::<u32>(), None);

// `Variant` implements `From`
let hello = Variant::from("Hello!");

// `get_str` tries to borrow a string slice.
assert_eq!(hello.get_str(), Some("Hello!"));
assert_eq!(num.get_str(), None);

Structs§

Variant
A generic immutable value capable of carrying various types.

Traits§

FromVariant
Extracts a value.
StaticVariantType
Returns VariantType of Self.
ToVariant
Converts to Variant.