#typenum #proc-macro #mapping #own #generate #type #u-int

macro typenum_mappings

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

1 unstable release

0.1.0 Jun 15, 2024

#1749 in Development tools

Download history 233/week @ 2024-06-09 96/week @ 2024-06-16

329 downloads per month
Used in aformat

MIT license

10KB
145 lines

typenum_mappings

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

Read the documentation via cargo doc --open --no-deps or on docs.rs.

Minimum Supported Rust Version

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


lib.rs:

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.

Dependencies

~0.4–0.9MB
~20K SLoC