Movatterモバイル変換


[0]ホーム

URL:


Any

std::any

TraitAny 

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

A trait to emulate dynamic typing.

Most types implementAny. However, any type which contains a non-'static reference does not.See themodule-level documentation for more details.

Required Methods§

1.34.0 ·Source

fntype_id(&self) ->TypeId

Gets theTypeId ofself.

If called on adyn Any trait object(or a trait object of a subtrait ofAny),this returns theTypeId of the underlyingconcrete type, not that ofdyn Any itself.

§Examples
usestd::any::{Any, TypeId};fnis_string(s:&dynAny) -> bool {    TypeId::of::<String>() == s.type_id()}assert_eq!(is_string(&0),false);assert_eq!(is_string(&"cookie monster".to_string()),true);

Implementations§

Source§

impl dynAny

1.0.0 ·Source

pub fnis<T>(&self) ->bool
where T:Any,

Returnstrue if the inner type is the same asT.

§Examples
usestd::any::Any;fnis_string(s:&dynAny) {ifs.is::<String>() {println!("It's a string!");    }else{println!("Not a string...");    }}is_string(&0);is_string(&"cookie monster".to_string());
1.0.0 ·Source

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

Returns some reference to the inner value if it is of typeT, orNone if it isn’t.

§Examples
usestd::any::Any;fnprint_if_string(s:&dynAny) {if letSome(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());
1.0.0 ·Source

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

Returns some mutable reference to the inner value if it is of typeT, orNone if it isn’t.

§Examples
usestd::any::Any;fnmodify_if_u32(s:&mutdynAny) {if letSome(num) = s.downcast_mut::<u32>() {*num =42;    }}letmutx =10u32;letmuts ="starlord".to_string();modify_if_u32(&mutx);modify_if_u32(&muts);assert_eq!(x,42);assert_eq!(&s,"starlord");
Source

pub unsafe fndowncast_unchecked_ref<T>(&self) ->&T
where T:Any,

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

Returns a reference to the inner value as typedyn T.

§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked_ref::<usize>(),1);}
§Safety

The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.

Source

pub unsafe fndowncast_unchecked_mut<T>(&mut self) ->&mut T
where T:Any,

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

Returns a mutable reference to the inner value as typedyn T.

§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letmutx: Box<dynAny> = Box::new(1_usize);unsafe{*x.downcast_unchecked_mut::<usize>() +=1;}assert_eq!(*x.downcast_ref::<usize>().unwrap(),2);
§Safety

The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.

Source§

impl dynAny +Send

1.0.0 ·Source

pub fnis<T>(&self) ->bool
where T:Any,

Forwards to the method defined on the typedyn Any.

§Examples
usestd::any::Any;fnis_string(s:&(dynAny + Send)) {ifs.is::<String>() {println!("It's a string!");    }else{println!("Not a string...");    }}is_string(&0);is_string(&"cookie monster".to_string());
1.0.0 ·Source

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

Forwards to the method defined on the typedyn Any.

§Examples
usestd::any::Any;fnprint_if_string(s:&(dynAny + Send)) {if letSome(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());
1.0.0 ·Source

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

Forwards to the method defined on the typedyn Any.

§Examples
usestd::any::Any;fnmodify_if_u32(s:&mut(dynAny + Send)) {if letSome(num) = s.downcast_mut::<u32>() {*num =42;    }}letmutx =10u32;letmuts ="starlord".to_string();modify_if_u32(&mutx);modify_if_u32(&muts);assert_eq!(x,42);assert_eq!(&s,"starlord");
Source

pub unsafe fndowncast_unchecked_ref<T>(&self) ->&T
where T:Any,

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

Forwards to the method defined on the typedyn Any.

§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked_ref::<usize>(),1);}
§Safety

The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.

Source

pub unsafe fndowncast_unchecked_mut<T>(&mut self) ->&mut T
where T:Any,

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

Forwards to the method defined on the typedyn Any.

§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letmutx: Box<dynAny> = Box::new(1_usize);unsafe{*x.downcast_unchecked_mut::<usize>() +=1;}assert_eq!(*x.downcast_ref::<usize>().unwrap(),2);
§Safety

The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.

Source§

impl dynAny +Send +Sync

1.28.0 ·Source

pub fnis<T>(&self) ->bool
where T:Any,

Forwards to the method defined on the typeAny.

§Examples
usestd::any::Any;fnis_string(s:&(dynAny + Send + Sync)) {ifs.is::<String>() {println!("It's a string!");    }else{println!("Not a string...");    }}is_string(&0);is_string(&"cookie monster".to_string());
1.28.0 ·Source

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

