[PATCH v1 1/5] rust: time: Change Delta methods to take &self instead of self

FUJITA Tomonori posted 5 patches 9 months, 1 week ago
There is a newer version of this series
[PATCH v1 1/5] rust: time: Change Delta methods to take &self instead of self
Posted by FUJITA Tomonori 9 months, 1 week ago
Change several methods of the `Delta` type in Rust to take `&self`
instead of `self`. These methods do not mutate or consume the `Delta`
value and are more idiomatically expressed as taking a shared
reference. This change improves consistency with common Rust practice
and allows calling these methods on references without requiring an
explicit copy or move of the value.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/kernel/time.rs | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 1be5ecd814d3..deca2999ced6 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -260,26 +260,26 @@ pub const fn from_secs(secs: i64) -> Self {
 
     /// Return `true` if the [`Delta`] spans no time.
     #[inline]
-    pub fn is_zero(self) -> bool {
+    pub fn is_zero(&self) -> bool {
         self.as_nanos() == 0
     }
 
     /// Return `true` if the [`Delta`] spans a negative amount of time.
     #[inline]
-    pub fn is_negative(self) -> bool {
+    pub fn is_negative(&self) -> bool {
         self.as_nanos() < 0
     }
 
     /// Return the number of nanoseconds in the [`Delta`].
     #[inline]
-    pub const fn as_nanos(self) -> i64 {
+    pub const fn as_nanos(&self) -> i64 {
         self.nanos
     }
 
     /// Return the smallest number of microseconds greater than or equal
     /// to the value in the [`Delta`].
     #[inline]
-    pub fn as_micros_ceil(self) -> i64 {
+    pub fn as_micros_ceil(&self) -> i64 {
         #[cfg(CONFIG_64BIT)]
         {
             self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC
@@ -294,7 +294,7 @@ pub fn as_micros_ceil(self) -> i64 {
 
     /// Return the number of milliseconds in the [`Delta`].
     #[inline]
-    pub fn as_millis(self) -> i64 {
+    pub fn as_millis(&self) -> i64 {
         #[cfg(CONFIG_64BIT)]
         {
             self.as_nanos() / NSEC_PER_MSEC
-- 
2.43.0
Re: [PATCH v1 1/5] rust: time: Change Delta methods to take &self instead of self
Posted by Alice Ryhl 8 months, 1 week ago
On Sun, May 04, 2025 at 01:59:54PM +0900, FUJITA Tomonori wrote:
> Change several methods of the `Delta` type in Rust to take `&self`
> instead of `self`. These methods do not mutate or consume the `Delta`
> value and are more idiomatically expressed as taking a shared
> reference. This change improves consistency with common Rust practice
> and allows calling these methods on references without requiring an
> explicit copy or move of the value.

For small values that can be freely copied, I actualy think that using
`self` is more common Rust practice.

Alice
Re: [PATCH v1 1/5] rust: time: Change Delta methods to take &self instead of self
Posted by Andreas Hindborg 8 months, 1 week ago
"Alice Ryhl" <aliceryhl@google.com> writes:

> On Sun, May 04, 2025 at 01:59:54PM +0900, FUJITA Tomonori wrote:
>> Change several methods of the `Delta` type in Rust to take `&self`
>> instead of `self`. These methods do not mutate or consume the `Delta`
>> value and are more idiomatically expressed as taking a shared
>> reference. This change improves consistency with common Rust practice
>> and allows calling these methods on references without requiring an
>> explicit copy or move of the value.
>
> For small values that can be freely copied, I actualy think that using
> `self` is more common Rust practice.

Besides best practice, the value will pass in a register. There is no
benefit at all from passing a reference here and no improved ergonomics
from using a reference.


Best regards,
Andreas Hindborg
Re: [PATCH v1 1/5] rust: time: Change Delta methods to take &self instead of self
Posted by FUJITA Tomonori 8 months, 1 week ago
On Mon, 02 Jun 2025 14:19:38 +0200
Andreas Hindborg <a.hindborg@kernel.org> wrote:

> "Alice Ryhl" <aliceryhl@google.com> writes:
> 
>> On Sun, May 04, 2025 at 01:59:54PM +0900, FUJITA Tomonori wrote:
>>> Change several methods of the `Delta` type in Rust to take `&self`
>>> instead of `self`. These methods do not mutate or consume the `Delta`
>>> value and are more idiomatically expressed as taking a shared
>>> reference. This change improves consistency with common Rust practice
>>> and allows calling these methods on references without requiring an
>>> explicit copy or move of the value.
>>
>> For small values that can be freely copied, I actualy think that using
>> `self` is more common Rust practice.
> 
> Besides best practice, the value will pass in a register. There is no
> benefit at all from passing a reference here and no improved ergonomics
> from using a reference.

Yeah, but somehow I thought that using &self is more common practice
in Rust.

I'll drop this patch in the next version.