[COMMITTED 069/144] gccrs: Introduce `IndexVec`

arthur.cohen@embecosm.com arthur.cohen@embecosm.com
Wed Mar 19 14:43:50 GMT 2025


From: Kushal Pal <kushalpal109@gmail.com>

gcc/rust/ChangeLog:

	* checks/errors/borrowck/rust-bir-place.h (struct Loan):
	Introduce new class `IndexVec` inspired from IndexVec of rust.
	It acts as a wrapper around `std::vector` and lets user specify
	a strong type to use as index.

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
---
 .../checks/errors/borrowck/rust-bir-place.h   | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-place.h b/gcc/rust/checks/errors/borrowck/rust-bir-place.h
index f7018d3af7f..23564608892 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-place.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-place.h
@@ -199,6 +199,25 @@ struct Loan
   location_t location;
 };
 
+// I is the index type, T is the contained type
+template <typename I, typename T> class IndexVec
+{
+  std::vector<T> internal_vector;
+
+public:
+  T &at (I pid) { return internal_vector[pid.value]; }
+  const T &at (I pid) const { return internal_vector[pid.value]; }
+  T &operator[] (I pid) { return internal_vector[pid.value]; }
+  const T &operator[] (I pid) const { return internal_vector[pid.value]; }
+
+  void push_back (T &&param) { internal_vector.push_back (std::move (param)); }
+  template <typename... Args> void emplace_back (Args &&... args)
+  {
+    internal_vector.emplace_back (std::forward<Args> (args)...);
+  }
+  size_t size () const { return internal_vector.size (); }
+};
+
 /** Allocated places and keeps track of paths. */
 class PlaceDB
 {
-- 
2.45.2



More information about the Gcc-rust mailing list