diff options
| author | Ben Kimock <kimockb@gmail.com> | 2024-04-11 21:13:57 -0400 |
|---|---|---|
| committer | Ben Kimock <kimockb@gmail.com> | 2024-04-12 18:14:29 -0400 |
| commit | f7d54fa6cb8d5a31914de285efbb79f55b60abb2 (patch) | |
| tree | 0d70779599a1a0f5796fbb34f1a87bfd3c556499 /library/alloc/src/raw_vec.rs | |
| parent | aa6a697a1c75b0aa06954136f7641706edadc2be (diff) | |
| download | rust-f7d54fa6cb8d5a31914de285efbb79f55b60abb2.tar.gz rust-f7d54fa6cb8d5a31914de285efbb79f55b60abb2.zip | |
Avoid more NonNull-raw-NonNull roundtrips in Vec
Diffstat (limited to 'library/alloc/src/raw_vec.rs')
| -rw-r--r-- | library/alloc/src/raw_vec.rs | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 0883080d735..1134c7f833e 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -259,6 +259,17 @@ impl<T, A: Allocator> RawVec<T, A> { Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap, alloc } } + /// A convenience method for hoisting the non-null precondition out of [`RawVec::from_raw_parts_in`]. + /// + /// # Safety + /// + /// See [`RawVec::from_raw_parts_in`]. + #[inline] + pub(crate) unsafe fn from_nonnull_in(ptr: NonNull<T>, capacity: usize, alloc: A) -> Self { + let cap = if T::IS_ZST { Cap::ZERO } else { unsafe { Cap(capacity) } }; + Self { ptr: Unique::from(ptr), cap, alloc } + } + /// Gets a raw pointer to the start of the allocation. Note that this is /// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must /// be careful. |
