Commit 583802cc authored by Lyude Paul's avatar Lyude Paul Committed by Andreas Hindborg
Browse files

rust: time: Add Instant::from_ktime()



For implementing Rust bindings which can return a point in time.

Signed-off-by: default avatarLyude Paul <lyude@redhat.com>
Reviewed-by: default avatarAndreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: default avatarAlice Ryhl <aliceryhl@google.com>
Reviewed-by: default avatarFUJITA Tomonori <fujita.tomonori@gmail.com>
Link: https://lore.kernel.org/r/20250821193259.964504-7-lyude@redhat.com


Signed-off-by: default avatarAndreas Hindborg <a.hindborg@kernel.org>
parent ac0a7bd2
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -200,6 +200,29 @@ pub fn elapsed(&self) -> Delta {
    pub(crate) fn as_nanos(&self) -> i64 {
        self.inner
    }

    /// Create an [`Instant`] from a `ktime_t` without checking if it is non-negative.
    ///
    /// # Panics
    ///
    /// On debug builds, this function will panic if `ktime` is not in the range from 0 to
    /// `KTIME_MAX`.
    ///
    /// # Safety
    ///
    /// The caller promises that `ktime` is in the range from 0 to `KTIME_MAX`.
    #[expect(unused)]
    #[inline]
    pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self {
        debug_assert!(ktime >= 0);

        // INVARIANT: Our safety contract ensures that `ktime` is in the range from 0 to
        // `KTIME_MAX`.
        Self {
            inner: ktime,
            _c: PhantomData,
        }
    }
}

impl<C: ClockSource> core::ops::Sub for Instant<C> {