diff options
| author | Maybe Waffle <waffle.lapkin@gmail.com> | 2023-04-19 11:12:42 +0000 |
|---|---|---|
| committer | Maybe Waffle <waffle.lapkin@gmail.com> | 2023-04-24 13:53:37 +0000 |
| commit | 7d23b5237623f5d1db9840838bbf5abfe3a14e29 (patch) | |
| tree | 477ed0c1a5d0cd4133c80329cf469c3aaf53efc8 | |
| parent | 99ebfe2f15048377960b006afae5b52252f933a7 (diff) | |
| download | rust-7d23b5237623f5d1db9840838bbf5abfe3a14e29.tar.gz rust-7d23b5237623f5d1db9840838bbf5abfe3a14e29.zip | |
`const`-ify some `{IndexVec, IndexSlice}` methods
| -rw-r--r-- | compiler/rustc_index/src/slice.rs | 18 | ||||
| -rw-r--r-- | 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<I: Idx, T> { impl<I: Idx, T> IndexSlice<I, T> { #[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<I: Idx, T> IndexSlice<I, T> { } #[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()` @@ -56,11 +61,6 @@ impl<I: Idx, T> IndexSlice<I, T> { } #[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<I: Idx, T> { impl<I: Idx, T> IndexVec<I, T> { #[inline] - pub fn new() -> Self { + pub const fn new() -> Self { IndexVec { raw: Vec::new(), _marker: PhantomData } } #[inline] - pub fn from_raw(raw: Vec<T>) -> Self { + pub const fn from_raw(raw: Vec<T>) -> Self { IndexVec { raw, _marker: PhantomData } } |
