pub trait Any: 'static {
// Required method
fn type_id(&self) -> TypeId;
}
Expand description
一个用来模拟动态类型的 trait。
大多数类型都实现了 Any
。但是,任何包含非 `static’ 引用的类型都不会。
有关更多详细信息,请参见 模块级文档。
Required Methods§
Implementations§
source§impl dyn Any
impl dyn Any
sourcepub fn downcast_ref<T: Any>(&self) -> Option<&T>
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
如果内部值的类型为 T
类型,则返回一些对内部值的引用,如果不是,则返回 None
。
Examples
use std::any::Any;
fn print_if_string(s: &dyn Any) {
if let Some(string) = s.downcast_ref::<String>() {
println!("It's a string({}): '{}'", string.len(), string);
} else {
println!("Not a string...");
}
}
print_if_string(&0);
print_if_string(&"cookie monster".to_string());
Runsourcepub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
如果内部值的类型为 T
类型,则返回一些对内部值的引用,如果不是,则返回 None
。
Examples
use std::any::Any;
fn modify_if_u32(s: &mut dyn Any) {
if let Some(num) = s.downcast_mut::<u32>() {
*num = 42;
}
}
let mut x = 10u32;
let mut s = "starlord".to_string();
modify_if_u32(&mut x);
modify_if_u32(&mut s);
assert_eq!(x, 42);
assert_eq!(&s, "starlord");
Runsourcepub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
downcast_unchecked
#90850)sourcepub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
downcast_unchecked
#90850)source§impl dyn Any + Send
impl dyn Any + Send
sourcepub fn downcast_ref<T: Any>(&self) -> Option<&T>
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
转发到在类型 dyn Any
上定义的方法。
Examples
use std::any::Any;
fn print_if_string(s: &(dyn Any + Send)) {
if let Some(string) = s.downcast_ref::<String>() {
println!("It's a string({}): '{}'", string.len(), string);
} else {
println!("Not a string...");
}
}
print_if_string(&0);
print_if_string(&"cookie monster".to_string());
Runsourcepub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
转发到在类型 dyn Any
上定义的方法。
Examples
use std::any::Any;
fn modify_if_u32(s: &mut (dyn Any + Send)) {
if let Some(num) = s.downcast_mut::<u32>() {
*num = 42;
}
}
let mut x = 10u32;
let mut s = "starlord".to_string();
modify_if_u32(&mut x);
modify_if_u32(&mut s);
assert_eq!(x, 42);
assert_eq!(&s, "starlord");
Runsourcepub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
downcast_unchecked
#90850)sourcepub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
downcast_unchecked
#90850)source§impl dyn Any + Send + Sync
impl dyn Any + Send + Sync
1.28.0 · sourcepub fn downcast_ref<T: Any>(&self) -> Option<&T>
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
转发到在 Any
类型上定义的方法。
Examples
use std::any::Any;
fn print_if_string(s: &(dyn Any + Send + Sync)) {
if let Some(string) = s.downcast_ref::<String>() {
println!("It's a string({}): '{}'", string.len(), string);
} else {
println!("Not a string...");
}
}
print_if_string(&0);
print_if_string(&"cookie monster".to_string());
Run1.28.0 · sourcepub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
转发到在 Any
类型上定义的方法。
Examples
use std::any::Any;
fn modify_if_u32(s: &mut (dyn Any + Send + Sync)) {
if let Some(num) = s.downcast_mut::<u32>() {
*num = 42;
}
}
let mut x = 10u32;
let mut s = "starlord".to_string();
modify_if_u32(&mut x);
modify_if_u32(&mut s);
assert_eq!(x, 42);
assert_eq!(&s, "starlord");
Runsourcepub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
downcast_unchecked
#90850)sourcepub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
🔬This is a nightly-only experimental API. (downcast_unchecked
#90850)
pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
downcast_unchecked
#90850)