Skip to content
/ rust Public
forked from rust-lang/rust

Commit f7d54fa

Browse files
committed
Avoid more NonNull-raw-NonNull roundtrips in Vec
1 parent aa6a697 commit f7d54fa

File tree

8 files changed

+57
-17
lines changed

8 files changed

+57
-17
lines changed

library/alloc/src/raw_vec.rs

+11
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,17 @@ impl<T, A: Allocator> RawVec<T, A> {
259259
Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap, alloc }
260260
}
261261

262+
/// A convenience method for hoisting the non-null precondition out of [`RawVec::from_raw_parts_in`].
263+
///
264+
/// # Safety
265+
///
266+
/// See [`RawVec::from_raw_parts_in`].
267+
#[inline]
268+
pub(crate) unsafe fn from_nonnull_in(ptr: NonNull<T>, capacity: usize, alloc: A) -> Self {
269+
let cap = if T::IS_ZST { Cap::ZERO } else { unsafe { Cap(capacity) } };
270+
Self { ptr: Unique::from(ptr), cap, alloc }
271+
}
272+
262273
/// Gets a raw pointer to the start of the allocation. Note that this is
263274
/// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must
264275
/// be careful.

library/alloc/src/vec/in_place_collect.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ use core::iter::{InPlaceIterable, SourceIter, TrustedRandomAccessNoCoerce};
161161
use core::marker::PhantomData;
162162
use core::mem::{self, ManuallyDrop, SizedTypeProperties};
163163
use core::num::NonZero;
164-
use core::ptr::{self, NonNull};
164+
use core::ptr;
165165

166166
use super::{InPlaceDrop, InPlaceDstDataSrcBufDrop, SpecFromIter, SpecFromIterNested, Vec};
167167

@@ -254,28 +254,30 @@ where
254254
let (src_buf, src_ptr, src_cap, mut dst_buf, dst_end, dst_cap) = unsafe {
255255
let inner = iterator.as_inner().as_into_iter();
256256
(
257-
inner.buf.as_ptr(),
257+
inner.buf,
258258
inner.ptr,
259259
inner.cap,
260-
inner.buf.as_ptr() as *mut T,
260+
inner.buf.cast::<T>(),
261261
inner.end as *const T,
262262
inner.cap * mem::size_of::<I::Src>() / mem::size_of::<T>(),
263263
)
264264
};
265265

266266
// SAFETY: `dst_buf` and `dst_end` are the start and end of the buffer.
267-
let len = unsafe { SpecInPlaceCollect::collect_in_place(&mut iterator, dst_buf, dst_end) };
267+
let len = unsafe {
268+
SpecInPlaceCollect::collect_in_place(&mut iterator, dst_buf.as_ptr() as *mut T, dst_end)
269+
};
268270

269271
let src = unsafe { iterator.as_inner().as_into_iter() };
270272
// check if SourceIter contract was upheld
271273
// caveat: if they weren't we might not even make it to this point
272-
debug_assert_eq!(src_buf, src.buf.as_ptr());
274+
debug_assert_eq!(src_buf, src.buf);
273275
// check InPlaceIterable contract. This is only possible if the iterator advanced the
274276
// source pointer at all. If it uses unchecked access via TrustedRandomAccess
275277
// then the source pointer will stay in its initial position and we can't use it as reference
276278
if src.ptr != src_ptr {
277279
debug_assert!(
278-
unsafe { dst_buf.add(len) as *const _ } <= src.ptr.as_ptr(),
280+
unsafe { dst_buf.add(len).cast() } <= src.ptr,
279281
"InPlaceIterable contract violation, write pointer advanced beyond read pointer"
280282
);
281283
}
@@ -315,18 +317,17 @@ where
315317
let dst_size = mem::size_of::<T>().unchecked_mul(dst_cap);
316318
let new_layout = Layout::from_size_align_unchecked(dst_size, dst_align);
317319

318-
let result =
319-
alloc.shrink(NonNull::new_unchecked(dst_buf as *mut u8), old_layout, new_layout);
320+
let result = alloc.shrink(dst_buf.cast(), old_layout, new_layout);
320321
let Ok(reallocated) = result else { handle_alloc_error(new_layout) };
321-
dst_buf = reallocated.as_ptr() as *mut T;
322+
dst_buf = reallocated.cast::<T>();
322323
}
323324
} else {
324325
debug_assert_eq!(src_cap * mem::size_of::<I::Src>(), dst_cap * mem::size_of::<T>());
325326
}
326327

327328
mem::forget(dst_guard);
328329

329-
let vec = unsafe { Vec::from_raw_parts(dst_buf, len, dst_cap) };
330+
let vec = unsafe { Vec::from_nonnull(dst_buf, len, dst_cap) };
330331

331332
vec
332333
}

library/alloc/src/vec/in_place_drop.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::marker::PhantomData;
2+
use core::ptr::NonNull;
23
use core::ptr::{self, drop_in_place};
34
use core::slice::{self};
45

