Module 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§

pub use crate::inj_type_fn;
pub use crate::type_fn;
pub use crate::type_fn;

Structs§

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

Traits§

InjTypeFn
An injective type-level function
RevTypeFn
The inverse of TypeFn, for getting the argument of a TypeFn from its return value.
TypeFn
A function that operates purely on the level of types.

Type Aliases§

CallFn
Calls the F type-level function with T as its argument.
CallInjFn
CallFn with an additional F:InjTypeFn<A> requirement, which helps with type inference.
UncallFn
Queries the argument to a F: TypeFn from its return value.