Type descriptor.
This struct provides compile-time reflection information about a type (including function types).
Methods
impl Type<T> { ... }
-
fn new() -> Type<T>
Creates a new type descriptor.
-
Returns the base name of a named item.
If the type is not a named type, this will return
Option::none()
. -
Returns the module path where the item is defined.
If the type is not a named type or if it is generated internally by the compiler, this will return
Option::none()
. -
Returns the pretty name of the type.
-
Returns the unique identifier of the type.
-
fn uninitialized(self: Type<T>) -> T
Creates a new uninitialized value of the type.
-
fn zeroed(self: Type<T>) -> T
Creates a new zero-initialized value of the type.
-
Returns the size of the type in bytes.
-
Returns the alignment of the type in bytes.
-
Returns
true
if the descriptor is for the same type as the other descriptor. -
Returns true if the type is void.
-
Returns true if the type is zero-sized.
-
Returns true if the type is primitive.
-
Returns true if the type is numeric.
-
Returns true if the type is integer.
-
Returns true if the type is floating point.
-
Returns true if the type is signed.
-
Returns true if the type is unsigned.
-
Returns true if the type is
dyn
pointer. -
Returns true if the type is a slice
-
Returns true if the type is pointer.
-
Returns true if the type is array.
-
Returns true if the type is tuple.
-
Returns true if the type is struct.
-
Returns true if the type is union.
-
Returns true if the type is an enum.
-
Returns true if the type is range.
-
Returns true if the type is named function.
-
Returns true if the type is a closure.
-
Returns true if the type is function pointer.
-
Returns true if the type is protocol.
-
Returns the type descriptor of the const pointer to the type.
-
Returns the type descriptor of the mutable pointer to the type.
impl Type<T> { ... }
T: Pointer
-
Returns the type descriptor of the pointed-to type.
-
Returns true if the pointer is mutable.
impl Type<T> { ... }
T: Array
-
fn element_type(self: Type<T>) -> Type<element_of<T>>
Returns the type descriptor of the element type of the array.
-
Returns the length of the array or tuple.
impl Type<T> { ... }
T: Tuple
-
fn element_types(self: Type<T>) -> tuple_map_of<T, Type<()>>
Returns a tuple of type descriptors for the elements of the tuple.
-
fn fields(self: Type<T>) -> typeof(/* ... */)
Returns a tuple of field descriptors for the fields of the struct or union
See Field for more information.
Example
use std::typing::Type; struct Foo { bar: i32, baz: f64, } let typ: Type<Foo> = Type::new(); let fields = typ.fields(); assert_eq!(fields.0.name(), Option::some("bar")); assert_eq!(fields.0.type().debug_name(), "i32"); assert_eq!(fields.1.name(), Option::some("baz")); assert_eq!(fields.1.type().debug_name(), "f64");
Run this example
impl Type<T> { ... }
T: Enum
-
fn variants(self: Type<T>) -> &[EnumVariant<T>]
Returns the names and associated values of enum variants.
Example
use std::typing::Type; enum Foo { Bar, Quux, } let desc: Type<Foo> = Type::new(); let variants = desc.variants(); assert_eq!(variants[0].name(), "Bar"); assert_eq!(variants[0].value(), Foo::Bar); assert_eq!(variants[1].name(), "Quux"); assert_eq!(variants[1].value(), Foo::Quux);
Run this example -
fn underlying_type(self: Type<T>) -> Type<underlying_type_of<T>>
Returns the underlying type of the enum.
Example
use std::typing::Type; enum Foo { Bar = 1u8, Quux, } let desc: Type<Foo> = Type::new(); let underlying = desc.underlying_type(); assert_eq!(underlying.debug_name(), "u8");
Run this example
-
fn value_type(self: Type<T>) -> Type<underlying_type_of<T>>
Returns the value type of the constant or static.
Example
use std::typing::Type; const FOO: i32 = 42; let desc: Type<FOO> = Type::new(); let value_type = desc.value_type(); assert_eq!(value_type.debug_name(), "i32");
Run this example -
fn value(self: &Type<T>) -> underlying_type_of<T>
Returns the value of the constant or static.
When used on a static, the return value will not be a constant expression (use as_ptr for that).
Example
use std::typing::Type; const FOO: i32 = 42; let desc: Type<FOO> = Type::new(); let value = desc.value(); assert_eq!(value, 42);
Run this example -
fn as_ptr(self: Type<T>) -> &underlying_type_of<T>
Returns the pointer to the constant or static.
Example
use std::typing::Type; static FOO: i32 = 42; let desc: Type<FOO> = Type::new(); let addr = desc.as_ptr(); assert_eq!(*addr, 42);
Run this example
impl Type<T> { ... }
T: Static
-
fn as_mut_ptr(self: Type<T>) -> &mut underlying_type_of<T>
Returns a mutable pointer to the static.
Example
use std::typing::Type; static FOO: i32 = 42; let desc: Type<FOO> = Type::new(); let addr = desc.as_mut_ptr(); *addr = 1337; assert_eq!(FOO, 1337);
Run this example
impl Type<T> { ... }
-
fn arguments(self: Type<T>) -> tuple_map_of<arguments_of<T>, Type<()>>
Returns a tuple of type descriptors for the arguments of the function.
-
fn return_type(self: Type<T>) -> Type<return_type_of<T>>
Returns the type descriptor of the return value of the function.
impl Type<T> { ... }
T: Closure
-
fn underlying_function(self: Type<T>) -> Type<return_type_of<T>>
Returns the type descriptor of the underlying function of the closure.
See also underlying_function_of.
-
fn captures(self: Type<T>) -> typeof(/* ... */)
Returns the captured field descriptors of the closure.
Example
use std::typing::Type; let a = 1; let b = true; #[allow(unused_closure_binding)] let closure = |=a, &b, _param: i32| {}; let desc: Type<typeof(closure)> = Type::new(); let captures = desc.captures(); assert_eq!(captures.len(), 2); assert_eq!(captures.0.name(), Option::some("a")); assert_eq!(captures.0.type().debug_name(), "i32"); assert_eq!(captures.1.name(), Option::some("b")); assert_eq!(captures.1.type().debug_name(), "&mut bool");
Run this example
impl Type<T> { ... }
-
fn invoke(self: Type<T>, args: arguments_of<T>) -> return_type_of<T>
Invokes the function with the provided arguments as a tuple.
Example
use std::typing::Type; fn add(a: i32, b: i32) -> i32 { a + b } let desc: Type<add> = Type::new(); let args = (1, 2); let result = desc.invoke(args); assert_eq!(result, 3);
Run this example -
fn as_fn(self: Type<T>) -> function_pointer_of<arguments_of<T>, return_type_of<T>>
Converts the function to a function pointer.
Example
use std::typing::Type; fn add(a: i32, b: i32) -> i32 { a + b } let desc: Type<add> = Type::new(); let fn_ptr: fn(i32, i32) -> i32 = desc.as_fn(); assert_eq!(fn_ptr(1, 2), 3);
Run this example