Skip to content

Commit 1114f83

Browse files
committed
avoid exposing that binary heap's IntoIter is backed by vec::IntoIter, use a private trait instead
1 parent 65e240d commit 1114f83

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/liballoc/collections/binary_heap.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ use core::ops::{Deref, DerefMut};
152152
use core::ptr;
153153

154154
use crate::slice;
155-
use crate::vec::{self, Vec};
155+
use crate::vec::{self, Vec, AsIntoIter};
156156

157157
use super::SpecExtend;
158158

@@ -1147,17 +1147,23 @@ impl<T> FusedIterator for IntoIter<T> {}
11471147

11481148
#[unstable(issue = "0", feature = "inplace_iteration")]
11491149
unsafe impl<T> SourceIter for IntoIter<T> {
1150-
type Source = impl Iterator<Item = T>;
1150+
type Source = IntoIter<T>;
11511151

11521152
#[inline]
11531153
fn as_inner(&mut self) -> &mut Self::Source {
1154-
&mut self.iter
1154+
self
11551155
}
11561156
}
11571157

11581158
#[unstable(issue = "0", feature = "inplace_iteration")]
11591159
unsafe impl<I> InPlaceIterable for IntoIter<I> {}
11601160

1161+
impl<I> AsIntoIter<I> for IntoIter<I> {
1162+
fn as_into_iter(&mut self) -> &mut vec::IntoIter<I> {
1163+
&mut self.iter
1164+
}
1165+
}
1166+
11611167
#[unstable(feature = "binary_heap_into_iter_sorted", issue = "59278")]
11621168
#[derive(Clone, Debug)]
11631169
pub struct IntoIterSorted<T> {

src/liballoc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
#![feature(associated_type_bounds)]
124124
#![feature(inplace_iteration)]
125125
#![feature(type_alias_impl_trait)]
126-
#![cfg_attr(bootstrap, feature(never_type))]
126+
#![feature(never_type)]
127127

128128
// Allow testing this library
129129

src/liballoc/vec.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,7 +2116,7 @@ impl<T> SpecFrom<T, IntoIter<T>> for Vec<T> {
21162116
// T can be split into IN and OUT which only need to have the same size and alignment
21172117
impl<T, I> SpecFrom<T, I> for Vec<T>
21182118
where
2119-
I: Iterator<Item = T> + InPlaceIterable + SourceIter<Source = IntoIter<T>>,
2119+
I: Iterator<Item = T> + InPlaceIterable + SourceIter<Source: AsIntoIter<T>>,
21202120
{
21212121
default fn from_iter(mut iterator: I) -> Self {
21222122
// This specialization only makes sense if we're juggling real allocations.
@@ -2125,8 +2125,8 @@ where
21252125
return SpecFromNested::from_iter(iterator);
21262126
}
21272127

2128-
let src_buf = iterator.as_inner().buf.as_ptr();
2129-
let src_end = iterator.as_inner().end;
2128+
let src_buf = iterator.as_inner().as_into_iter().buf.as_ptr();
2129+
let src_end = iterator.as_inner().as_into_iter().end;
21302130
let dst = src_buf;
21312131

21322132
let dst = if mem::needs_drop::<T>() {
@@ -2168,14 +2168,14 @@ where
21682168
.unwrap()
21692169
};
21702170

2171-
let src = iterator.as_inner();
2171+
let src = iterator.as_inner().as_into_iter();
21722172
// check if SourceIter and InPlaceIterable contracts were upheld.
21732173
// caveat: if they weren't we may not even make it to this point
21742174
debug_assert_eq!(src_buf, src.buf.as_ptr());
21752175
debug_assert!(dst as *const _ <= src.ptr, "InPlaceIterable contract violation");
21762176

21772177
if mem::needs_drop::<T>() {
2178-
// drop tail if iterator was only partially exhaused
2178+
// drop tail if iterator was only partially exhausted
21792179
unsafe {
21802180
ptr::drop_in_place(src.as_mut_slice());
21812181
}
@@ -2844,6 +2844,17 @@ unsafe impl<T> SourceIter for IntoIter<T> {
28442844
}
28452845
}
28462846

2847+
// internal helper trait for in-place iteration specialization.
2848+
pub(crate) trait AsIntoIter<T> {
2849+
fn as_into_iter(&mut self) -> &mut IntoIter<T>;
2850+
}
2851+
2852+
impl<T> AsIntoIter<T> for IntoIter<T> {
2853+
fn as_into_iter(&mut self) -> &mut IntoIter<T> {
2854+
self
2855+
}
2856+
}
2857+
28472858
/// A draining iterator for `Vec<T>`.
28482859
///
28492860
/// This `struct` is created by the [`drain`] method on [`Vec`].

0 commit comments

Comments
 (0)