[][src]Module glib::value

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>).

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 an optional value of the specified type
// and returns an `Err` if the type doesn't match.
assert_eq!(num.get(), Ok(Some(10)));
assert!(num.get::<String>().is_err());
assert_eq!(hello.get(), Ok(Some(String::from("Hello!"))));
assert_eq!(hello.get::<String>(), Ok(Some(String::from("Hello!"))));
assert_eq!(str_none.get::<String>(), Ok(None));

// `get_some` tries to get a value of the specified non-optional type
// and returns an `Err` if the type doesn't match.
assert_eq!(num.get_some::<i32>(), Ok(10));
assert!(num.get_some::<bool>().is_err());

// `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

GetError

An error returned from the get or get_some functions on a Value

SendValue

A version of Value for storing Send types, that implements Send itself.

TypedValue

A statically typed Value.

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 SendValue.

ToValue

Converts to Value.