Offer (opt-in) const-generic
mappings. Mainly, a U<N>
type alias.
#176
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Through a
cfg
-gatedconst-generics
Cargo feature (disabled by default, obviously), aconst_generic_mappings
module is added, featuringU<N>
, a nifty type alias so thatU<42> = U42;
, and so on.It can even be used in a generic context, thanks to the following clause:
which allows
U<N>
to Just Work™, whereConst<N>
andToUInt
are the two other items featured within that module. All three are re-exported at the root of the crate for convenience.Note that until
generic_const_exprs
are powerful enough to allow bit operations on a genericconst
to be used as another generic const parameter, the mapping from aConst<N>
to aUInt
cannot be expressed in a fully generic fashion.Instead, similarly to the list of hard-coded
U...
aliases, there is an equally long list of hard-codedimpl ToUInt for Const<...> { type Output = U...; }
impls. This means that not allconst N: usize
are valid choices forU<>
(hence the needed extrawhere
clause to restrict it to the list of valid hard-coded impls).Commits
The addition of the
build.rs
script code to generate such impls, as well as the Cargo feature, importing the items, their documentation, and tweaking the workflow file to enable that feature on non-MSRV have all been implemented in the first commit.The second commit is an unrelated minor quality-of-life improvement for devs, whereby the
build.rs
script is not re-executed should code undersrc/
change. This commit can be removed if it is not deemed relevant.The third commit is another unrelated improvement: when one goes over https://docs.rs/typenum/1.14.0/typenum/, they find this huge list of type aliases taking up all the screen. This is because the
pub use
was#[doc(inline)]
by default. I've made that blob re-export become#[doc(no_inline)]
, but added some re-exports explicitly for the sake of readability:For those curious about the exact values of
U...
, that huge list is still available within theconsts
module.And then a version bump commit (I don't think this warrants a minor bump since the new logic is feature-gated: a patch commit makes sense).
How to test this feature until this PR is merged and released upstream
Use a
[patch]
section in your workspace'sCargo.toml
targetting the third commit (danielhenrymantilla@077aa83) of this PR: