From 5d706a22e7b783823afe97a171550decfffb0c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Kr=C3=BCger?= Date: Fri, 27 Mar 2026 21:54:59 +0000 Subject: [PATCH] feat(vector): add index operations without bounds checks --- data/src/components/vector.rs | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/data/src/components/vector.rs b/data/src/components/vector.rs index a72cd5e18e..85983a0d37 100644 --- a/data/src/components/vector.rs +++ b/data/src/components/vector.rs @@ -92,6 +92,26 @@ impl Vector { ) -> Result<(), E> { M::try_resize_with(self, new_len, new_value) } + + /// Perform an [`Self::index`] operation without a bounds check. + /// + /// # Safety + /// + /// The caller must ensure that the provided index is within the vector's bounds. + #[inline(always)] + pub unsafe fn index_unchecked(&self, idx: usize) -> &T { + unsafe { M::index_unchecked(self, idx) } + } + + /// Perform an [`Self::index_mut`] operation without a bounds check. + /// + /// # Safety + /// + /// The caller must ensure that the provided index is within the vector's bounds. + #[inline(always)] + pub unsafe fn index_unchecked_mut(&mut self, idx: usize) -> &mut T { + unsafe { M::index_unchecked_mut(self, idx) } + } } impl Vector { @@ -381,6 +401,20 @@ pub trait VectorMode: Mode { /// See [`Vector::index`]. fn index(this: &Vector, idx: usize) -> &T; + /// See [`Vector::index_unchecked`]. + /// + /// # Safety + /// + /// See notice in [`Vector::index_unchecked`]. + unsafe fn index_unchecked(this: &Vector, idx: usize) -> &T; + + /// See [`Vector::index_unchecked_mut`]. + /// + /// # Safety + /// + /// See notice in [`Vector::index_unchecked_mut`]. + unsafe fn index_unchecked_mut(this: &mut Vector, idx: usize) -> &mut T; + /// See [`Vector::index_mut`]. fn index_mut(this: &mut Vector, idx: usize) -> &mut T; @@ -404,6 +438,16 @@ impl VectorMode for Normal { &this.vector[idx] } + #[inline(always)] + unsafe fn index_unchecked(this: &Vector, idx: usize) -> &T { + unsafe { this.vector.get_unchecked(idx) } + } + + #[inline(always)] + unsafe fn index_unchecked_mut(this: &mut Vector, idx: usize) -> &mut T { + unsafe { this.vector.get_unchecked_mut(idx) } + } + fn index_mut(this: &mut Vector, idx: usize) -> &mut T { &mut this.vector[idx] } @@ -459,6 +503,14 @@ impl VectorMode for Prove<'_> { } } + unsafe fn index_unchecked(this: &Vector, idx: usize) -> &T { + Self::index(this, idx) + } + + unsafe fn index_unchecked_mut(this: &mut Vector, idx: usize) -> &mut T { + Self::index_mut(this, idx) + } + fn index_mut(this: &mut Vector, idx: usize) -> &mut T { this.vector.record_access(idx); @@ -534,6 +586,14 @@ impl VectorMode for Verify { } } + unsafe fn index_unchecked(this: &Vector, idx: usize) -> &T { + Self::index(this, idx) + } + + unsafe fn index_unchecked_mut(this: &mut Vector, idx: usize) -> &mut T { + Self::index_mut(this, idx) + } + fn index_mut(this: &mut Vector, idx: usize) -> &mut T { match this.vector.items.get_mut(idx) { Some(item) => item,