Skip to content

Commit 13815e4

Browse files
authored
Rollup merge of #72811 - pickfire:liballoc-impl, r=Amanieu
Liballoc impl Mainly code rearrangements
2 parents e135087 + 7140463 commit 13815e4

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

src/liballoc/raw_vec.rs

+24-26
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,30 @@ impl<T> RawVec<T, Global> {
118118
RawVec::from_raw_parts(slice.as_mut_ptr(), slice.len())
119119
}
120120
}
121+
122+
/// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`.
123+
///
124+
/// Note that this will correctly reconstitute any `cap` changes
125+
/// that may have been performed. (See description of type for details.)
126+
///
127+
/// # Safety
128+
///
129+
/// * `len` must be greater than or equal to the most recently requested capacity, and
130+
/// * `len` must be less than or equal to `self.capacity()`.
131+
///
132+
/// Note, that the requested capacity and `self.capacity()` could differ, as
133+
/// an allocator could overallocate and return a greater memory block than requested.
134+
pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit<T>]> {
135+
// Sanity-check one half of the safety requirement (we cannot check the other half).
136+
debug_assert!(
137+
len <= self.capacity(),
138+
"`len` must be smaller than or equal to `self.capacity()`"
139+
);
140+
141+
let me = ManuallyDrop::new(self);
142+
let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len);
143+
Box::from_raw(slice)
144+
}
121145
}
122146

123147
impl<T, A: AllocRef> RawVec<T, A> {
@@ -520,32 +544,6 @@ where
520544
Ok(memory)
521545
}
522546

523-
impl<T> RawVec<T, Global> {
524-
/// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`.
525-
///
526-
/// Note that this will correctly reconstitute any `cap` changes
527-
/// that may have been performed. (See description of type for details.)
528-
///
529-
/// # Safety
530-
///
531-
/// * `len` must be greater than or equal to the most recently requested capacity, and
532-
/// * `len` must be less than or equal to `self.capacity()`.
533-
///
534-
/// Note, that the requested capacity and `self.capacity()` could differ, as
535-
/// an allocator could overallocate and return a greater memory block than requested.
536-
pub unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit<T>]> {
537-
// Sanity-check one half of the safety requirement (we cannot check the other half).
538-
debug_assert!(
539-
len <= self.capacity(),
540-
"`len` must be smaller than or equal to `self.capacity()`"
541-
);
542-
543-
let me = ManuallyDrop::new(self);
544-
let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len);
545-
Box::from_raw(slice)
546-
}
547-
}
548-
549547
unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> {
550548
/// Frees the memory owned by the `RawVec` *without* trying to drop its contents.
551549
fn drop(&mut self) {

src/liballoc/vec.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,22 @@ unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
19051905
// Common trait implementations for Vec
19061906
////////////////////////////////////////////////////////////////////////////////
19071907

1908+
#[stable(feature = "rust1", since = "1.0.0")]
1909+
impl<T> ops::Deref for Vec<T> {
1910+
type Target = [T];
1911+
1912+
fn deref(&self) -> &[T] {
1913+
unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
1914+
}
1915+
}
1916+
1917+
#[stable(feature = "rust1", since = "1.0.0")]
1918+
impl<T> ops::DerefMut for Vec<T> {
1919+
fn deref_mut(&mut self) -> &mut [T] {
1920+
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
1921+
}
1922+
}
1923+
19081924
#[stable(feature = "rust1", since = "1.0.0")]
19091925
impl<T: Clone> Clone for Vec<T> {
19101926
#[cfg(not(test))]
@@ -1960,22 +1976,6 @@ impl<T, I: SliceIndex<[T]>> IndexMut<I> for Vec<T> {
19601976
}
19611977
}
19621978

1963-
#[stable(feature = "rust1", since = "1.0.0")]
1964-
impl<T> ops::Deref for Vec<T> {
1965-
type Target = [T];
1966-
1967-
fn deref(&self) -> &[T] {
1968-
unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
1969-
}
1970-
}
1971-
1972-
#[stable(feature = "rust1", since = "1.0.0")]
1973-
impl<T> ops::DerefMut for Vec<T> {
1974-
fn deref_mut(&mut self) -> &mut [T] {
1975-
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
1976-
}
1977-
}
1978-
19791979
#[stable(feature = "rust1", since = "1.0.0")]
19801980
impl<T> FromIterator<T> for Vec<T> {
19811981
#[inline]

0 commit comments

Comments
 (0)