From c73262483298e7f5d5cb2a91a8a45f849c26a787 Mon Sep 17 00:00:00 2001 From: Sven Van Asbroeck Date: Thu, 3 Jun 2021 19:43:46 -0400 Subject: [PATCH] rust: add safety annotations in of.rs and types.rs See #340 Signed-off-by: Sven Van Asbroeck --- rust/kernel/of.rs | 1 + rust/kernel/types.rs | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs index 2f061a23538e51..06d1bd4a796a81 100644 --- a/rust/kernel/of.rs +++ b/rust/kernel/of.rs @@ -69,6 +69,7 @@ impl PointerWrapper for OfMatchTable { } unsafe fn from_pointer(p: *const c_types::c_void) -> Self { + // SAFETY: The passed pointer comes from a previous call to [`InnerTable::into_pointer()`]. Self(unsafe { InnerTable::from_pointer(p) }) } } diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index bcd065b8a4d544..e9a4d6852351fa 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -54,6 +54,7 @@ impl PointerWrapper for Box { } unsafe fn from_pointer(ptr: *const c_types::c_void) -> Self { + // SAFETY: The passed pointer comes from a previous call to [`Self::into_pointer()`]. unsafe { Box::from_raw(ptr as _) } } } @@ -64,6 +65,7 @@ impl PointerWrapper for Ref { } unsafe fn from_pointer(ptr: *const c_types::c_void) -> Self { + // SAFETY: The passed pointer comes from a previous call to [`Self::into_pointer()`]. unsafe { Ref::from_raw(ptr as _) } } } @@ -74,6 +76,7 @@ impl PointerWrapper for Arc { } unsafe fn from_pointer(ptr: *const c_types::c_void) -> Self { + // SAFETY: The passed pointer comes from a previous call to [`Self::into_pointer()`]. unsafe { Arc::from_raw(ptr as _) } } } @@ -87,7 +90,8 @@ impl PointerWrapper for Pin { } unsafe fn from_pointer(p: *const c_types::c_void) -> Self { - // TODO: Review: SAFETY: The object was originally pinned. + // SAFETY: The object was originally pinned. + // The passed pointer comes from a previous call to `inner::into_pointer()`. unsafe { Pin::new_unchecked(T::from_pointer(p)) } } }