Skip to content

Commit 583802c

Browse files
LyudeAndreas Hindborg
authored andcommitted
rust: time: Add Instant::from_ktime()
For implementing Rust bindings which can return a point in time. Signed-off-by: Lyude Paul <lyude@redhat.com> Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Link: https://lore.kernel.org/r/20250821193259.964504-7-lyude@redhat.com Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
1 parent ac0a7bd commit 583802c

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

rust/kernel/time.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,29 @@ impl<C: ClockSource> Instant<C> {
200200
pub(crate) fn as_nanos(&self) -> i64 {
201201
self.inner
202202
}
203+
204+
/// Create an [`Instant`] from a `ktime_t` without checking if it is non-negative.
205+
///
206+
/// # Panics
207+
///
208+
/// On debug builds, this function will panic if `ktime` is not in the range from 0 to
209+
/// `KTIME_MAX`.
210+
///
211+
/// # Safety
212+
///
213+
/// The caller promises that `ktime` is in the range from 0 to `KTIME_MAX`.
214+
#[expect(unused)]
215+
#[inline]
216+
pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self {
217+
debug_assert!(ktime >= 0);
218+
219+
// INVARIANT: Our safety contract ensures that `ktime` is in the range from 0 to
220+
// `KTIME_MAX`.
221+
Self {
222+
inner: ktime,
223+
_c: PhantomData,
224+
}
225+
}
203226
}
204227

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

0 commit comments

Comments
 (0)