Re: [PATCH net-next v2 2/6] rust: time: Introduce Delta type

From: Andrew Lunn
Date: Sat Oct 05 2024 - 14:03:33 EST


> +/// A span of time.
> +#[derive(Copy, Clone)]
> +pub struct Delta {
> + nanos: i64,

Is there are use case for negative Deltas ? Should this be u64?

A u64 would allow upto 500 years, if i did my back of an envelope
maths correct. So i suppose 250 years allowing negative delta would
also work.

> +}
> +
> +impl Delta {
> + /// Create a new `Delta` from a number of nanoseconds.
> + #[inline]
> + pub fn from_nanos(nanos: u16) -> Self {

So here you don't allow negative values.

But why limit it to u16, when the base value is a 63 bits? 65535 nS is
not very long.

> + Self {
> + nanos: nanos.into(),
> + }
> + }
> +
> + /// Create a new `Delta` from a number of microseconds.
> + #[inline]
> + pub fn from_micros(micros: u16) -> Self {

A u32 should not overflow when converted to nS in an i64.

Dumb question. What does Rust in the kernel do if there is an
overflow?

> + /// Return the number of nanoseconds in the `Delta`.
> + #[inline]
> + pub fn as_nanos(self) -> i64 {
> + self.nanos
> + }
> +
> + /// Return the number of microseconds in the `Delta`.
> + #[inline]
> + pub fn as_micros(self) -> i64 {
> + self.nanos / NSEC_PER_USEC
> + }
> +}

So here we are back to signed values. And also you cannot create a
Delta from a Delta because the types are not transitive.

Andrew