@@ -12,10 +12,26 @@ use std::vec;
12
12
use crate :: { Idx , IndexSlice } ;
13
13
14
14
/// An owned contiguous collection of `T`s, indexed by `I` rather than by `usize`.
15
+ /// Its purpose is to avoid mixing indexes.
15
16
///
16
17
/// While it's possible to use `u32` or `usize` directly for `I`,
17
18
/// you almost certainly want to use a [`newtype_index!`]-generated type instead.
18
19
///
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
+ ///
19
35
/// [`newtype_index!`]: ../macro.newtype_index.html
20
36
#[ derive( Clone , PartialEq , Eq , Hash ) ]
21
37
#[ repr( transparent) ]
@@ -25,11 +41,13 @@ pub struct IndexVec<I: Idx, T> {
25
41
}
26
42
27
43
impl < I : Idx , T > IndexVec < I , T > {
44
+ /// Constructs a new, empty `IndexVec<I, T>`.
28
45
#[ inline]
29
46
pub const fn new ( ) -> Self {
30
47
IndexVec :: from_raw ( Vec :: new ( ) )
31
48
}
32
49
50
+ /// Constructs a new `IndexVec<I, T>` from a `Vec<T>`
33
51
#[ inline]
34
52
pub const fn from_raw ( raw : Vec < T > ) -> Self {
35
53
IndexVec { raw, _marker : PhantomData }
@@ -59,6 +77,7 @@ impl<I: Idx, T> IndexVec<I, T> {
59
77
IndexVec :: from_raw ( vec ! [ elem; universe. len( ) ] )
60
78
}
61
79
80
+ /// Creates a new `IndexVec` with n copies of `elem`
62
81
#[ inline]
63
82
pub fn from_elem_n ( elem : T , n : usize ) -> Self
64
83
where
0 commit comments