From 646946f206341ad4f77866885b2cc99a50184e0f Mon Sep 17 00:00:00 2001 From: DOCtorActoAntohich Date: Sun, 15 Nov 2026 14:41:06 +0300 Subject: [PATCH] mark more functions as const --- src/lib.rs | 1 + src/volatile_ptr/operations.rs | 4 ++-- src/volatile_ref.rs | 10 +++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c419c8b..1559949 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,6 +42,7 @@ #![cfg_attr(docsrs, feature(doc_auto_cfg))] #![cfg_attr(feature = "unstable", allow(internal_features))] #![warn(missing_docs)] +#![warn(clippy::missing_const_for_fn)] #![deny(unsafe_op_in_unsafe_fn)] #![doc(test(attr(deny(warnings))))] #![doc(test(attr(allow(dead_code))))] diff --git a/src/volatile_ptr/operations.rs b/src/volatile_ptr/operations.rs index b3f0e0d..8bc924d 100644 --- a/src/volatile_ptr/operations.rs +++ b/src/volatile_ptr/operations.rs @@ -24,7 +24,7 @@ where /// - The given pointer must be valid. /// - No other thread must have access to the given pointer. This must remain true /// for the whole lifetime of the `VolatilePtr`. - pub unsafe fn new(pointer: NonNull) -> VolatilePtr<'a, T, ReadWrite> { + pub const unsafe fn new(pointer: NonNull) -> VolatilePtr<'a, T, ReadWrite> { unsafe { VolatilePtr::new_restricted(ReadWrite, pointer) } } @@ -156,7 +156,7 @@ where /// assert_eq!(unsafe { *unwrapped }, 50); // non volatile access, be careful! /// ``` #[must_use] - pub fn as_raw_ptr(self) -> NonNull { + pub const fn as_raw_ptr(self) -> NonNull { self.pointer } diff --git a/src/volatile_ref.rs b/src/volatile_ref.rs index c0e7a31..0a9f8e1 100644 --- a/src/volatile_ref.rs +++ b/src/volatile_ref.rs @@ -56,7 +56,7 @@ where /// chosen and does not necessarily reflect the actual lifetime of the data. In particular, /// while this `VolatileRef` exists, the memory the pointer points to must not get accessed /// (_read or written_) through any other pointer. - pub unsafe fn new(pointer: NonNull) -> Self { + pub const unsafe fn new(pointer: NonNull) -> Self { unsafe { VolatileRef::new_restricted(ReadWrite, pointer) } } @@ -101,11 +101,11 @@ where /// so the compiler is allowed to insert non-volatile reads. This might lead to undesired /// (or even undefined?) behavior when accessing volatile data. So to be safe, only create /// raw pointers to volatile data and use the [`Self::new`] constructor instead. - pub fn from_ref(reference: &'a T) -> VolatileRef<'a, T, ReadOnly> + pub const fn from_ref(reference: &'a T) -> VolatileRef<'a, T, ReadOnly> where T: 'a, { - unsafe { VolatileRef::new_restricted(ReadOnly, reference.into()) } + unsafe { VolatileRef::new_restricted(ReadOnly, NonNull::from_ref(reference)) } } /// Creates a `VolatileRef` from the given mutable reference. @@ -115,11 +115,11 @@ where /// so the compiler is allowed to insert non-volatile reads. This might lead to undesired /// (or even undefined?) behavior when accessing volatile data. So to be safe, only create /// raw pointers to volatile data and use the [`Self::new`] constructor instead. - pub fn from_mut_ref(reference: &'a mut T) -> Self + pub const fn from_mut_ref(reference: &'a mut T) -> Self where T: 'a, { - unsafe { VolatileRef::new(reference.into()) } + unsafe { VolatileRef::new(NonNull::from_ref(reference)) } } const unsafe fn new_generic(pointer: NonNull) -> VolatileRef<'a, T, A> {