From 7d23b5237623f5d1db9840838bbf5abfe3a14e29 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 19 Apr 2023 11:12:42 +0000 Subject: [PATCH] `const`-ify some `{IndexVec, IndexSlice}` methods --- compiler/rustc_index/src/slice.rs | 18 +++++++++--------- compiler/rustc_index/src/vec.rs | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_index/src/slice.rs b/compiler/rustc_index/src/slice.rs index 8eae079e2f8..0663c7247de 100644 --- a/compiler/rustc_index/src/slice.rs +++ b/compiler/rustc_index/src/slice.rs @@ -23,12 +23,12 @@ pub struct IndexSlice { impl IndexSlice { #[inline] - pub fn empty() -> &'static Self { - Default::default() + pub const fn empty() -> &'static Self { + Self::from_raw(&[]) } #[inline] - pub fn from_raw(raw: &[T]) -> &Self { + pub const fn from_raw(raw: &[T]) -> &Self { let ptr: *const [T] = raw; // SAFETY: `IndexSlice` is `repr(transparent)` over a normal slice unsafe { &*(ptr as *const Self) } @@ -42,10 +42,15 @@ impl IndexSlice { } #[inline] - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.raw.len() } + #[inline] + pub const fn is_empty(&self) -> bool { + self.raw.is_empty() + } + /// Gives the next index that will be assigned when `push` is called. /// /// Manual bounds checks can be done using `idx < slice.next_index()` @@ -55,11 +60,6 @@ impl IndexSlice { I::new(self.len()) } - #[inline] - pub fn is_empty(&self) -> bool { - self.raw.is_empty() - } - #[inline] pub fn iter(&self) -> slice::Iter<'_, T> { self.raw.iter() diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index dce5334461e..5631e2867b0 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -26,12 +26,12 @@ pub struct IndexVec { impl IndexVec { #[inline] - pub fn new() -> Self { + pub const fn new() -> Self { IndexVec { raw: Vec::new(), _marker: PhantomData } } #[inline] - pub fn from_raw(raw: Vec) -> Self { + pub const fn from_raw(raw: Vec) -> Self { IndexVec { raw, _marker: PhantomData } }