@@ -31,7 +32,7 @@ impl<T> Drop for InPlaceDrop<T> {
3132
// the source allocation - i.e. before the reallocation happened - to avoid leaking them
3233
// if some other destructor panics.
3334
pub(super) struct InPlaceDstDataSrcBufDrop<Src, Dest> {
34-
pub(super) ptr: *mut Dest,
35+
pub(super) ptr: NonNull<Dest>,
3536
pub(super) len: usize,
3637
pub(super) src_cap: usize,
3738
pub(super) src: PhantomData<Src>,
@@ -42,8 +43,8 @@ impl<Src, Dest> Drop for InPlaceDstDataSrcBufDrop<Src, Dest> {
4243
fn drop(&mut self) {
4344
unsafe {
4445
let _drop_allocation =
45-
RawVec::<Src>::from_raw_parts_in(self.ptr.cast::<Src>(), self.src_cap, Global);
46-
drop_in_place(core::ptr::slice_from_raw_parts_mut::<Dest>(self.ptr, self.len));
46+
RawVec::<Src>::from_nonnull_in(self.ptr.cast::<Src>(), self.src_cap, Global);
47+
drop_in_place(core::ptr::slice_from_raw_parts_mut::<Dest>(self.ptr.as_ptr(), self.len));
4748
};
4849
}
4950
}

library/alloc/src/vec/into_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> {
433433
// `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec
434434
let alloc = ManuallyDrop::take(&mut self.0.alloc);
435435
// RawVec handles deallocation
436-
let _ = RawVec::from_raw_parts_in(self.0.buf.as_ptr(), self.0.cap, alloc);
436+
let _ = RawVec::from_nonnull_in(self.0.buf, self.0.cap, alloc);
437437
}
438438
}
439439
}

library/alloc/src/vec/mod.rs

+27
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,17 @@ impl<T> Vec<T> {
603603
pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
604604
unsafe { Self::from_raw_parts_in(ptr, length, capacity, Global) }
605605
}
606+
607+
/// A convenience method for hoisting the non-null precondition out of [`Vec::from_raw_parts`].
608+
///
609+
/// # Safety
610+
///
611+
/// See [`Vec::from_raw_parts`].
612+
#[inline]
613+
#[cfg(not(no_global_oom_handling))] // required by tests/run-make/alloc-no-oom-handling
614+
pub(crate) unsafe fn from_nonnull(ptr: NonNull<T>, length: usize, capacity: usize) -> Self {
615+
unsafe { Self::from_nonnull_in(ptr, length, capacity, Global) }
616+
}
606617
}
607618

608619
impl<T, A: Allocator> Vec<T, A> {
@@ -820,6 +831,22 @@ impl<T, A: Allocator> Vec<T, A> {
820831
unsafe { Vec { buf: RawVec::from_raw_parts_in(ptr, capacity, alloc), len: length } }
821832
}
822833

834+
/// A convenience method for hoisting the non-null precondition out of [`Vec::from_raw_parts_in`].
835+
///
836+
/// # Safety
837+
///
838+
/// See [`Vec::from_raw_parts_in`].
839+
#[inline]
840+
#[cfg(not(no_global_oom_handling))] // required by tests/run-make/alloc-no-oom-handling
841+
pub(crate) unsafe fn from_nonnull_in(
842+
ptr: NonNull<T>,
843+
length: usize,
844+
capacity: usize,
845+
alloc: A,
846+
) -> Self {
847+
unsafe { Vec { buf: RawVec::from_nonnull_in(ptr, capacity, alloc), len: length } }
848+
}
849+
823850
/// Decomposes a `Vec<T>` into its raw components: `(pointer, length, capacity)`.
824851
///
825852
/// Returns the raw pointer to the underlying data, the length of

library/alloc/src/vec/spec_from_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<T> SpecFromIter<T, IntoIter<T>> for Vec<T> {
5151
if has_advanced {
5252
ptr::copy(it.ptr.as_ptr(), it.buf.as_ptr(), it.len());
5353
}
54-
return Vec::from_raw_parts(it.buf.as_ptr(), it.len(), it.cap);
54+
return Vec::from_nonnull(it.buf, it.len(), it.cap);
5555
}
5656
}
5757

tests/ui/suggestions/deref-path-method.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: if you're trying to build a new `Vec<_, _>` consider using one of the foll
99
Vec::<T>::with_capacity
1010
Vec::<T>::try_with_capacity
1111
Vec::<T>::from_raw_parts
12-
and 4 others
12+
and 6 others
1313
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
1414
help: the function `contains` is implemented on `[_]`
1515
|

tests/ui/ufcs/bad-builder.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: if you're trying to build a new `Vec<Q>` consider using one of the followi
99
Vec::<T>::with_capacity
1010
Vec::<T>::try_with_capacity
1111
Vec::<T>::from_raw_parts
12-
and 4 others
12+
and 6 others
1313
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
1414
help: there is an associated function `new` with a similar name
1515
|

0 commit comments

Comments
 (0)