Skip to content

Commit

Permalink
Rollup merge of #91806 - woppopo:const_unique, r=dtolnay
Browse files Browse the repository at this point in the history
Make `Unique`s methods `const`

Tracking issue: None
  • Loading branch information
matthiaskrgr committed Dec 11, 2021
2 parents 9aade50 + 34eaf52 commit 9031ac4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 3 additions & 3 deletions library/core/src/ptr/unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<T: ?Sized> Unique<T> {

/// Creates a new `Unique` if `ptr` is non-null.
#[inline]
pub fn new(ptr: *mut T) -> Option<Self> {
pub const fn new(ptr: *mut T) -> Option<Self> {
if !ptr.is_null() {
// SAFETY: The pointer has already been checked and is not null.
Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } })
Expand All @@ -115,7 +115,7 @@ impl<T: ?Sized> Unique<T> {
/// (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() }
Expand All @@ -128,7 +128,7 @@ impl<T: ?Sized> Unique<T> {
/// (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() }
Expand Down

0 comments on commit 9031ac4

Please sign in to comment.