#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

#22 in #typenum

Download history 602/week @ 2024-11-16 515/week @ 2024-11-23 269/week @ 2024-11-30 391/week @ 2024-12-07 157/week @ 2024-12-14 59/week @ 2024-12-21 147/week @ 2024-12-28 178/week @ 2025-01-04 307/week @ 2025-01-11 271/week @ 2025-01-18 231/week @ 2025-01-25 295/week @ 2025-02-01 484/week @ 2025-02-08 672/week @ 2025-02-15 408/week @ 2025-02-22 388/week @ 2025-03-01

1,988 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

~320–790KB
~18K SLoC