Skip to content

Commit e1d49aa

Browse files
committed
Auto merge of #83821 - camelid:improve-thinvec, r=petrochenkov
Add `FromIterator` and `IntoIterator` impls for `ThinVec` These should make using `ThinVec` feel much more like using `Vec`. They will allow users of `Vec` to switch to `ThinVec` while continuing to use `collect()`, `for` loops, and other parts of the iterator API. I don't know if there were use cases before for using the iterator API with `ThinVec`, but I would like to start using `ThinVec` in rustdoc, and having it conform to the iterator API would make the transition *a lot* easier. I added a `FromIterator` impl, an `IntoIterator` impl that yields owned elements, and `IntoIterator` impls that yield immutable or mutable references to elements. I also added some unit tests for `ThinVec`.
2 parents 354cc75 + 09ff88b commit e1d49aa

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

compiler/rustc_data_structures/src/thin_vec.rs

+49
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::stable_hasher::{HashStable, StableHasher};
22

3+
use std::iter::FromIterator;
4+
35
/// A vector type optimized for cases where this size is usually 0 (cf. `SmallVector`).
46
/// The `Option<Box<..>>` wrapping allows us to represent a zero sized vector with `None`,
57
/// which uses only a single (null) pointer.
@@ -10,6 +12,14 @@ impl<T> ThinVec<T> {
1012
pub fn new() -> Self {
1113
ThinVec(None)
1214
}
15+
16+
pub fn iter(&self) -> std::slice::Iter<'_, T> {
17+
self.into_iter()
18+
}
19+
20+
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> {
21+
self.into_iter()
22+
}
1323
}
1424

1525
impl<T> From<Vec<T>> for ThinVec<T> {
@@ -46,6 +56,42 @@ impl<T> ::std::ops::DerefMut for ThinVec<T> {
4656
}
4757
}
4858

59+
impl<T> FromIterator<T> for ThinVec<T> {
60+
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
61+
// `Vec::from_iter()` should not allocate if the iterator is empty.
62+
let vec: Vec<_> = iter.into_iter().collect();
63+
if vec.is_empty() { ThinVec(None) } else { ThinVec(Some(Box::new(vec))) }
64+
}
65+
}
66+
67+
impl<T> IntoIterator for ThinVec<T> {
68+
type Item = T;
69+
type IntoIter = std::vec::IntoIter<T>;
70+
71+
fn into_iter(self) -> Self::IntoIter {
72+
// This is still performant because `Vec::new()` does not allocate.
73+
self.0.map_or_else(Vec::new, |ptr| *ptr).into_iter()
74+
}
75+
}
76+
77+
impl<'a, T> IntoIterator for &'a ThinVec<T> {
78+
type Item = &'a T;
79+
type IntoIter = std::slice::Iter<'a, T>;
80+
81+
fn into_iter(self) -> Self::IntoIter {
82+
self.as_ref().iter()
83+
}
84+
}
85+
86+
impl<'a, T> IntoIterator for &'a mut ThinVec<T> {
87+
type Item = &'a mut T;
88+
type IntoIter = std::slice::IterMut<'a, T>;
89+
90+
fn into_iter(self) -> Self::IntoIter {
91+
self.as_mut().iter_mut()
92+
}
93+
}
94+
4995
impl<T> Extend<T> for ThinVec<T> {
5096
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
5197
match *self {
@@ -80,3 +126,6 @@ impl<T> Default for ThinVec<T> {
80126
Self(None)
81127
}
82128
}
129+
130+
#[cfg(test)]
131+
mod tests;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use super::*;
2+
3+
impl<T> ThinVec<T> {
4+
fn into_vec(self) -> Vec<T> {
5+
self.into()
6+
}
7+
}
8+
9+
#[test]
10+
fn test_from_iterator() {
11+
assert_eq!(std::iter::empty().collect::<ThinVec<String>>().into_vec(), Vec::<String>::new());
12+
assert_eq!(std::iter::once(42).collect::<ThinVec<_>>().into_vec(), vec![42]);
13+
assert_eq!(vec![1, 2].into_iter().collect::<ThinVec<_>>().into_vec(), vec![1, 2]);
14+
assert_eq!(vec![1, 2, 3].into_iter().collect::<ThinVec<_>>().into_vec(), vec![1, 2, 3]);
15+
}
16+
17+
#[test]
18+
fn test_into_iterator_owned() {
19+
assert_eq!(ThinVec::new().into_iter().collect::<Vec<String>>(), Vec::<String>::new());
20+
assert_eq!(ThinVec::from(vec![1]).into_iter().collect::<Vec<_>>(), vec![1]);
21+
assert_eq!(ThinVec::from(vec![1, 2]).into_iter().collect::<Vec<_>>(), vec![1, 2]);
22+
assert_eq!(ThinVec::from(vec![1, 2, 3]).into_iter().collect::<Vec<_>>(), vec![1, 2, 3]);
23+
}
24+
25+
#[test]
26+
fn test_into_iterator_ref() {
27+
assert_eq!(ThinVec::new().iter().collect::<Vec<&String>>(), Vec::<&String>::new());
28+
assert_eq!(ThinVec::from(vec![1]).iter().collect::<Vec<_>>(), vec![&1]);
29+
assert_eq!(ThinVec::from(vec![1, 2]).iter().collect::<Vec<_>>(), vec![&1, &2]);
30+
assert_eq!(ThinVec::from(vec![1, 2, 3]).iter().collect::<Vec<_>>(), vec![&1, &2, &3]);
31+
}
32+
33+
#[test]
34+
fn test_into_iterator_ref_mut() {
35+
assert_eq!(ThinVec::new().iter_mut().collect::<Vec<&mut String>>(), Vec::<&mut String>::new());
36+
assert_eq!(ThinVec::from(vec![1]).iter_mut().collect::<Vec<_>>(), vec![&mut 1]);
37+
assert_eq!(ThinVec::from(vec![1, 2]).iter_mut().collect::<Vec<_>>(), vec![&mut 1, &mut 2]);
38+
assert_eq!(
39+
ThinVec::from(vec![1, 2, 3]).iter_mut().collect::<Vec<_>>(),
40+
vec![&mut 1, &mut 2, &mut 3],
41+
);
42+
}

0 commit comments

Comments
 (0)