Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
orion GONZALEZ (contractor) committed Mar 5, 2024
1 parent cace4b0 commit beb45df
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions compiler/rustc_index/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@ use crate::{Idx, IndexSlice};

/// An owned contiguous collection of `T`s, indexed by `I` rather than by `usize`.
///
/// Why use this instead of a `Vec`?
/// This enforces the user to index a given IndexVec only with the associated index thus making it
/// impossible to use the wrong index for a given `IndexVec`.
/// ## Why use this instead of a `Vec`?
///
/// An `IndexVec` allows element access only via a specific associated index type, meaning that
/// trying to use the wrong index type (possibly accessing an invalid element) will fail at
/// compile time.
///
/// It also documents what the index is indexing: in a `HashMap<usize, Something>` it's not
/// immediately clear what the `usize` means, while a `HashMap<FieldIdx, Something>` makes it obvious.
///
/// ```compile_fail
/// use rustc_index::{Idx, IndexVec};
///
/// fn f<I1: Idx, I2: Idx>(vec1: IndexVec<I1, u8>, idx1: I1, idx2: I2) {
/// &vec1[idx1]; // Ok
/// &vec1[idx2]; // Error!
/// &vec1[idx2]; // Compile error!
/// }
/// ```
///
/// While it's possible to use `u32` or `usize` directly for `I`,
/// you almost certainly want to use a [`newtype_index!`]-generated type instead.
/// you almost certainly want to use a [`newtype_index!`] generated type instead.
///
/// This allows to index the IndexVec with the new index type.
///
Expand Down

0 comments on commit beb45df

Please sign in to comment.