Skip to content

Logarithmus/typenum_alias

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

typenum_alias

Strong aliases for typenum, powered by const generics. Makes compilation errors more readable.

Motivation

typenum defines convenient type aliases for frequently used numbers. Unfortunately, rustc & rust-analyzer expand them into their full binary representation, e. g. typenum::U10 is expanded to this:

pub type U10 = UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>;

For bigger numbers it's even longer. There's rust-lang/rust-analyzer#1666, which will hopefully solve the problem someday.

typenum_alias aims to provide a temporary solution which works on stable Rust using only min_const_generics.

How it works

typenum_alias defines struct Const<const N: i32>, trait ToTypenum & trait ToConst. Arithmetical operations are implemented for Const<N> in the following way:

  1. Const<N> is converted to PInt, NInt or Z0
  2. typenum performs the calculations
  3. The result is converted back to Const<N>

Thanks to this technique, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0> becomes just Const<10_i32>. You can shorten it even more to Const<10> either by using latest nightly, which already contains the fix (rust-lang/rust#99393), or by waiting for 1.64.0 stable release of Rust.