Module glib::value [−][src]
Value binding and helper traits.
The type of a Value is dynamic in that it generally
isn't known at compile time but once created a Value can't change its
type.
TypedValue has a statically known type and
dereferences to Value so it can be used everywhere Value references are
accepted.
SendValue is a version of Value
that can only store types that implement Send and as such implements Send itself. It
dereferences to Value so it can be used everywhere Value references are accepted.
Supported types are bool, i8, u8, i32, u32, i64, u64, f32,
f64, String and objects (T: IsA<Object>).
In addition any 'static type implementing Any and Clone can be stored in a
Value by using AnyValue or
AnySendValue.
Examples
use glib::prelude::*; // or `use gtk::prelude::*;` use glib::{TypedValue, Value}; // Value and TypedValue implement From<&i32>, From<&str> // and From<Option<&str>>. Another option is the `ToValue` trait. let mut num = 10.to_value(); let mut hello = Value::from("Hello!"); let none: Option<&str> = None; let str_none = Value::from(none.clone()); let typed_str_none = TypedValue::from(none); // `is` tests the type of the value. assert!(num.is::<i32>()); assert!(hello.is::<String>()); // `get` tries to get a value of specific type and returns None // if the type doesn't match or the value is None. assert_eq!(num.get(), Some(10)); assert_eq!(num.get::<String>(), None); assert_eq!(hello.get(), Some(String::from("Hello!"))); assert_eq!(hello.get::<String>(), Some(String::from("Hello!"))); assert_eq!(str_none.get::<String>(), None); // `typed` tries to convert a `Value` to `TypedValue`. let mut typed_num = num.downcast::<i32>().unwrap(); let mut typed_hello = hello.downcast::<String>().unwrap(); // `str_none` is not an `i32` assert!(str_none.downcast::<i32>().is_err()); // `get` assert!(typed_hello.get().unwrap() == "Hello!"); assert!(typed_str_none.get() == None); // Numeric types can't have value `None`, `get` always returns `Some`. // Such types have `get_some`, which avoids unnecessary `unwrap`ping. assert_eq!(typed_num.get().unwrap(), 10); assert_eq!(typed_num.get_some(), 10); // `set_none` sets the value to `None` if the type supports it. typed_hello.set_none(); assert!(typed_hello.get().is_none()); // `set` takes an optional reference for types that support `None`. typed_hello.set(Some("Hello again!")); assert!(typed_hello.get().unwrap() == "Hello again!"); // `set_some` is the only setter for types that don't support `None`. typed_num.set_some(&20); assert_eq!(typed_num.get_some(), 20);
Structs
| AnySendValue |
A container type that allows storing any |
| AnyValue |
A container type that allows storing any |
| SendValue |
A version of |
| TypedValue |
A statically typed |
| Value |
A generic value capable of carrying various types. |
| ValueArray |
Traits
| FromValue |
Extracts a value. |
| FromValueOptional |
Extracts a value. |
| SetValue |
Sets a value. |
| SetValueOptional |
Sets a value. |
| ToSendValue |
Converts to |
| ToValue |
Converts to |