Skip to content

Commit 281ceb2

Browse files
committed
Document the struct and a few methods
1 parent 9273d63 commit 281ceb2

File tree

1 file changed

+19
-0
lines changed
  • compiler/rustc_index/src

1 file changed

+19
-0
lines changed

compiler/rustc_index/src/vec.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,26 @@ use std::vec;
1212
use crate::{Idx, IndexSlice};
1313

1414
/// An owned contiguous collection of `T`s, indexed by `I` rather than by `usize`.
15+
/// Its purpose is to avoid mixing indexes.
1516
///
1617
/// While it's possible to use `u32` or `usize` directly for `I`,
1718
/// you almost certainly want to use a [`newtype_index!`]-generated type instead.
1819
///
20+
/// This allows to index the IndexVec with the new index type:
21+
///
22+
/// ```
23+
/// use crate as rustc_index;
24+
/// use rustc_index::{IndexVec, newtype_index};
25+
///
26+
/// newtype_index! {
27+
/// pub struct MyIdx {}
28+
/// }
29+
///
30+
/// let my_index_vec: IndexVec<MyIdx, u32> = IndexVec::from_raw(vec![0,1,2,3]);
31+
/// let idx: MyIdx = MyIdx::from_u32(2);
32+
/// assert_eq!(my_index_vec[idx], 2);
33+
/// ```
34+
///
1935
/// [`newtype_index!`]: ../macro.newtype_index.html
2036
#[derive(Clone, PartialEq, Eq, Hash)]
2137
#[repr(transparent)]
@@ -25,11 +41,13 @@ pub struct IndexVec<I: Idx, T> {
2541
}
2642

2743
impl<I: Idx, T> IndexVec<I, T> {
44+
/// Constructs a new, empty `IndexVec<I, T>`.
2845
#[inline]
2946
pub const fn new() -> Self {
3047
IndexVec::from_raw(Vec::new())
3148
}
3249

50+
/// Constructs a new `IndexVec<I, T>` from a `Vec<T>`
3351
#[inline]
3452
pub const fn from_raw(raw: Vec<T>) -> Self {
3553
IndexVec { raw, _marker: PhantomData }
@@ -59,6 +77,7 @@ impl<I: Idx, T> IndexVec<I, T> {
5977
IndexVec::from_raw(vec![elem; universe.len()])
6078
}
6179

80+
/// Creates a new `IndexVec` with n copies of `elem`
6281
#[inline]
6382
pub fn from_elem_n(elem: T, n: usize) -> Self
6483
where

0 commit comments

Comments
 (0)