typewit::type_fn

Trait TypeFn

Source
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:

  1. If A == B, then CallFn<F, A> == CallFn<F, B>.
  2. If CallFn<F, A> != CallFn<F, B>, then A != 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§

Source

const TYPE_FN_ASSERTS: () = ()

Helper constant for adding asserts in the TypeFn impl;

Required Associated Types§

Source

type Output: ?Sized

The return value of the function

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.

Implementations on Foreign Types§

Source§

impl<F, T: ?Sized> TypeFn<T> for PhantomData<F>
where F: TypeFn<T>,

Source§

type Output = <F as TypeFn<T>>::Output

Implementors§

Source§

impl<'a, T: 'a + ?Sized> TypeFn<T> for GRef<'a>

Source§

impl<'a, T: 'a + ?Sized> TypeFn<T> for GRefMut<'a>

Source§

impl<F, A: ?Sized> TypeFn<A> for FnRev<F>
where F: RevTypeFn<A>,

Source§

type Output = <F as RevTypeFn<A>>::Arg

Source§

impl<F, T: ?Sized> TypeFn<T> for Invoke<F>
where F: TypeFn<T>,

Source§

type Output = <F as TypeFn<T>>::Output

Source§

impl<T: ?Sized> TypeFn<T> for FnIdentity