Struct TypeId
struct TypeId { ... }
A TypeId represents a globally unique identifier for a type.
Each TypeId is an opaque object which does not allow inspection of what's
inside but does allow basic operations such as cloning, comparison,
printing, and showing.
A TypeId is currently only available for types which ascribe to 'static,
but this limitation may be removed in the future.
While TypeId implements Hash, PartialOrd, and Ord, it is worth
noting that the hashes and ordering will vary between Rust releases. Beware
of relying on them inside of your code!
Layout
Like other Rust-representation types, TypeId's size and layout are unstable.
In particular, this means that you cannot rely on the size and layout of TypeId remaining the
same between Rust releases; they are subject to change without prior notice between Rust
releases.
Danger of Improper Variance
You might think that subtyping is impossible between two static types,
but this is false; there exists a static type with a static subtype.
To wit, fn(&str), which is short for for<'any> fn(&'any str), and
fn(&'static str), are two distinct, static types, and yet,
fn(&str) is a subtype of fn(&'static str), since any value of type
fn(&str) can be used where a value of type fn(&'static str) is needed.
This means that abstractions around TypeId, despite its
'static bound on arguments, still need to worry about unnecessary
and improper variance: it is advisable to strive for invariance
first. The usability impact will be negligible, while the reduction
in the risk of unsoundness will be most welcome.
Examples
Suppose SubType is a subtype of SuperType, that is,
a value of type SubType can be used wherever
a value of type SuperType is expected.
Suppose also that CoVar<T> is a generic type, which is covariant over T
(like many other types, including PhantomData<T> and Vec<T>).
Then, by covariance, CoVar<SubType> is a subtype of CoVar<SuperType>,
that is, a value of type CoVar<SubType> can be used wherever
a value of type CoVar<SuperType> is expected.
Then if CoVar<SuperType> relies on TypeId::of::<SuperType>() to uphold any invariants,
those invariants may be broken because a value of type CoVar<SuperType> can be created
without going through any of its methods, like so:
type SubType = fn;
type SuperType = fn;
type CoVar<T> = ; // imagine something more complicated
let sub: = new;
// we have a `CoVar<SuperType>` instance without
// *ever* having called `CoVar::<SuperType>::new()`!
let fake_super: = sub;
The following is an example program that tries to use TypeId::of to
implement a generic type Unique<T> that guarantees unique instances for each Unique<T>,
that is, and for each type T there can be at most one value of type Unique<T> at any time.
use Unique;
// `OtherRing` is a subtype of `TheOneRing`. Both are 'static, and thus have a TypeId.
type TheOneRing = fn;
type OtherRing = fn;
Implementations
impl TypeId
const fn of<T: ?Sized + 'static>() -> TypeIdReturns the
TypeIdof the generic type parameter.Examples
use ; Sized + Any> assert_eq!; assert_eq!;
impl crate::any::TypeId
const fn info(self: Self) -> TypeCompute the type information of a concrete type. It can only be called at compile time.
impl Clone for TypeId
fn clone(self: &Self) -> TypeId
impl Copy for TypeId
impl Debug for TypeId
fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error>
impl Eq for TypeId
impl Freeze for TypeId
impl Hash for TypeId
fn hash<H: hash::Hasher>(self: &Self, state: &mut H)
impl Ord for TypeId
fn cmp(self: &Self, other: &TypeId) -> $crate::cmp::Ordering
impl PartialEq for TypeId
fn eq(self: &Self, other: &Self) -> bool
impl PartialOrd for TypeId
fn partial_cmp(self: &Self, other: &TypeId) -> $crate::option::Option<$crate::cmp::Ordering>
impl RefUnwindSafe for TypeId
impl Send for TypeId
impl Sync for TypeId
impl Unpin for TypeId
impl UnwindSafe for TypeId
impl<T> Any for TypeId
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for TypeId
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for TypeId
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for TypeId
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for TypeId
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into for TypeId
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for TypeId
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for TypeId
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>