Crate typenum_mappings

Crate typenum_mappings 

Source
Expand description

A proc-macro to generate mappings from typenum’s UInt types to your own type.

This is useful when emulating const_generic_exprs on stable, as the third step in the process of:

  1. Converting const generics to typenum types
  2. Performing the expression via typenum types
  3. Converting those typenum types back to the resulting type

§Example - concat_array

use std::ops::Add;

use typenum::{U, ToUInt, Const};

trait ArrayLike {
    fn new() -> Self;
}

impl<T: Default, const N: usize> ArrayLike for [T; N] {
    fn new() -> Self {
        std::array::from_fn(|_| T::default())
    }
}

trait TypenumToArray<T> {
    type Array: ArrayLike;
}

typenum_mappings::impl_typenum_mapping!(
    impl<const N: usize = 0..=1000, T: Default> TypenumToArray<T> for #TypeNumName {
        type Array: = [T; N];
    }
);


type RunAdd<T1, T2> = <T1 as Add<T2>>::Output;
type RunTypeToArray<T, N> = <N as TypenumToArray<T>>::Array;

fn concat_array<const N1: usize, const N2: usize, T>(a1: [T; N1], a2: [T; N2]) -> RunTypeToArray<T, RunAdd<U<N1>, U<N2>>>
where
    Const<N1>: ToUInt,
    Const<N2>: ToUInt,

    U<N1>: Add<U<N2>>,
    <U<N1> as Add<U<N2>>>::Output: TypenumToArray<T>
{
    todo!()
}

let out = concat_array([1, 2, 3], [4, 5, 6]);
assert_eq!(out.len(), 6);

§Minimum Supported Rust Version

This is currently 1.63, and it is considered a breaking change to increase.

Macros§

impl_typenum_mapping