Builtin types and their method implementations.
This module holds impl blocks for builtin types all items with #[lang(builtin_X)]
are
treated as X when lowering into IR, so they cannot actually exist as proper structs.
Builtin protocols also cannot be directly implemented by user code, even though they contain no associated functions. They are handled specially by the compiler.
Builtin types
()
)
!
)
Protocols
i8
, i16
, i32
, ...)
u8
, u16
, u32
, ...)
a..b
, a..=b
, ..b
, ..=b
, ...)
T
T
(regardless of mutability)
T
T
(same size and alignment)
T
Types
-
type unsigned_of<T> = when /* ... */
T: IntegerUnsigned integer type of the same size as the original type.
Example
use std::typing::matches; use std::builtins::unsigned_of; assert!(matches::<unsigned_of<i32>, u32>()); assert!(matches::<unsigned_of<isize>, usize>()); assert!(matches::<unsigned_of<u8>, u8>());
Run this example -
type signed_of<T> = when /* ... */
T: IntegerSigned integer type of the same size as the original type.
Example
use std::typing::matches; use std::builtins::signed_of; assert!(matches::<signed_of<u32>, i32>()); assert!(matches::<signed_of<usize>, isize>()); assert!(matches::<signed_of<i8>, i8>());
Run this example -
type element_of<T> = typeof(/* ... */)
T: ArrayElement type of the provided array
Example
use std::typing::matches; use std::builtins::element_of; assert!(matches::<element_of<[i32; 3]>, i32>());
Run this example -
Return type of the provided function
Example
use std::typing::matches; use std::builtins::return_type_of; fn add(x: i32, y: i32) -> i32 { x + y } assert!(matches::<return_type_of<add>, i32>()); assert!(matches::<return_type_of<fn() -> u8>, u8>());
Run this example -
Tuple of argument types of the provided function
Example
use std::typing::matches; use std::builtins::arguments_of; fn add(x: i32, y: i32) -> i32 { x + y } assert!(matches::<arguments_of<add>, (i32, i32)>()); assert!(matches::<arguments_of<fn() -> u8>, ()>());
Run this example -
Tuple of the generic arguments of a given type
Example
use std::typing::matches; use std::builtins::generic_args_of; fn add<T>(x: T, y: T) -> T { x + y } assert!(matches::<generic_args_of<add<i32>>, (i32,)>()); assert!(matches::<generic_args_of<Result<&[u8], i32>>, (&[u8], i32)>()); assert!(matches::<generic_args_of<std::cmp::Ordering>, ()>());
Run this example -
type replace_generic_args_of<T, Args>
Args: TupleType with replaced generic arguments provided by a tuple
Example
use std::typing::matches; use std::builtins::replace_generic_args_of; let o = Option::some(42); assert!(matches::<replace_generic_args_of<typeof(o), (bool,)>, Option<bool>>());
Run this example -
type pointer_with_mut_of<T, Ptr>
Ptr: PointerPointer to
T
with same constness asPtr
.Example
use std::typing::matches; use std::builtins::pointer_with_mut_of; assert!(matches::<pointer_with_mut_of<void, &i32>, &void>()); assert!(matches::<pointer_with_mut_of<usize, &mut Option<usize>>, &mut usize>());
Run this example -
type array_with_length_of<T, Arr>
Arr: ArrayArray of
T
with same length asArr
.Example
use std::typing::matches; use std::builtins::array_with_length_of; assert!(matches::<array_with_length_of<i32, [(); 42]>, [i32; 42]>()); assert!(matches::<array_with_length_of<Option<bool>, [i64; 1337]>, [Option<bool>; 1337]>());
Run this example -
Backing type of an
enum
or aconst
/static
type.Example
use std::typing::matches; use std::builtins::underlying_type_of; enum Foo { A, B, C } enum Bar { A = 0usize, B, C } const FOO: i32 = 42; static BAR: usize = 42; assert!(matches::<underlying_type_of<Foo>, i32>()); assert!(matches::<underlying_type_of<Bar>, usize>()); assert!(matches::<underlying_type_of<FOO>, i32>()); assert!(matches::<underlying_type_of<BAR>, usize>());
Run this example -
type underlying_function_of<T>
T: ClosureUnderlying function of a closure
Underlying function is a regular named function that accepts the closure object as its first argument by a mutable pointer.
See also as_callback for a function that converts a pointer to a closure into a C-style void pointer and a function pointer pair.
Example
use std::builtins::underlying_function_of; let a = 1; let f = |=a, x: i32| -> i32 { x + a }; let g: underlying_function_of<typeof(f)>; assert_eq!( f(2), g(&f, 2) );
Run this example -
Make a function pointer type from a tuple of arguments and the return type.
Example
use std::typing::matches; use std::builtins::function_pointer_of; assert!(matches::<function_pointer_of<(i32, ), i32>, fn(i32) -> i32>());
Run this example