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.
use glib::prelude::*;
use glib::{AnyValue, Value};
let v = AnyValue::new(String::from("123")).to_value();
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")));
Create a new AnyValue
from val
Attempt the value to its concrete type.
Returns true
if the boxed type is the same as T
.
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());
}
Returns some reference to the boxed value if it is of type T
, or
None
if it isn't.
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());
}
Forwards to the method defined on the type Any
.
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());
}
Forwards to the method defined on the type Any
.
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());
}
Forwards to the method defined on the type Any
.
use std::any::Any;
fn is_string(s: &(Any + Send + Sync)) {
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());
}
Forwards to the method defined on the type Any
.
use std::any::Any;
fn print_if_string(s: &(Any + Send + Sync)) {
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());
}
Performs copy-assignment from source
. Read more
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Returns the type identifier of Self
.