chrono
chrono copied to clipboard
Make Duration::seconds (and siblings) const fn
This would allow to specify things like:
const THRESHOLD: Duration = Duration::seconds(3);
I don't think this is possible in current Rust - const fn
isn't allowed to use branches as of Rust 1.33.
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.
const fn
on NaiveDate and friends would also be useful.
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.
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?
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...
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.
We now have const panics in rust 1.57 would it be possible to move this issue forward?
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?
@quodlibetor does your offer still stand ?
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).
Fixed in https://github.com/chronotope/chrono/pull/1337.
Thanks !