Trait typewit::type_fn::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>
}

Required Associated Types§

source

type Output: ?Sized

The return value of the function

Provided Associated Constants§

source

const TYPE_FN_ASSERTS: () = ()

Helper constant for adding asserts in the TypeFn impl;

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

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

§

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>,

§

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

source§

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

§

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

source§

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

§

type Output = T

source§

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

§

type Output = Box<T>