lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250701001809.496389-1-fujita.tomonori@gmail.com>
Date: Tue,  1 Jul 2025 09:18:09 +0900
From: FUJITA Tomonori <fujita.tomonori@...il.com>
To: a.hindborg@...nel.org,
	alex.gaynor@...il.com,
	ojeda@...nel.org
Cc: aliceryhl@...gle.com,
	anna-maria@...utronix.de,
	bjorn3_gh@...tonmail.com,
	boqun.feng@...il.com,
	dakr@...nel.org,
	frederic@...nel.org,
	gary@...yguo.net,
	jstultz@...gle.com,
	linux-kernel@...r.kernel.org,
	lossin@...nel.org,
	lyude@...hat.com,
	rust-for-linux@...r.kernel.org,
	sboyd@...nel.org,
	tglx@...utronix.de,
	tmgross@...ch.edu
Subject: [PATCH v1] rust: time: Add examples with doctest for Delta

Add examples with doctest for Delta methods and associated
functions. These examples explicitly test overflow and saturation
behavior.

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

diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 64c8dcf548d6..c6322275115a 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -228,11 +228,34 @@ impl Delta {
     /// A span of time equal to zero.
     pub const ZERO: Self = Self { nanos: 0 };
 
+    /// Create a new [`Delta`] from a number of nanoseconds.
+    ///
+    /// The `nanos` can range from [`i64::MIN`] to [`i64::MAX`].
+    #[inline]
+    pub const fn from_nanos(nanos: i64) -> Self {
+        Self { nanos }
+    }
+
     /// Create a new [`Delta`] from a number of microseconds.
     ///
     /// The `micros` can range from -9_223_372_036_854_775 to 9_223_372_036_854_775.
     /// If `micros` is outside this range, `i64::MIN` is used for negative values,
     /// and `i64::MAX` is used for positive values due to saturation.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let delta = kernel::time::Delta::from_micros(5);
+    /// assert_eq!(delta.as_nanos(), 5_000);
+    /// let delta = kernel::time::Delta::from_micros(9_223_372_036_854_775);
+    /// assert_eq!(delta.as_nanos(), 9_223_372_036_854_775_000);
+    /// let delta = kernel::time::Delta::from_micros(9_223_372_036_854_776);
+    /// assert_eq!(delta.as_nanos(), i64::MAX);
+    /// let delta = kernel::time::Delta::from_micros(-9_223_372_036_854_775);
+    /// assert_eq!(delta.as_nanos(), -9_223_372_036_854_775_000);
+    /// let delta = kernel::time::Delta::from_micros(-9_223_372_036_854_776);
+    /// assert_eq!(delta.as_nanos(), i64::MIN);
+    /// ```
     #[inline]
     pub const fn from_micros(micros: i64) -> Self {
         Self {
@@ -245,6 +268,21 @@ pub const fn from_micros(micros: i64) -> Self {
     /// The `millis` can range from -9_223_372_036_854 to 9_223_372_036_854.
     /// If `millis` is outside this range, `i64::MIN` is used for negative values,
     /// and `i64::MAX` is used for positive values due to saturation.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let delta = kernel::time::Delta::from_millis(5);
+    /// assert_eq!(delta.as_nanos(), 5_000_000);
+    /// let delta = kernel::time::Delta::from_millis(9_223_372_036_854);
+    /// assert_eq!(delta.as_nanos(), 9_223_372_036_854_000_000);
+    /// let delta = kernel::time::Delta::from_millis(9_223_372_036_855);
+    /// assert_eq!(delta.as_nanos(), i64::MAX);
+    /// let delta = kernel::time::Delta::from_millis(-9_223_372_036_854);
+    /// assert_eq!(delta.as_nanos(), -9_223_372_036_854_000_000);
+    /// let delta = kernel::time::Delta::from_millis(-9_223_372_036_855);
+    /// assert_eq!(delta.as_nanos(), i64::MIN);
+    /// ```
     #[inline]
     pub const fn from_millis(millis: i64) -> Self {
         Self {
@@ -257,6 +295,21 @@ pub const fn from_millis(millis: i64) -> Self {
     /// The `secs` can range from -9_223_372_036 to 9_223_372_036.
     /// If `secs` is outside this range, `i64::MIN` is used for negative values,
     /// and `i64::MAX` is used for positive values due to saturation.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let delta = kernel::time::Delta::from_secs(5);
+    /// assert_eq!(delta.as_nanos(), 5_000_000_000);
+    /// let delta = kernel::time::Delta::from_secs(9_223_372_036);
+    /// assert_eq!(delta.as_nanos(), 9_223_372_036_000_000_000);
+    /// let delta = kernel::time::Delta::from_secs(9_223_372_037);
+    /// assert_eq!(delta.as_nanos(), i64::MAX);
+    /// let delta = kernel::time::Delta::from_secs(-9_223_372_036);
+    /// assert_eq!(delta.as_nanos(), -9_223_372_036_000_000_000);
+    /// let delta = kernel::time::Delta::from_secs(-9_223_372_037);
+    /// assert_eq!(delta.as_nanos(), i64::MIN);
+    /// ```
     #[inline]
     pub const fn from_secs(secs: i64) -> Self {
         Self {
@@ -284,6 +337,13 @@ pub const fn as_nanos(self) -> i64 {
 
     /// Return the smallest number of microseconds greater than or equal
     /// to the value in the [`Delta`].
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let delta = kernel::time::Delta::from_nanos(123_456_789);
+    /// assert_eq!(delta.as_micros_ceil(), 123_457);
+    /// ```
     #[inline]
     pub fn as_micros_ceil(self) -> i64 {
         #[cfg(CONFIG_64BIT)]
@@ -299,6 +359,13 @@ pub fn as_micros_ceil(self) -> i64 {
     }
 
     /// Return the number of milliseconds in the [`Delta`].
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let delta = kernel::time::Delta::from_nanos(123_456_789);
+    /// assert_eq!(delta.as_millis(), 123);
+    /// ```
     #[inline]
     pub fn as_millis(self) -> i64 {
         #[cfg(CONFIG_64BIT)]

base-commit: d4b29ddf82a458935f1bd4909b8a7a13df9d3bdc
-- 
2.43.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