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
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
"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
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.
© 2016 - 2026 Red Hat, Inc.