chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Make Duration::seconds (and siblings) const fn

Open axos88 opened this issue 5 years ago • 11 comments

This would allow to specify things like:

const THRESHOLD: Duration = Duration::seconds(3);

axos88 avatar Mar 17 '19 21:03 axos88

I don't think this is possible in current Rust - const fn isn't allowed to use branches as of Rust 1.33.

bspeice avatar Mar 18 '19 21:03 bspeice

Yes, that's what I'm currently holding off on. Even once const fn is stable enough for chrono we'll need to think about how best to support them in a way that doesn't break backcompat too terribly. Chrono currently supports rust 1.13, but const fn is the strongest reason to upgrade to a new version that is upcoming.

Obviously we'll update, but I'd like to keep things as compatible as possible, if that means adding some features or something else so that we can still work on old rustcs, I'm not sure.

quodlibetor avatar Mar 19 '19 00:03 quodlibetor

const fn on NaiveDate and friends would also be useful.

hjmallon avatar Sep 03 '19 01:09 hjmallon

If anyone wants to create a PR for this behind something like an unstable-nightly-const feature gate I would be happy to take it. It should just be a matter of duplicating some functions and adding some cfg and cfg(not) attributes.

quodlibetor avatar Sep 03 '19 02:09 quodlibetor

For the googlers that came here like me, it looks like the current blocker on this issue are const-panics (RFC) (issue) that are triggered when doing the .expect/panic-based bounds checking on the various Duration constructors.

Perhaps we can introduce variants that follow stdlib functions like checked_{mul,div,...}, and return an Option<Duration>, with None meaning out-of-bounds, just like checked_mul.

Since Duration is re-exported from the time crate, shouldn't this really be an issue there?

chrismooredev avatar Mar 09 '20 20:03 chrismooredev

Still not good to go on with that change? I'd like to vote for it, because Duration constants make much sense, imho. I actually had to quit using Duration for that and resort to ints (with a particular unit attached to their names), having to create a Duration variable every time... Do you, authors, tought of a better way to address this scenario than to use constants? Found it nowhere in the docs...

zertyz avatar Feb 18 '21 23:02 zertyz

With https://github.com/dtolnay/rustversion, there's a specific feature for making functions const conditionally on compiler versions since X. I'd argue this is a big win for a very cheap (no syn) extra dep.

Edit: also found https://github.com/taiki-e/const_fn which is more specific macro and used by time 0.2 crate.

robjtede avatar Mar 17 '21 15:03 robjtede

We now have const panics in rust 1.57 would it be possible to move this issue forward?

msdrigg avatar Dec 22 '21 17:12 msdrigg

I'm waiting for this. It seems this can be reviewed relatively easily. Can we merge the @msdrigg 's PR? Or are there other concerns?

hoon-prestolabs avatar Mar 25 '22 10:03 hoon-prestolabs

@quodlibetor does your offer still stand ?

bestouff avatar Mar 25 '22 13:03 bestouff

I'm open to merging something like this, provided it is made conditional on an opt-in feature flag (so that people who don't need this can still depend on the currently 1.32 MSRV).

djc avatar Mar 28 '22 08:03 djc

Fixed in https://github.com/chronotope/chrono/pull/1337.

pitdicker avatar Feb 02 '24 09:02 pitdicker

Thanks !

bestouff avatar Feb 02 '24 09:02 bestouff