pub trait TypeFn<T: ?Sized> {
type Output: ?Sized;
}
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.
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>
}