Type Alias typewit::type_fn::CallInjFn

source ·
pub type CallInjFn<F, A> = <F as InjTypeFn<A>>::Ret;
Expand description

CallFn with an additional F:InjTypeFn<A> requirement, which helps with type inference.

§Example

use typewit::{InjTypeFn, CallInjFn};
 
// inferred return type
let inferred_ret = upcast(3u8);
assert_eq!(inferred_ret, 3);
 
// inferred argument type
let inferred_arg: u32 = upcast(5);
assert_eq!(inferred_arg, 5);
 
// Because the return type is `CallInjFn<_, I>`,
// this can infer `I` from the return type,
fn upcast<I>(int: I) -> CallInjFn<Upcast, I>
where
    Upcast: InjTypeFn<I>,
    CallInjFn<Upcast, I>: From<I>,
{
    int.into()
}
 
 
typewit::inj_type_fn!{
    struct Upcast;
     
    impl u8 => u16;
    impl u16 => u32;
    impl u32 => u64;
    impl u64 => u128;
}

As of October 2023, replacing CallInjFn with CallFn can cause type inference errors:

error[E0277]: the trait bound `Upcast: TypeFn<{integer}>` is not satisfied
  --> src/type_fn/injective.rs:132:32
   |
11 | let inferred_arg: u32 = upcast(5);
   |                         ------ ^ the trait `TypeFn<{integer}>` is not implemented for `Upcast`
   |                         |
   |                         required by a bound introduced by this call
   |
   = help: the following other types implement trait `TypeFn<T>`:
             <Upcast as TypeFn<u16>>
             <Upcast as TypeFn<u32>>
             <Upcast as TypeFn<u64>>
             <Upcast as TypeFn<u8>>