Skip to content

Commit 2bb1d8e

Browse files
committed
Make into_parts methods on Vec associated functions
This is more consistent with `Box::into_raw()` and clears out potential confusion about whether the method acts on a vector or on a slice.
1 parent 7b344ad commit 2bb1d8e

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3140,7 +3140,7 @@ impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> {
31403140
/// any additional memory.
31413141
#[inline]
31423142
fn from(other: Vec<T, A>) -> Self {
3143-
let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc();
3143+
let (ptr, len, cap, alloc) = Vec::into_raw_parts_with_alloc(other);
31443144
Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } }
31453145
}
31463146
}

library/alloc/src/rc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2811,7 +2811,7 @@ impl<T, A: Allocator> From<Vec<T, A>> for Rc<[T], A> {
28112811
#[inline]
28122812
fn from(v: Vec<T, A>) -> Rc<[T], A> {
28132813
unsafe {
2814-
let (vec_ptr, len, cap, alloc) = v.into_raw_parts_with_alloc();
2814+
let (vec_ptr, len, cap, alloc) = Vec::into_raw_parts_with_alloc(v);
28152815

28162816
let rc_ptr = Self::allocate_for_slice_in(len, &alloc);
28172817
ptr::copy_nonoverlapping(vec_ptr, (&raw mut (*rc_ptr).value) as *mut T, len);

library/alloc/src/string.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,15 +935,15 @@ impl String {
935935
/// #![feature(vec_into_raw_parts)]
936936
/// let s = String::from("hello");
937937
///
938-
/// let (ptr, len, cap) = s.into_raw_parts();
938+
/// let (ptr, len, cap) = String::into_raw_parts(s);
939939
///
940940
/// let rebuilt = unsafe { String::from_raw_parts(ptr, len, cap) };
941941
/// assert_eq!(rebuilt, "hello");
942942
/// ```
943943
#[must_use = "losing the pointer will leak memory"]
944944
#[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
945-
pub fn into_raw_parts(self) -> (*mut u8, usize, usize) {
946-
self.vec.into_raw_parts()
945+
pub fn into_raw_parts(string: Self) -> (*mut u8, usize, usize) {
946+
Vec::into_raw_parts(string.vec)
947947
}
948948

949949
/// Creates a new `String` from a pointer, a length and a capacity.

library/alloc/src/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3837,7 +3837,7 @@ impl<T, A: Allocator + Clone> From<Vec<T, A>> for Arc<[T], A> {
38373837
#[inline]
38383838
fn from(v: Vec<T, A>) -> Arc<[T], A> {
38393839
unsafe {
3840-
let (vec_ptr, len, cap, alloc) = v.into_raw_parts_with_alloc();
3840+
let (vec_ptr, len, cap, alloc) = Vec::into_raw_parts_with_alloc(v);
38413841

38423842
let rc_ptr = Self::allocate_for_slice_in(len, &alloc);
38433843
ptr::copy_nonoverlapping(vec_ptr, (&raw mut (*rc_ptr).data) as *mut T, len);

library/alloc/src/vec/mod.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A contiguous growable array type with heap-allocated contents, written
1+
//! A contiguous growable array type with heap-allocated contents, writtenalloc/vec/
22
//! `Vec<T>`.
33
//!
44
//! Vectors have *O*(1) indexing, amortized *O*(1) push (to the end) and
@@ -1085,7 +1085,7 @@ impl<T, A: Allocator> Vec<T, A> {
10851085
/// #![feature(vec_into_raw_parts)]
10861086
/// let v: Vec<i32> = vec![-1, 0, 1];
10871087
///
1088-
/// let (ptr, len, cap) = v.into_raw_parts();
1088+
/// let (ptr, len, cap) = Vec::into_raw_parts(v);
10891089
///
10901090
/// let rebuilt = unsafe {
10911091
/// // We can now make changes to the components, such as
@@ -1098,8 +1098,8 @@ impl<T, A: Allocator> Vec<T, A> {
10981098
/// ```
10991099
#[must_use = "losing the pointer will leak memory"]
11001100
#[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
1101-
pub fn into_raw_parts(self) -> (*mut T, usize, usize) {
1102-
let mut me = ManuallyDrop::new(self);
1101+
pub fn into_raw_parts(vec: Self) -> (*mut T, usize, usize) {
1102+
let mut me = ManuallyDrop::new(vec);
11031103
(me.as_mut_ptr(), me.len(), me.capacity())
11041104
}
11051105

@@ -1126,7 +1126,7 @@ impl<T, A: Allocator> Vec<T, A> {
11261126
///
11271127
/// let v: Vec<i32> = vec![-1, 0, 1];
11281128
///
1129-
/// let (ptr, len, cap) = v.into_parts();
1129+
/// let (ptr, len, cap) = Vec::into_parts(v);
11301130
///
11311131
/// let rebuilt = unsafe {
11321132
/// // We can now make changes to the components, such as
@@ -1140,8 +1140,8 @@ impl<T, A: Allocator> Vec<T, A> {
11401140
#[must_use = "losing the pointer will leak memory"]
11411141
#[unstable(feature = "box_vec_non_null", reason = "new API", issue = "130364")]
11421142
// #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
1143-
pub fn into_parts(self) -> (NonNull<T>, usize, usize) {
1144-
let (ptr, len, capacity) = self.into_raw_parts();
1143+
pub fn into_parts(vec: Self) -> (NonNull<T>, usize, usize) {
1144+
let (ptr, len, capacity) = Self::into_raw_parts(vec);
11451145
// SAFETY: A `Vec` always has a non-null pointer.
11461146
(unsafe { NonNull::new_unchecked(ptr) }, len, capacity)
11471147
}
@@ -1172,7 +1172,7 @@ impl<T, A: Allocator> Vec<T, A> {
11721172
/// v.push(0);
11731173
/// v.push(1);
11741174
///
1175-
/// let (ptr, len, cap, alloc) = v.into_raw_parts_with_alloc();
1175+
/// let (ptr, len, cap, alloc) = Vec::into_raw_parts_with_alloc(v);
11761176
///
11771177
/// let rebuilt = unsafe {
11781178
/// // We can now make changes to the components, such as
@@ -1186,8 +1186,8 @@ impl<T, A: Allocator> Vec<T, A> {
11861186
#[must_use = "losing the pointer will leak memory"]
11871187
#[unstable(feature = "allocator_api", issue = "32838")]
11881188
// #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
1189-
pub fn into_raw_parts_with_alloc(self) -> (*mut T, usize, usize, A) {
1190-
let mut me = ManuallyDrop::new(self);
1189+
pub fn into_raw_parts_with_alloc(vec: Self) -> (*mut T, usize, usize, A) {
1190+
let mut me = ManuallyDrop::new(vec);
11911191
let len = me.len();
11921192
let capacity = me.capacity();
11931193
let ptr = me.as_mut_ptr();
@@ -1222,7 +1222,7 @@ impl<T, A: Allocator> Vec<T, A> {
12221222
/// v.push(0);
12231223
/// v.push(1);
12241224
///
1225-
/// let (ptr, len, cap, alloc) = v.into_parts_with_alloc();
1225+
/// let (ptr, len, cap, alloc) = Vec::into_parts_with_alloc(v);
12261226
///
12271227
/// let rebuilt = unsafe {
12281228
/// // We can now make changes to the components, such as
@@ -1237,8 +1237,8 @@ impl<T, A: Allocator> Vec<T, A> {
12371237
#[unstable(feature = "allocator_api", issue = "32838")]
12381238
// #[unstable(feature = "box_vec_non_null", reason = "new API", issue = "130364")]
12391239
// #[unstable(feature = "vec_into_raw_parts", reason = "new API", issue = "65816")]
1240-
pub fn into_parts_with_alloc(self) -> (NonNull<T>, usize, usize, A) {
1241-
let (ptr, len, capacity, alloc) = self.into_raw_parts_with_alloc();
1240+
pub fn into_parts_with_alloc(vec: Self) -> (NonNull<T>, usize, usize, A) {
1241+
let (ptr, len, capacity, alloc) = Vec::into_raw_parts_with_alloc(vec);
12421242
// SAFETY: A `Vec` always has a non-null pointer.
12431243
(unsafe { NonNull::new_unchecked(ptr) }, len, capacity, alloc)
12441244
}
@@ -3132,7 +3132,7 @@ impl<T, A: Allocator, const N: usize> Vec<[T; N], A> {
31323132
/// ```
31333133
#[stable(feature = "slice_flatten", since = "1.80.0")]
31343134
pub fn into_flattened(self) -> Vec<T, A> {
3135-
let (ptr, len, cap, alloc) = self.into_raw_parts_with_alloc();
3135+
let (ptr, len, cap, alloc) = Vec::into_raw_parts_with_alloc(self);
31363136
let (new_len, new_cap) = if T::IS_ZST {
31373137
(len.checked_mul(N).expect("vec len overflow"), usize::MAX)
31383138
} else {

0 commit comments

Comments
 (0)