Skip to content

Commit 143227f

Browse files
authored
Rollup merge of #155951 - nnethercote:unsafe-FlatMapInPlaceVec, r=kivooeo
Make `FlatMapInPlaceVec` an unsafe trait. Because the memory safety of `FlatMapInPlace::flat_map_in_place` depends on `FlatMapInPlaceVec` impls behaving correctly. r? @chenyukang
2 parents 99f9bea + a940270 commit 143227f

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

compiler/rustc_data_structures/src/flat_map_in_place.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ impl<V: FlatMapInPlaceVec> FlatMapInPlace<V::Elem> for V {
6767
}
6868
}
6969

70-
// A vec-like type must implement these operations to support `flat_map_in_place`.
71-
pub trait FlatMapInPlaceVec {
70+
/// A vec-like type must implement these operations to support `flat_map_in_place`.
71+
///
72+
/// # Safety
73+
///
74+
/// The memory safety of the unsafe block in `flat_map_in_place` relies on impls of this trait
75+
/// implementing all the operations correctly.
76+
pub unsafe trait FlatMapInPlaceVec {
7277
type Elem;
7378

7479
fn len(&self) -> usize;
@@ -78,7 +83,7 @@ pub trait FlatMapInPlaceVec {
7883
fn insert(&mut self, idx: usize, elem: Self::Elem);
7984
}
8085

81-
impl<T> FlatMapInPlaceVec for Vec<T> {
86+
unsafe impl<T> FlatMapInPlaceVec for Vec<T> {
8287
type Elem = T;
8388

8489
fn len(&self) -> usize {
@@ -104,7 +109,7 @@ impl<T> FlatMapInPlaceVec for Vec<T> {
104109
}
105110
}
106111

107-
impl<T> FlatMapInPlaceVec for ThinVec<T> {
112+
unsafe impl<T> FlatMapInPlaceVec for ThinVec<T> {
108113
type Elem = T;
109114

110115
fn len(&self) -> usize {
@@ -130,7 +135,7 @@ impl<T> FlatMapInPlaceVec for ThinVec<T> {
130135
}
131136
}
132137

133-
impl<T, const N: usize> FlatMapInPlaceVec for SmallVec<[T; N]> {
138+
unsafe impl<T, const N: usize> FlatMapInPlaceVec for SmallVec<[T; N]> {
134139
type Elem = T;
135140

136141
fn len(&self) -> usize {

0 commit comments

Comments
 (0)