Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer (opt-in) const-generic mappings. Mainly, a U<N> type alias. #176

Merged
merged 5 commits into from
Dec 5, 2022

Conversation

danielhenrymantilla
Copy link
Contributor

@danielhenrymantilla danielhenrymantilla commented Nov 21, 2021

Through a cfg-gated const-generics Cargo feature (disabled by default, obviously), a const_generic_mappings module is added, featuring U<N>, a nifty type alias so that U<42> = U42;, and so on.

It can even be used in a generic context, thanks to the following clause:

...<const N: usize> ...
where
    Const<N> : ToUInt,
...

which allows U<N> to Just Work™, where Const<N> and ToUInt are the two other items featured within that module. All three are re-exported at the root of the crate for convenience.

  • image

  • image

Note that until generic_const_exprs are powerful enough to allow bit operations on a generic const to be used as another generic const parameter, the mapping from a Const<N> to a UInt 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-coded impl ToUInt for Const<...> { type Output = U...; } impls. This means that not all const N: usize are valid choices for U<> (hence the needed extra where clause to restrict it to the list of valid hard-coded impls).

Commits

  1. 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.

  2. The second commit is an unrelated minor quality-of-life improvement for devs, whereby the build.rs script is not re-executed should code under src/ change. This commit can be removed if it is not deemed relevant.

  3. 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:

    image

    For those curious about the exact values of U..., that huge list is still available within the consts module.

  4. 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's Cargo.toml targetting the third commit (danielhenrymantilla@077aa83) of this PR:

[patch.crates-io.typenum]
git = "https://github.com/danielhenrymantilla/typenum"
version = "1.0.14"
rev = "077aa8386e0f22c0ce90f66bebe9e3f3e0fd3123"

@paholg
Copy link
Owner

paholg commented Nov 21, 2021

At a glance, this looks awesome, thanks!

I'm super busy this week so likely won't have time to take a look right now, but feel free to ping me a reminder in a week or so if I haven't gotten to it.

pub use consts::{
False, True, B0, B1,
U0, U1, U2, *,
N1, N2, Z0, P1, P2, *,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: re-export the negative numbers in the right order:

Suggested change
N1, N2, Z0, P1, P2, *,
N2, N1, Z0, P1, P2, *,

@InBetweenNames
Copy link

What would it take to get this merged?

@paholg
Copy link
Owner

paholg commented Dec 5, 2022

Sorry for the delay.

@paholg paholg merged commit 4ce4473 into paholg:main Dec 5, 2022
@danielhenrymantilla
Copy link
Contributor Author

No worries, I myself forgot to "ping / remind you" 😄 Thanks for the work with this crate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants