-
Notifications
You must be signed in to change notification settings - Fork 542
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
Use core::time::Duration via an enum #714
Conversation
It's an interesting idea. Can you explain explicitly: (a) what problems does this solve? (Effectively, this is like a mini-RFC.) |
Thanks @djc - good idea re. mini-RFC. Worth noting here that I'll (hopefully shortly) set up another branch with a different approach using (a) what problems does this solve?
(b) which downsides do you see?
(c) are there alternative solutions to (a) that we should consider?
|
This is just an opinion, but I think |
Thanks for the feedback @Liamolucko. There is also some discussion to be had whether we use Having the add/sub methods take |
@Liamolucko - what are your thoughts on One difference between the current |
Those work, although they're a bit long. Maybe Another difference between this and |
Excellent point |
The |
The advantage of it being an enum is that it internally stores the However even if we were to move away from either of those two options, I'd argue that we should ensure that we have an |
Hmm, I was thinking we should use |
Makes sense, so we just duplicate the impls, once for |
Where it makes sense, yes. |
ok - and do you have a preference between the below and using an enum? struct TimeDelta {
duration: core::time::Duration,
forwards: bool,
} |
I guess it doesn't really matter -- which one do you think will make the code most readable? |
I think the |
cdb88ad
to
67e4a40
Compare
Will rebase this again once |
…e::time::Duration` readme updates fix broken function name remove wayward install-cross fix broken function name - v2
67e4a40
to
05c808f
Compare
Hmm these test failures probably warrant more discussion. The fundamental issue stems from the |
Where do we need to return |
So my plan here was to make the migration process as painless as possible. To do this I've tried to keep as many as possible of the function signatures the same, while changing the name and underlying implementation of the type (I could potentially even add back the fallible conversions for improved compatibility). The existing However, IMO there is a better way to solve this - which is that, realistically even i64 is a larger range of seconds than would ever be useful (except perhaps for physics use-cases, which might be better served by
This potentially has an effect on doing zero-copy de/serialization however as there are byte patterns that aren't valid for the type itself (eg underlying storage might be i64/u64, but some of the valid i64/u64 values wouldn't be valid for |
Hmm, not sure I agree with this plan. Given the fact that we have such a broad user base, we won't get to break API compatibility very often. I think 0.5 should be more correct/robust (fewer, and well-documented, panics) and expose a minimal API, even if that means migration would be more work. Given that 0.5 will get rid of the time 0.1 compatibility, I think willingness for maintained downstream packages to migrate will be reasonably high even in the face of substantial changes. |
Excellent, this sounds like a good approach! Another thought - perhaps pub enum TimeDelta<D = core::time::Duration> {
Forwards(D),
Backwards(D),
}
impl<D: Copy> TimeDelta<D> {
fn abs(self) -> D {
todo!()
}
fn forwards(self) -> Option<D> {
todo!()
}
fn backwards(self) -> Option<D> {
todo!()
}
} and use cases like: // handle the direction
match my_datetime - my_other_datetime {
TimeDelta::Forwards(d) => (),
TimeDelta::Backwards(d) => (),
}
// just get the magnitude
(my_datetime - my_other_datetime).abs()
// handle the direction
// could be just `Delta` here?
match my_date - my_other_date {
TimeDelta::Forwards(d) => (),
TimeDelta::Backwards(d) => (),
}
// just get the magnitude
(my_date - my_other_date).abs()
// handle the direction
// could be just `Delta` here?
match my_date.signed_months_since(my_other_date) {
TimeDelta::Forwards(d) => (),
TimeDelta::Backwards(d) => (),
}
// just get the magnitude
my_date.signed_months_since(my_other_date).abs() |
Let's first do a version without generics and see where we feel a need for generics. My spidey sense is that there's too much complexity to justify the benefit but I don't have a good feel for the potential benefit right now. |
closing in favour of #824 |
Setting this up so it can run through the CI for now, this shouldn't be merged until at least version 0.5, or possibly never.
This is one option to use
core::time::Duration
in chrono's duration. The main change here is to remove theoldtime
modules, and rename theDuration
type toSignedDuration
which can now be infallibly converted both ways withcore::time::Duration
(albeit via an absolute value when going tocore::time::Duration
)