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
Download history 66/week @ 2024-07-20 157/week @ 2024-07-27 251/week @ 2024-08-03 168/week @ 2024-08-10 145/week @ 2024-08-17 143/week @ 2024-08-24 120/week @ 2024-08-31 289/week @ 2024-09-07 165/week @ 2024-09-14 144/week @ 2024-09-21 73/week @ 2024-09-28 294/week @ 2024-10-05 88/week @ 2024-10-12 233/week @ 2024-10-19 118/week @ 2024-10-26 192/week @ 2024-11-02

636 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.3–0.8MB
~18K SLoC