Forwards to the method defined on the typeAny.

§Examples
usestd::any::Any;fnprint_if_string(s:&(dynAny + Send + Sync)) {if letSome(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());
1.28.0 ·Source

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

Forwards to the method defined on the typeAny.

§Examples
usestd::any::Any;fnmodify_if_u32(s:&mut(dynAny + Send + Sync)) {if letSome(num) = s.downcast_mut::<u32>() {*num =42;    }}letmutx =10u32;letmuts ="starlord".to_string();modify_if_u32(&mutx);modify_if_u32(&muts);assert_eq!(x,42);assert_eq!(&s,"starlord");
Source

pub unsafe fndowncast_unchecked_ref<T>(&self) ->&T
where T:Any,

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

Forwards to the method defined on the typeAny.

§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked_ref::<usize>(),1);}
§Safety

The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.

Source

pub unsafe fndowncast_unchecked_mut<T>(&mut self) ->&mut T
where T:Any,

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

Forwards to the method defined on the typeAny.

§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letmutx: Box<dynAny> = Box::new(1_usize);unsafe{*x.downcast_unchecked_mut::<usize>() +=1;}assert_eq!(*x.downcast_ref::<usize>().unwrap(),2);
§Safety

The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.

Source§

impl<A>Box<dynAny, A>
where A:Allocator,

1.0.0 ·Source

pub fndowncast<T>(self) ->Result<Box<T, A>,Box<dynAny, A>>
where T:Any,

Attempts to downcast the box to a concrete type.

§Examples
usestd::any::Any;fnprint_if_string(value: Box<dynAny>) {if letOk(string) = value.downcast::<String>() {println!("String ({}): {}", string.len(), string);    }}letmy_string ="Hello World".to_string();print_if_string(Box::new(my_string));print_if_string(Box::new(0i8));
Source

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

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

Downcasts the box to a concrete type.

For a safe alternative seedowncast.

§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked::<usize>(),1);}
§Safety

The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.

Source§

impl<A>Box<dynAny +Send, A>
where A:Allocator,

1.0.0 ·Source

pub fndowncast<T>(self) ->Result<Box<T, A>,Box<dynAny +Send, A>>
where T:Any,

Attempts to downcast the box to a concrete type.

§Examples
usestd::any::Any;fnprint_if_string(value: Box<dynAny + Send>) {if letOk(string) = value.downcast::<String>() {println!("String ({}): {}", string.len(), string);    }}letmy_string ="Hello World".to_string();print_if_string(Box::new(my_string));print_if_string(Box::new(0i8));
Source

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

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

Downcasts the box to a concrete type.

For a safe alternative seedowncast.

§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny + Send> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked::<usize>(),1);}
§Safety

The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.

Source§

impl<A>Box<dynAny +Send +Sync, A>
where A:Allocator,

1.51.0 ·Source

pub fndowncast<T>(self) ->Result<Box<T, A>,Box<dynAny +Send +Sync, A>>
where T:Any,

Attempts to downcast the box to a concrete type.

§Examples
usestd::any::Any;fnprint_if_string(value: Box<dynAny + Send + Sync>) {if letOk(string) = value.downcast::<String>() {println!("String ({}): {}", string.len(), string);    }}letmy_string ="Hello World".to_string();print_if_string(Box::new(my_string));print_if_string(Box::new(0i8));
Source

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

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

Downcasts the box to a concrete type.

For a safe alternative seedowncast.

§Examples
#![feature(downcast_unchecked)]usestd::any::Any;letx: Box<dynAny + Send + Sync> = Box::new(1_usize);unsafe{assert_eq!(*x.downcast_unchecked::<usize>(),1);}
§Safety

The contained value must be of typeT. Calling this methodwith the incorrect type isundefined behavior.

Trait Implementations§

1.0.0 ·Source§

implDebug for dynAny

Source§

fnfmt(&self, f: &mutFormatter<'_>) ->Result<(),Error>

Formats the value using the given formatter.Read more
1.0.0 ·Source§

implDebug for dynAny +Send

Source§

fnfmt(&self, f: &mutFormatter<'_>) ->Result<(),Error>

Formats the value using the given formatter.Read more
1.28.0 ·Source§

implDebug for dynAny +Send +Sync

Source§

fnfmt(&self, f: &mutFormatter<'_>) ->Result<(),Error>

Formats the value using the given formatter.Read more

Implementors§

1.0.0 ·Source§

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


[8]ページ先頭

©2009-2026 Movatter.jp