Skip to content

Commit 7d23b52

Browse files
committed
const-ify some {IndexVec, IndexSlice} methods
1 parent 99ebfe2 commit 7d23b52

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

compiler/rustc_index/src/slice.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ pub struct IndexSlice<I: Idx, T> {
2323

2424
impl<I: Idx, T> IndexSlice<I, T> {
2525
#[inline]
26-
pub fn empty() -> &'static Self {
27-
Default::default()
26+
pub const fn empty() -> &'static Self {
27+
Self::from_raw(&[])
2828
}
2929

3030
#[inline]
31-
pub fn from_raw(raw: &[T]) -> &Self {
31+
pub const fn from_raw(raw: &[T]) -> &Self {
3232
let ptr: *const [T] = raw;
3333
// SAFETY: `IndexSlice` is `repr(transparent)` over a normal slice
3434
unsafe { &*(ptr as *const Self) }
@@ -42,10 +42,15 @@ impl<I: Idx, T> IndexSlice<I, T> {
4242
}
4343

4444
#[inline]
45-
pub fn len(&self) -> usize {
45+
pub const fn len(&self) -> usize {
4646
self.raw.len()
4747
}
4848

49+
#[inline]
50+
pub const fn is_empty(&self) -> bool {
51+
self.raw.is_empty()
52+
}
53+
4954
/// Gives the next index that will be assigned when `push` is called.
5055
///
5156
/// Manual bounds checks can be done using `idx < slice.next_index()`
@@ -55,11 +60,6 @@ impl<I: Idx, T> IndexSlice<I, T> {
5560
I::new(self.len())
5661
}
5762

58-
#[inline]
59-
pub fn is_empty(&self) -> bool {
60-
self.raw.is_empty()
61-
}
62-
6363
#[inline]
6464
pub fn iter(&self) -> slice::Iter<'_, T> {
6565
self.raw.iter()

compiler/rustc_index/src/vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ pub struct IndexVec<I: Idx, T> {
2626

2727
impl<I: Idx, T> IndexVec<I, T> {
2828
#[inline]
29-
pub fn new() -> Self {
29+
pub const fn new() -> Self {
3030
IndexVec { raw: Vec::new(), _marker: PhantomData }
3131
}
3232

3333
#[inline]
34-
pub fn from_raw(raw: Vec<T>) -> Self {
34+
pub const fn from_raw(raw: Vec<T>) -> Self {
3535
IndexVec { raw, _marker: PhantomData }
3636
}
3737

0 commit comments

Comments
 (0)