Module typewit::type_fn

source ·
Expand description

Type-level functions.

Type-level functions come in two flavors: injective, and non-injective

§Injective

An injective function is any function f for which a != b implies f(a) != f(b).
(For both injective and non-injective functions, f(a) != f(b) implies a != b)

The InjTypeFn trait encodes injective type-level functions, requiring the type to implement both TypeFn and RevTypeFn.

§Example: injective function

typewit::inj_type_fn!{
    struct Upcast;
     
    impl u8 => u16;
    impl u16 => u32;
    impl u32 => u64;
    impl u64 => u128;
}
let _: CallInjFn<Upcast, u8> = 3u16;
let _: CallInjFn<Upcast, u16> = 5u32;

Because Upcast is injective, it is possible to query the argument from the returned value:

let _: UncallFn<Upcast, u16> = 3u8;
let _: UncallFn<Upcast, u128> = 5u64;

§Non-injective

The TypeFn trait allows implementors to be non-injective.

§Example: non-injective function

typewit::type_fn!{
    struct Bar;
     
    impl<T> Vec<T> => T;
    impl<T> Box<T> => T;
}

Bar is non-injective because it maps both Vec<T> and Box<T> to T.

Re-exports§

Structs§

  • Type-level identity function
  • Reverses an InjTypeFn, its arguments become return values, and its return values become arguments.
  • Type-level function from T to &'a T
  • Type-level function from T to &'a mut T
  • Type-level function which implements TypeFn by delegating to F

Traits§

Type Aliases§