pub struct Type { /* private fields */ }
Expand description
The base representation of a Rust type.
When possible, it is recommended to use &'static TypeInfo
instead of this
as it provides more information as well as being smaller
(since a reference only takes the same number of bytes as a usize
).
However, where a static reference to TypeInfo
is not possible,
such as with trait objects and other types that can’t implement Typed
,
this type can be used instead.
It only requires that the type implements TypePath
.
And unlike TypeInfo
, this type implements Copy
, Eq
, and Hash
,
making it useful as a key type.
It’s especially helpful when compared to TypeId
as it can provide the
actual [type path] when debugging, while still having the same performance
as hashing/comparing TypeId
directly—at the cost of a little more memory.
§Examples
use bevy_reflect::{Type, TypePath};
fn assert_char<T: ?Sized + TypePath>(t: &T) -> Result<(), String> {
let ty = Type::of::<T>();
if Type::of::<char>() == ty {
Ok(())
} else {
Err(format!("expected `char`, got `{}`", ty.path()))
}
}
assert_eq!(
assert_char(&'a'),
Ok(())
);
assert_eq!(
assert_char(&String::from("Hello, world!")),
Err(String::from("expected `char`, got `alloc::string::String`"))
);
Implementations§
Source§impl Type
impl Type
Sourcepub fn path(&self) -> &'static str
pub fn path(&self) -> &'static str
See TypePath::type_path
.
Sourcepub fn short_path(&self) -> &'static str
pub fn short_path(&self) -> &'static str
Sourcepub fn ident(&self) -> Option<&'static str>
pub fn ident(&self) -> Option<&'static str>
See TypePath::type_ident
.
Sourcepub fn crate_name(&self) -> Option<&'static str>
pub fn crate_name(&self) -> Option<&'static str>
See TypePath::crate_name
.
Sourcepub fn module_path(&self) -> Option<&'static str>
pub fn module_path(&self) -> Option<&'static str>
Sourcepub fn type_path_table(&self) -> &TypePathTable
pub fn type_path_table(&self) -> &TypePathTable
A representation of the type path of this.
Provides dynamic access to all methods on TypePath
.
Trait Implementations§
Source§impl Debug for Type
impl Debug for Type
impl Copy for Type
impl Eq for Type
Auto Trait Implementations§
impl Freeze for Type
impl RefUnwindSafe for Type
impl Send for Type
impl Sync for Type
impl Unpin for Type
impl UnwindSafe for Type
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.