Skip to content

Commit 421232a

Browse files
committed
Auto merge of rust-lang#96100 - Raekye:master, r=dtolnay
Change `NonNull::as_uninit_*` to take self by value (as opposed to reference), matching primitive pointers. Copied from my comment on [rust-lang#75402](rust-lang#75402 (comment)): > I noticed that `as_uninit_*` on pointers take `self` by value (and pointers are `Copy`), e.g. see [`as_uninit_mut`](https://doc.rust-lang.org/core/primitive.pointer.html#method.as_uninit_mut). > > However, on `NonNull`, these functions take `self` by reference, e.g. see the function with the same name by for `NonNull`: [`as_uninit_mut`](https://doc.rust-lang.org/std/ptr/struct.NonNull.html#method.as_uninit_mut) takes `self` by mutable reference. Even more inconsistent, [`as_uninit_slice_mut`](https://doc.rust-lang.org/std/ptr/struct.NonNull.html#method.as_uninit_slice_mut) returns a mutable reference, but takes `self` by immutable reference. > > I think these methods should take `self` by value for consistency. The returned lifetime is unbounded anyways and not tied to the pointer/NonNull value anyways I realized the change is trivial (if desired) so here I am creating my first PR. I think it's not a breaking change since (it's on nightly and) `NonNull` is `Copy`; all previous usages of these methods taking `self` by reference should continue to compile. However, it might cause warnings to appear on usages of `NonNull::as_uninit_mut`, which used to require the the `NonNull` variable be declared `mut`, but now it's not necessary.
2 parents 11b913e + 87464fe commit 421232a

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

core/src/ptr/non_null.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<T: Sized> NonNull<T> {
125125
#[must_use]
126126
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
127127
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
128-
pub const unsafe fn as_uninit_ref<'a>(&self) -> &'a MaybeUninit<T> {
128+
pub const unsafe fn as_uninit_ref<'a>(self) -> &'a MaybeUninit<T> {
129129
// SAFETY: the caller must guarantee that `self` meets all the
130130
// requirements for a reference.
131131
unsafe { &*self.cast().as_ptr() }
@@ -159,7 +159,7 @@ impl<T: Sized> NonNull<T> {
159159
#[must_use]
160160
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
161161
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
162-
pub const unsafe fn as_uninit_mut<'a>(&mut self) -> &'a mut MaybeUninit<T> {
162+
pub const unsafe fn as_uninit_mut<'a>(self) -> &'a mut MaybeUninit<T> {
163163
// SAFETY: the caller must guarantee that `self` meets all the
164164
// requirements for a reference.
165165
unsafe { &mut *self.cast().as_ptr() }
@@ -593,7 +593,7 @@ impl<T> NonNull<[T]> {
593593
#[must_use]
594594
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
595595
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
596-
pub const unsafe fn as_uninit_slice<'a>(&self) -> &'a [MaybeUninit<T>] {
596+
pub const unsafe fn as_uninit_slice<'a>(self) -> &'a [MaybeUninit<T>] {
597597
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice`.
598598
unsafe { slice::from_raw_parts(self.cast().as_ptr(), self.len()) }
599599
}
@@ -656,7 +656,7 @@ impl<T> NonNull<[T]> {
656656
#[must_use]
657657
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
658658
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
659-
pub const unsafe fn as_uninit_slice_mut<'a>(&self) -> &'a mut [MaybeUninit<T>] {
659+
pub const unsafe fn as_uninit_slice_mut<'a>(self) -> &'a mut [MaybeUninit<T>] {
660660
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
661661
unsafe { slice::from_raw_parts_mut(self.cast().as_ptr(), self.len()) }
662662
}

0 commit comments

Comments
 (0)