Re: [PATCH 2/5] rust: time: Introduce Duration type

From: Boqun Feng
Date: Tue Mar 26 2024 - 13:22:12 EST


On Tue, Mar 26, 2024 at 10:11:07AM -0700, Boqun Feng wrote:
[...]
> > > +impl Duration {
> > > + /// Creates a new duration of `ns` nanoseconds.
> > > + pub const fn new(ns: i64) -> Self {
> > > + Self { inner: ns }
> > > + }
> > > +
> > > + /// Divides the number of nanoseconds by a compile-time constant.
> > > + #[inline]
> > > + fn divns_constant<const DIV: i64>(self) -> i64 {
> > > + self.to_ns() / DIV
> > > + }
> >
> > I am a bit confused, why is this better than writing
> > `self.to_ns() / DIV` at the callsite?
> >
>
> Hmm.. you're right, there should be no difference I think. If there is
> nothing I'm missing from Alice, I will drop this function in the next
> version.
>

On a second thought, I think this prevents accidentally divide a
non-const value, in other words, if you use this function, you're
guaranteed the divisor is a constant, and you have the compiler checking
that for you. So in that sense, I think it makes sense to remain it
here. Thoughts?

Regards,
Boqun

> Regards,
> Boqun
>
> > --
> > Cheers,
> > Benno
> >
> > > +
> > > + /// Returns the number of milliseconds.
> > > + #[inline]
> > > + pub fn to_ms(self) -> i64 {
> > > + self.divns_constant::<NSEC_PER_MSEC>()
> > > + }
> > > +
> > > + /// Returns the number of nanoseconds.
> > > + #[inline]
> > > + pub fn to_ns(self) -> i64 {
> > > + self.inner
> > > + }
> > > +}
> > > --
> > > 2.44.0
> > >