Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions compiler/rustc_index/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ impl<I: Idx, T> IndexVec<I, T> {
pub fn append(&mut self, other: &mut Self) {
self.raw.append(&mut other.raw);
}

#[inline]
pub fn debug_map_view(&self) -> IndexSliceMapView<'_, I, T> {
IndexSliceMapView(self.as_slice())
}
}

/// `IndexVec` is often used as a map, so it provides some map-like APIs.
Expand All @@ -220,14 +225,44 @@ impl<I: Idx, T> IndexVec<I, Option<T>> {
pub fn contains(&self, index: I) -> bool {
self.get(index).and_then(Option::as_ref).is_some()
}

#[inline]
pub fn debug_map_view_compact(&self) -> IndexSliceMapViewCompact<'_, I, T> {
IndexSliceMapViewCompact(self.as_slice())
}
}

pub struct IndexSliceMapView<'a, I: Idx, T>(&'a IndexSlice<I, T>);
pub struct IndexSliceMapViewCompact<'a, I: Idx, T>(&'a IndexSlice<I, Option<T>>);

impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexVec<I, T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.raw, fmt)
}
}

impl<'a, I: Idx, T: fmt::Debug> fmt::Debug for IndexSliceMapView<'a, I, T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut entries = fmt.debug_map();
for (idx, val) in self.0.iter_enumerated() {
entries.entry(&idx, val);
}
entries.finish()
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just make this the impl for IndexVec and IndexSlice?


impl<'a, I: Idx, T: fmt::Debug> fmt::Debug for IndexSliceMapViewCompact<'a, I, T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut entries = fmt.debug_map();
for (idx, val) in self.0.iter_enumerated() {
if let Some(val) = val {
entries.entry(&idx, val);
}
}
entries.finish()
}
}

impl<I: Idx, T> Deref for IndexVec<I, T> {
type Target = IndexSlice<I, T>;

Expand Down
Loading