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

Add feature gated and unstable const constructor for nightly? #5

Open
phil-opp opened this issue Apr 14, 2018 · 8 comments
Open

Add feature gated and unstable const constructor for nightly? #5

phil-opp opened this issue Apr 14, 2018 · 8 comments

Comments

@phil-opp
Copy link

phil-opp commented Apr 14, 2018

Hi, we're using this library for rust-osdev/x86_64. We now have the problem that the ux types do not have a const constructor. Would you accept a pull request that adds a “nightly" feature and conditionally implements unstable const constructors?

@kjetilkjeka
Copy link
Collaborator

Hi, awesome that you're using the library. Yes, this is something i would merge.

The construction of ux types are at the moment far from ideal. The ideal case would be custom literals that evaluates in compile time and give a warning if numbers are out of range (just like rust built in integeres). The new function is a way to circumvent that this is not at the moment possible. I think that lack of a good way to do construction is the biggest 1.0 blocker at the moment.

What is the status of const fn panicing these day? The new function currently panics if given an integer out of range.

Are there any other constructors than a const fn new() that is needed?

@Ixrec
Copy link

Ixrec commented Apr 28, 2018

Seems like panicking in const fns is stalled, I can't find a discussion more recent than the RFC rust-lang/rfcs#2345, which hasn't even been postponed.

@kjetilkjeka kjetilkjeka changed the title Nightly feature? Add feature gated and unstable const constructor for nightly? May 18, 2018
@kitlith
Copy link
Contributor

kitlith commented Feb 28, 2019

It appears at this point the RFC is merged and implemented under the feature flag const_panic.

@Nemo157
Copy link

Nemo157 commented Feb 28, 2019

Panicking in constants works, but there are no conditionals yet so you can only have a const fn that always panics. Supporting conditionals in const fn are on the roadmap, and likely to be one of the next supported const features.

In the meantime, it should be possible to provide const unsafe fn new_unchecked(value: u8) -> u4 etc. (same as the recently const-stabilised NonZeroU32::new_unchecked).

@BenLeadbetter
Copy link

We could potentially add a procedural macro constructor which can check the value of a literal at compile time.

let _ = ux::lit!(0x7F_u7);
let _ = ux::lit!(0xFF_u7); // compile error

Of course, this would a bunch of proc macro dependency to the now otherwise very lightweight crate. I guess this constructor could go behind a feature?

@BenLeadbetter
Copy link

BenLeadbetter commented May 24, 2024

In the meantime, it should be possible to provide const unsafe fn new_unchecked(value: u8) -> u4

I've been wanting this function for a while now too. 👍

I think the unsafe keyword would not be appropriate, however. It wouldn't violate the rust memory model when misused - it's just the internal state of the type which would be in an invalid state. The _unchecked suffix suffices imo.

@chrysn
Copy link
Member

chrysn commented May 24, 2024 via email

@chrysn
Copy link
Member

chrysn commented May 24, 2024 via email

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

No branches or pull requests

7 participants