const-ify some {IndexVec, IndexSlice} methods

This commit is contained in:
Maybe Waffle 2023-04-19 11:12:42 +00:00
parent 99ebfe2f15
commit 7d23b52376
2 changed files with 11 additions and 11 deletions

View file

@ -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()`
@ -55,11 +60,6 @@ impl<I: Idx, T> IndexSlice<I, T> {
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()

View file

@ -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 }
}