Just an idea. Is there any reason we wouldn’t want a variable-precision Duration class, like C++'s std::chrono::duration? The equivalent of std::ratio can be done with a trait and static methods:
trait Ratio {
fn num() -> i64;
fn denom() -> i64;
}
Each ratio would be defined by a (zero-sized) type implementing Ratio:
struct Nano;
impl Ratio for Nano {
fn num() -> i64 { 1 }
fn denom() -> i64 { 1000000000 }
}
Duration could then be written like this:
struct Duration<T: Signed+Bounded, P: Ratio> {
ticks: T
}
where T is the type the user wants to use and P the period that spans each tick.
T would probably be i64 in most cases, but smaller-sized integers and floats have valid use cases, I think.
The tricky part would be implementing with_period<N: Ratio>(&self) -> Duration<T, N>. A cast function alike to C++'s duration_cast to cast between duration types, that would have to deal the type conversion and precision issues.
It’s a big change, but I think I should propose this before 1.0.
Fast edit: Now that I think of it, wouldn’t calling to P::num() require UFCS to be implemented first? We could work around it by using a zero-sized field with an instance of the type and having Ratio take a &self parameter, but I don’t like having to hack around the language, even less for the std…