From 34eaf52829b193c8a87c17b4011e342ebf7ca90a Mon Sep 17 00:00:00 2001 From: woppopo Date: Sun, 12 Dec 2021 04:27:43 +0900 Subject: [PATCH] Make `Unique`s methods `const` --- library/core/src/lib.rs | 1 + library/core/src/ptr/unique.rs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 78383b54c5d1e..d9a40a9b2ec0d 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -124,6 +124,7 @@ #![feature(const_option)] #![feature(const_pin)] #![feature(const_replace)] +#![feature(const_ptr_is_null)] #![feature(const_ptr_offset)] #![feature(const_ptr_offset_from)] #![feature(const_ptr_read)] diff --git a/library/core/src/ptr/unique.rs b/library/core/src/ptr/unique.rs index d650a6f974b97..f5c624c225f26 100644 --- a/library/core/src/ptr/unique.rs +++ b/library/core/src/ptr/unique.rs @@ -92,7 +92,7 @@ impl Unique { /// Creates a new `Unique` if `ptr` is non-null. #[inline] - pub fn new(ptr: *mut T) -> Option { + pub const fn new(ptr: *mut T) -> Option { if !ptr.is_null() { // SAFETY: The pointer has already been checked and is not null. Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } }) @@ -115,7 +115,7 @@ impl Unique { /// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`. #[must_use] #[inline] - pub unsafe fn as_ref(&self) -> &T { + pub const unsafe fn as_ref(&self) -> &T { // SAFETY: the caller must guarantee that `self` meets all the // requirements for a reference. unsafe { &*self.as_ptr() } @@ -128,7 +128,7 @@ impl Unique { /// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`. #[must_use] #[inline] - pub unsafe fn as_mut(&mut self) -> &mut T { + pub const unsafe fn as_mut(&mut self) -> &mut T { // SAFETY: the caller must guarantee that `self` meets all the // requirements for a mutable reference. unsafe { &mut *self.as_ptr() }