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 -
fn matches<T1, T2>() -> bool
Returns
trueif 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
trueif 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_void<T>() -> bool
Returns
trueif T is `()`Example
use std::typing::*; assert!(is_void::<()>()); assert!(!is_void::<u8>());Run this example -
fn is_zero_sized<T>() -> bool
Returns
trueif 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
trueif 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
trueif 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
trueif 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_closure<T>() -> bool
-
fn is_enum<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
Type identifier.
Currently, this is just a
usize, but it is not guaranteed to be so in the future. It is guaranteed to be comparable and hashable. -
type tuple_map_of<Tup, T> = when /* ... */
Tup: TupleApply a generic type to the elements of a tuple.
The generic type
Tmust be provided as a specific instantiation (e.g. with T<()>), and the resulting type is a tuple with the same structure as the input tuple, but with the elements replaced byT<_>.Example
use std::typing::{tuple_map_of, is_same}; assert!(is_same::< tuple_map_of<(u8, u16, u32), Option<()>>, (Option<u8>, Option<u16>, Option<u32>) >());Run this example -
type type_of<T> = generic_args_of<T>.0
Extract the type of the type descriptor (Type)
Example
use std::typing::{Type, type_of}; let _: type_of<Type<u8>> = 42u8;Run this example