Type reflection and dyn
objects
Most functions in this module are const-evaluable, so they can be used in when
expression for conditional compilation based on e.g. the generic parameter.
Structs
Self
type in the protocol of a dyn
object.
Functions
-
fn type_id<T>() -> TypeId
Returns a unique identifer for the type passed as the generic parameter.
The resulting value is not guaranteed to be the same across compilations, but it is guaranteed to be unique for each distinct type.
Example
use std::typing::type_id; assert_ne!(type_id::<u8>(), type_id::<u16>());
Run this example -
fn type_name<T>() -> &[u8]
Returns a human-readable name for the type passed as the generic parameter.
Example
use std::typing::type_name; assert_eq!(type_name::<u8>(), "u8");
Run this example -
Returns the names and associated values of enum variants.
Example
use std::typing::enum_variants; enum Foo { Bar, Quux, } let variants = enum_variants::<Foo>(); assert_eq!(variants.len(), 2); assert_eq!(variants[0].0, "Bar"); assert_eq!(variants[0].1, Foo::Bar); assert_eq!(variants[1].0, "Quux"); assert_eq!(variants[1].1, Foo::Quux);
Run this example -
fn matches<T1, T2>() -> bool
Returns
true
if type T1 matches bound T2.T2 can be a protocol or a type (in which case the result indicates whether T1 and T2 refer to the same type).
See also is_same.
Example
use std::typing::matches; use std::builtins::Primitive; assert!(matches::<u8, u8>()); assert!(!matches::<u8, u16>()); assert!(matches::<u8, Primitive>()); assert!(!matches::<&[u8], Primitive>());
Run this example -
fn is_same<T1, T2>() -> bool
Returns
true
if type T1 is exactly the same type as T2.See also matches.
Example
use std::typing::is_same; use std::builtins::Primitive; assert!(is_same::<u8, u8>()); assert!(!is_same::<u8, u16>()); // Protocols can also be compared for equality assert!(is_same::<Fn(u8) -> u8, std::builtins::Callable<(u8), u8>>()); assert!(!is_same::<u8, Primitive>());
Run this example -
fn is_zero_sized<T>() -> bool
Returns
true
if T is zero sized.See also ZeroSized.
Example
use std::typing::*; assert!(is_zero_sized::<()>()); assert!(is_zero_sized::<([u8; 0], ())>()); assert!(!is_zero_sized::<u8>());
Run this example -
fn is_primitive<T>() -> bool
Returns
true
if T is primitive.See also Primitive.
Example
use std::typing::*; assert!(is_primitive::<u8>()); assert!(!is_primitive::<(u8, bool)>());
Run this example -
fn is_numeric<T>() -> bool
Returns
true
if T is numeric.See also Numeric.
Example
use std::typing::*; assert!(is_numeric::<u8>()); assert!(is_numeric::<i32>()); assert!(is_numeric::<f64>()); assert!(!is_numeric::<bool>()); assert!(!is_numeric::<&u8>());
Run this example -
fn is_integer<T>() -> bool
Returns
true
if T is integer.See also Integer.
Example
use std::typing::*; assert!(is_integer::<u8>()); assert!(is_integer::<i32>()); assert!(!is_integer::<f64>()); assert!(!is_numeric::<bool>());
Run this example -
fn is_floating_point<T>() -> bool
-
fn is_signed<T>() -> bool
-
fn is_unsigned<T>() -> bool
-
fn is_pointer<T>() -> bool
-
fn is_array<T>() -> bool
-
fn is_tuple<T>() -> bool
-
fn is_struct<T>() -> bool
-
fn is_union<T>() -> bool
-
fn is_range<T>() -> bool
-
fn is_named_function<T>() -> bool
-
fn is_function_pointer<T>() -> bool
-
fn is_protocol<T>() -> bool
-
fn is_same_layout_as<T1, T2>() -> bool
-
fn is_same_base_as<T1, T2>() -> bool
-
fn is_array_of<T, U>() -> bool
-
fn is_pointer_of<T, U>() -> bool
-
fn is_range_of<T, U>() -> bool
Types
-
type TypeId = usize