Trait std::any::Any

1.0.0 · source ·
pub trait Any: 'static {
    // Required method
    fn type_id(&self) -> TypeId;
}
Expand description

一个用来模拟动态类型的 trait。

大多数类型都实现了 Any。但是,任何包含非 `static’ 引用的类型都不会。 有关更多详细信息,请参见 模块级文档

Required Methods§

1.34.0 · source

fn type_id(&self) -> TypeId

获取 selfTypeId

Examples
use std::any::{Any, TypeId};

fn is_string(s: &dyn Any) -> bool {
    TypeId::of::<String>() == s.type_id()
}

assert_eq!(is_string(&0), false);
assert_eq!(is_string(&"cookie monster".to_string()), true);
Run

Implementations§

source§

impl<A> Box<dyn Any + 'static, A>where A: Allocator,

source

pub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any + 'static, A>>where T: Any,

尝试将 box 转换为具体类型。

Examples
use std::any::Any;

fn print_if_string(value: Box<dyn Any>) {
    if let Ok(string) = value.downcast::<String>() {
        println!("String ({}): {}", string.len(), string);
    }
}

let my_string = "Hello World".to_string();
print_if_string(Box::new(my_string));
print_if_string(Box::new(0i8));
Run
source

pub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>where T: Any,

🔬This is a nightly-only experimental API. (downcast_unchecked #90850)

将 box 向下转换为具体类型。

有关安全的替代方案,请参见 downcast

Examples
#![feature(downcast_unchecked)]

use std::any::Any;

let x: Box<dyn Any> = Box::new(1_usize);

unsafe {
    assert_eq!(*x.downcast_unchecked::<usize>(), 1);
}
Run
Safety

包含的值必须是 T 类型。 使用不正确的类型调用此方法是 未定义的行为

source§

impl<A> Box<dyn Any + Send + 'static, A>where A: Allocator,

source

pub fn downcast<T>(self) -> Result<Box<T, A>, Box<dyn Any + Send + 'static, A>>where T: Any,

尝试将 box 转换为具体类型。

Examples
use std::any::Any;

fn print_if_string(value: Box<dyn Any + Send>) {
    if let Ok(string) = value.downcast::<String>() {
        println!("String ({}): {}", string.len(), string);
    }
}

let my_string = "Hello World".to_string();
print_if_string(Box::new(my_string));
print_if_string(Box::new(0i8));
Run
source

pub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>where T: Any,

🔬This is a nightly-only experimental API. (downcast_unchecked #90850)

将 box 向下转换为具体类型。

有关安全的替代方案,请参见 downcast

Examples
#![feature(downcast_unchecked)]

use std::any::Any;

let x: Box<dyn Any + Send> = Box::new(1_usize);

unsafe {
    assert_eq!(*x.downcast_unchecked::<usize>(), 1);
}
Run
Safety

包含的值必须是 T 类型。 使用不正确的类型调用此方法是 未定义的行为

source§

impl<A> Box<dyn Any + Send + Sync + 'static, A>where A: Allocator,

1.51.0 · source

pub fn downcast<T>( self ) -> Result<Box<T, A>, Box<dyn Any + Send + Sync + 'static, A>>where T: Any,

尝试将 box 转换为具体类型。

Examples
use std::any::Any;

fn print_if_string(value: Box<dyn Any + Send + Sync>) {
    if let Ok(string) = value.downcast::<String>() {
        println!("String ({}): {}", string.len(), string);
    }
}

let my_string = "Hello World".to_string();
print_if_string(Box::new(my_string));
print_if_string(Box::new(0i8));
Run
source

pub unsafe fn downcast_unchecked<T>(self) -> Box<T, A>where T: Any,

🔬This is a nightly-only experimental API. (downcast_unchecked #90850)

将 box 向下转换为具体类型。

有关安全的替代方案,请参见 downcast

Examples
#![feature(downcast_unchecked)]

use std::any::Any;

let x: Box<dyn Any + Send + Sync> = Box::new(1_usize);

unsafe {
    assert_eq!(*x.downcast_unchecked::<usize>(), 1);
}
Run
Safety

包含的值必须是 T 类型。 使用不正确的类型调用此方法是 未定义的行为

source§

impl dyn Any + 'static

source

pub fn is<T>(&self) -> boolwhere T: Any,

如果内部类型与 T 相同,则返回 true

Examples
use std::any::Any;

fn is_string(s: &dyn Any) {
    if s.is::<String>() {
        println!("It's a string!");
    } else {
        println!("Not a string...");
    }
}

is_string(&0);
is_string(&"cookie monster".to_string());
Run
source

pub fn downcast_ref<T>(&self) -> Option<&T>where T: Any,

如果内部值的类型为 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());
Run
source

pub fn downcast_mut<T>(&mut self) -> Option<&mut T>where T: Any,

如果内部值的类型为 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");
Run
source

pub unsafe fn downcast_ref_unchecked<T>(&self) -> &Twhere T: Any,

🔬This is a nightly-only experimental API. (downcast_unchecked #90850)

返回对内部值的引用,类型为 dyn T

Examples
#![feature(downcast_unchecked)]

use std::any::Any;

let x: Box<dyn Any> = Box::new(1_usize);

unsafe {
    assert_eq!(*x.downcast_ref_unchecked::<usize>(), 1);
}
Run
Safety

包含的值必须是 T 类型。 使用不正确的类型调用此方法是 未定义的行为

source

pub unsafe fn downcast_mut_unchecked<T>(&mut self) -> &mut Twhere T: Any,

🔬This is a nightly-only experimental API. (downcast_unchecked #90850)

返回对内部值的可变引用,类型为 dyn T

Examples
#![feature(downcast_unchecked)]

use std::any::Any;

let mut x: Box<dyn Any> = Box::new(1_usize);

unsafe {
    *x.downcast_mut_unchecked::<usize>() += 1;
}

assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
Run
Safety

包含的值必须是 T 类型。 使用不正确的类型调用此方法是 未定义的行为

source§

impl dyn Any + Send + 'static

source

pub fn is<T>(&self) -> boolwhere T: Any,

转发到在类型 dyn Any 上定义的方法。

Examples
use std::any::Any;

fn is_string(s: &(dyn Any + Send)) {
    if s.is::<String>() {
        println!("It's a string!");
    } else {
        println!("Not a string...");
    }
}

is_string(&0);
is_string(&"cookie monster".to_string());
Run
source

pub fn downcast_ref<T>(&self) -> Option<&T>where T: Any,

转发到在类型 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());
Run
source

pub fn downcast_mut<T>(&mut self) -> Option<&mut T>where T: Any,

转发到在类型 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");
Run
source

pub unsafe fn downcast_ref_unchecked<T>(&self) -> &Twhere T: Any,

🔬This is a nightly-only experimental API. (downcast_unchecked #90850)

转发到在类型 dyn Any 上定义的方法。

Examples
#![feature(downcast_unchecked)]

use std::any::Any;

let x: Box<dyn Any> = Box::new(1_usize);

unsafe {
    assert_eq!(*x.downcast_ref_unchecked::<usize>(), 1);
}
Run
Safety

dyn Any 类型上的方法相同。

source

pub unsafe fn downcast_mut_unchecked<T>(&mut self) -> &mut Twhere T: Any,

🔬This is a nightly-only experimental API. (downcast_unchecked #90850)

转发到在类型 dyn Any 上定义的方法。

Examples
#![feature(downcast_unchecked)]

use std::any::Any;

let mut x: Box<dyn Any> = Box::new(1_usize);

unsafe {
    *x.downcast_mut_unchecked::<usize>() += 1;
}

assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
Run
Safety

dyn Any 类型上的方法相同。

source§

impl dyn Any + Send + Sync + 'static

1.28.0 · source

pub fn is<T>(&self) -> boolwhere T: Any,

转发到在 Any 类型上定义的方法。

Examples
use std::any::Any;

fn is_string(s: &(dyn Any + Send + Sync)) {
    if s.is::<String>() {
        println!("It's a string!");
    } else {
        println!("Not a string...");
    }
}

is_string(&0);
is_string(&"cookie monster".to_string());
Run
1.28.0 · source

pub fn downcast_ref<T>(&self) -> Option<&T>where T: Any,

转发到在 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());
Run
1.28.0 · source

pub fn downcast_mut<T>(&mut self) -> Option<&mut T>where T: Any,

转发到在 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");
Run
source

pub unsafe fn downcast_ref_unchecked<T>(&self) -> &Twhere T: Any,

🔬This is a nightly-only experimental API. (downcast_unchecked #90850)

转发到在 Any 类型上定义的方法。

Examples
#![feature(downcast_unchecked)]

use std::any::Any;

let x: Box<dyn Any> = Box::new(1_usize);

unsafe {
    assert_eq!(*x.downcast_ref_unchecked::<usize>(), 1);
}
Run
source

pub unsafe fn downcast_mut_unchecked<T>(&mut self) -> &mut Twhere T: Any,

🔬This is a nightly-only experimental API. (downcast_unchecked #90850)

转发到在 Any 类型上定义的方法。

Examples
#![feature(downcast_unchecked)]

use std::any::Any;

let mut x: Box<dyn Any> = Box::new(1_usize);

unsafe {
    *x.downcast_mut_unchecked::<usize>() += 1;
}

assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
Run

Trait Implementations§

source§

impl Debug for dyn Any + 'static

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

使用给定的格式化程序格式化该值。 Read more
source§

impl Debug for dyn Any + Send + 'static

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

使用给定的格式化程序格式化该值。 Read more
1.28.0 · source§

impl Debug for dyn Any + Send + Sync + 'static

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

使用给定的格式化程序格式化该值。 Read more

Implementors§

source§

impl<T> Any for Twhere T: 'static + ?Sized,