Struct glib::value::AnyValue
[−]
[src]
pub struct AnyValue { /* fields omitted */ }
A container type that allows storing any 'static
type that implements Any
and Clone
to be
stored in a Value
.
See the module documentation for more details.
Examples
use glib::prelude::*; // or `use gtk::prelude::*;` use glib::{AnyValue, Value}; // Store a Rust string inside a Value let v = AnyValue::new(String::from("123")).to_value(); // Retrieve the Rust String from the the Value again let any_v = v.get::<&AnyValue>() .expect("Value did not actually contain an AnyValue"); assert_eq!(any_v.downcast_ref::<String>(), Some(&String::from("123")));
Methods
impl AnyValue
[src]
pub fn new<T: Any + Clone + 'static>(val: T) -> Self
[src]
Create a new AnyValue
from val
pub fn downcast<T: Any + Clone + 'static>(self) -> Result<T, Self>
[src]
Attempt the value to its concrete type.
Methods from Deref<Target = Any>
pub fn is<T>(&self) -> bool where
T: Any,
1.0.0[src]
T: Any,
Returns true
if the boxed type is the same as T
.
Examples
use std::any::Any; fn is_string(s: &Any) { if s.is::<String>() { println!("It's a string!"); } else { println!("Not a string..."); } } fn main() { is_string(&0); is_string(&"cookie monster".to_string()); }
pub fn downcast_ref<T>(&self) -> Option<&T> where
T: Any,
1.0.0[src]
T: Any,
Returns some reference to the boxed value if it is of type T
, or
None
if it isn't.
Examples
use std::any::Any; fn print_if_string(s: &Any) { if let Some(string) = s.downcast_ref::<String>() { println!("It's a string({}): '{}'", string.len(), string); } else { println!("Not a string..."); } } fn main() { print_if_string(&0); print_if_string(&"cookie monster".to_string()); }
pub fn is<T>(&self) -> bool where
T: Any,
1.0.0[src]
T: Any,
Forwards to the method defined on the type Any
.
Examples
use std::any::Any; fn is_string(s: &(Any + Send)) { if s.is::<String>() { println!("It's a string!"); } else { println!("Not a string..."); } } fn main() { is_string(&0); is_string(&"cookie monster".to_string()); }
pub fn downcast_ref<T>(&self) -> Option<&T> where
T: Any,
1.0.0[src]
T: Any,
Forwards to the method defined on the type Any
.
Examples
use std::any::Any; fn print_if_string(s: &(Any + Send)) { if let Some(string) = s.downcast_ref::<String>() { println!("It's a string({}): '{}'", string.len(), string); } else { println!("Not a string..."); } } fn main() { print_if_string(&0); print_if_string(&"cookie monster".to_string()); }
Trait Implementations
impl Clone for AnyValue
[src]
fn clone(&self) -> Self
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Debug for AnyValue
[src]
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
[src]
Formats the value using the given formatter. Read more
impl Deref for AnyValue
[src]
type Target = Any
The resulting type after dereferencing.
fn deref(&self) -> &Any
[src]
Dereferences the value.
impl<'a> FromValueOptional<'a> for &'a AnyValue
[src]
unsafe fn from_value_optional(value: &'a Value) -> Option<Self>
[src]
impl SetValue for AnyValue
[src]
impl StaticType for AnyValue
[src]
fn static_type() -> Type
[src]
Returns the type identifier of Self
.