From 281ceb2bd2b2dd5f6ac201f585a2de2d21a41250 Mon Sep 17 00:00:00 2001 From: ardi Date: Wed, 10 Jan 2024 10:26:18 +0100 Subject: [PATCH 1/4] Document the struct and a few methods --- compiler/rustc_index/src/vec.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index 66c5cc774b224..3148b054790fc 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -12,10 +12,26 @@ use std::vec; use crate::{Idx, IndexSlice}; /// An owned contiguous collection of `T`s, indexed by `I` rather than by `usize`. +/// Its purpose is to avoid mixing indexes. /// /// While it's possible to use `u32` or `usize` directly for `I`, /// you almost certainly want to use a [`newtype_index!`]-generated type instead. /// +/// This allows to index the IndexVec with the new index type: +/// +/// ``` +/// use crate as rustc_index; +/// use rustc_index::{IndexVec, newtype_index}; +/// +/// newtype_index! { +/// pub struct MyIdx {} +/// } +/// +/// let my_index_vec: IndexVec = IndexVec::from_raw(vec![0,1,2,3]); +/// let idx: MyIdx = MyIdx::from_u32(2); +/// assert_eq!(my_index_vec[idx], 2); +/// ``` +/// /// [`newtype_index!`]: ../macro.newtype_index.html #[derive(Clone, PartialEq, Eq, Hash)] #[repr(transparent)] @@ -25,11 +41,13 @@ pub struct IndexVec { } impl IndexVec { + /// Constructs a new, empty `IndexVec`. #[inline] pub const fn new() -> Self { IndexVec::from_raw(Vec::new()) } + /// Constructs a new `IndexVec` from a `Vec` #[inline] pub const fn from_raw(raw: Vec) -> Self { IndexVec { raw, _marker: PhantomData } @@ -59,6 +77,7 @@ impl IndexVec { IndexVec::from_raw(vec![elem; universe.len()]) } + /// Creates a new `IndexVec` with n copies of `elem` #[inline] pub fn from_elem_n(elem: T, n: usize) -> Self where From 1bf3aee3811b92d5833154fd35b98159a233b938 Mon Sep 17 00:00:00 2001 From: ardi Date: Wed, 10 Jan 2024 11:17:46 +0100 Subject: [PATCH 2/4] Oh well --- compiler/rustc_index/src/vec.rs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index 3148b054790fc..3c66e6dde1c2e 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -17,27 +17,15 @@ use crate::{Idx, IndexSlice}; /// While it's possible to use `u32` or `usize` directly for `I`, /// you almost certainly want to use a [`newtype_index!`]-generated type instead. /// -/// This allows to index the IndexVec with the new index type: +/// This allows to index the IndexVec with the new index type /// -/// ``` -/// use crate as rustc_index; -/// use rustc_index::{IndexVec, newtype_index}; -/// -/// newtype_index! { -/// pub struct MyIdx {} -/// } -/// -/// let my_index_vec: IndexVec = IndexVec::from_raw(vec![0,1,2,3]); -/// let idx: MyIdx = MyIdx::from_u32(2); -/// assert_eq!(my_index_vec[idx], 2); -/// ``` /// /// [`newtype_index!`]: ../macro.newtype_index.html #[derive(Clone, PartialEq, Eq, Hash)] #[repr(transparent)] pub struct IndexVec { pub raw: Vec, - _marker: PhantomData, + _marker: PhantomData, } impl IndexVec { @@ -77,7 +65,7 @@ impl IndexVec { IndexVec::from_raw(vec![elem; universe.len()]) } - /// Creates a new `IndexVec` with n copies of `elem` + /// Creates a new IndexVec #[inline] pub fn from_elem_n(elem: T, n: usize) -> Self where From ee8510e4e13e723639e904cdc218e6f3013f0bb4 Mon Sep 17 00:00:00 2001 From: ardi Date: Wed, 10 Jan 2024 18:28:42 +0100 Subject: [PATCH 3/4] Fix some mistakes + new doc --- compiler/rustc_index/src/vec.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index 3c66e6dde1c2e..73265eb89961c 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -17,15 +17,13 @@ use crate::{Idx, IndexSlice}; /// While it's possible to use `u32` or `usize` directly for `I`, /// you almost certainly want to use a [`newtype_index!`]-generated type instead. /// -/// This allows to index the IndexVec with the new index type -/// -/// +/// This allows to index the IndexVec with the new index type. /// [`newtype_index!`]: ../macro.newtype_index.html #[derive(Clone, PartialEq, Eq, Hash)] #[repr(transparent)] pub struct IndexVec { pub raw: Vec, - _marker: PhantomData, + _marker: PhantomData, } impl IndexVec { @@ -35,7 +33,7 @@ impl IndexVec { IndexVec::from_raw(Vec::new()) } - /// Constructs a new `IndexVec` from a `Vec` + /// Constructs a new `IndexVec` from a `Vec`. #[inline] pub const fn from_raw(raw: Vec) -> Self { IndexVec { raw, _marker: PhantomData } @@ -65,7 +63,7 @@ impl IndexVec { IndexVec::from_raw(vec![elem; universe.len()]) } - /// Creates a new IndexVec + /// Creates a new IndexVec with n copies of the `elem`. #[inline] pub fn from_elem_n(elem: T, n: usize) -> Self where @@ -92,6 +90,7 @@ impl IndexVec { IndexSlice::from_raw_mut(&mut self.raw) } + /// Pushes an element to the array returning the index where it was pushed to. #[inline] pub fn push(&mut self, d: T) -> I { let idx = self.next_index(); From 00ada8e30cb32a45dcac70630a8d4645a3ab393b Mon Sep 17 00:00:00 2001 From: Ardi <47633543+dev-ardi@users.noreply.github.com> Date: Fri, 26 Jan 2024 08:37:37 +0100 Subject: [PATCH 4/4] Update compiler/rustc_index/src/vec.rs Co-authored-by: Wesley Wiser --- compiler/rustc_index/src/vec.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index 73265eb89961c..d876174e620f4 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -18,6 +18,7 @@ use crate::{Idx, IndexSlice}; /// you almost certainly want to use a [`newtype_index!`]-generated type instead. /// /// This allows to index the IndexVec with the new index type. +/// /// [`newtype_index!`]: ../macro.newtype_index.html #[derive(Clone, PartialEq, Eq, Hash)] #[repr(transparent)]