pub trait TypeFn<T: ?Sized> {
type Output: ?Sized;
const TYPE_FN_ASSERTS: () = ();
}
Expand description
A function that operates purely on the level of types.
These can be used in typewit
to
map the type arguments of TypeEq
.
Type-level functions can also be declared with the
type_fn
macro.
§Properties
These are properties about TypeFn
implementors that users can rely on.
For any given F: TypeFn<A> + TypeFn<B>
these hold:
- If
A == B
, thenCallFn<F, A> == CallFn<F, B>
. - If
CallFn<F, A> != CallFn<F, B>
, thenA != B
.
§Examples
§Manual Implementation
use typewit::{TypeFn, CallFn};
let string: CallFn<AddOutput<String>, &str> = "foo".to_string() + ", bar";
let _: String = string;
assert_eq!(string, "foo, bar");
struct AddOutput<Lhs>(core::marker::PhantomData<Lhs>);
// This part is optional,
// only necessary to pass the function as a value, not just as a type.
impl<Lhs> AddOutput<Lhs> {
const NEW: Self = Self(core::marker::PhantomData);
}
impl<Lhs, Rhs> TypeFn<Rhs> for AddOutput<Lhs>
where
Lhs: core::ops::Add<Rhs>
{
type Output = Lhs::Output;
}
§Macro-based Implementation
This example uses the type_fn
macro
to declare the type-level function,
and is otherwise equivalent to the manual one.
use typewit::CallFn;
let string: CallFn<AddOutput<String>, &str> = "foo".to_string() + ", bar";
let _: String = string;
assert_eq!(string, "foo, bar");
typewit::type_fn! {
struct AddOutput<Lhs>;
impl<Rhs> Rhs => Lhs::Output
where Lhs: core::ops::Add<Rhs>
}
Provided Associated Constants§
Sourceconst TYPE_FN_ASSERTS: () = ()
const TYPE_FN_ASSERTS: () = ()
Helper constant for adding asserts in the TypeFn
impl;
Required Associated Types§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.