Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/idxslice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,27 @@ impl<I: Idx, T> IndexSlice<I, [T]> {

/// Returns the underlying slice.
#[inline(always)]
pub fn as_raw_slice_mut(&mut self) -> &mut [T] {
pub const fn as_raw_slice_mut(&mut self) -> &mut [T] {
&mut self.raw
}

/// Returns the underlying slice.
#[inline(always)]
pub fn as_raw_slice(&self) -> &[T] {
pub const fn as_raw_slice(&self) -> &[T] {
&self.raw
}

/// Returns an unsafe mutable pointer to the slice's buffer.
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut T {
pub const fn as_mut_ptr(&mut self) -> *mut T {
self.raw.as_mut_ptr()
}

/// Returns an unsafe pointer to the slice's buffer.
///
/// # Panics
#[inline]
pub fn as_ptr(&self) -> *const T {
pub const fn as_ptr(&self) -> *const T {
self.raw.as_ptr()
}

Expand All @@ -148,7 +148,7 @@ impl<I: Idx, T> IndexSlice<I, [T]> {

/// Returns the length of our slice.
#[inline]
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.raw.len()
}

Expand All @@ -160,7 +160,7 @@ impl<I: Idx, T> IndexSlice<I, [T]> {

/// Returns true if we're empty.
#[inline]
pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.raw.is_empty()
}

Expand Down Expand Up @@ -406,7 +406,7 @@ impl<I: Idx, T> IndexSlice<I, [T]> {

/// Return the the last element, if we are not empty.
#[inline(always)]
pub fn last(&self) -> Option<&T> {
pub const fn last(&self) -> Option<&T> {
self.raw.last()
}

Expand All @@ -418,7 +418,7 @@ impl<I: Idx, T> IndexSlice<I, [T]> {

/// Return the the first element, if we are not empty.
#[inline]
pub fn first(&self) -> Option<&T> {
pub const fn first(&self) -> Option<&T> {
self.raw.first()
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ type Enumerated<Iter, I, T> = iter::Map<iter::Enumerate<Iter>, fn((usize, T)) ->
impl<I: Idx, T> IndexVec<I, T> {
/// Construct a new IndexVec.
#[inline]
pub fn new() -> Self {
pub const fn new() -> Self {
IndexVec { raw: Vec::new(), _marker: PhantomData }
}

Expand Down Expand Up @@ -346,14 +346,14 @@ impl<I: Idx, T> IndexVec<I, T> {

/// Equivalent to accessing our `raw` field, but as a function.
#[inline(always)]
pub fn as_vec(&self) -> &Vec<T> {
pub const fn as_vec(&self) -> &Vec<T> {
&self.raw
}

/// Equivalent to accessing our `raw` field mutably, but as a function, if
/// that's what you'd prefer.
#[inline(always)]
pub fn as_mut_vec(&mut self) -> &mut Vec<T> {
pub const fn as_mut_vec(&mut self) -> &mut Vec<T> {
&mut self.raw
}

Expand Down
27 changes: 14 additions & 13 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ macro_rules! define_nonmax_index_type {
$v const CHECKS_MAX_INDEX: bool = true;

#[inline(always)]
$v fn new(value: usize) -> Self {
$v const fn new(value: usize) -> Self {
Self::from_usize(value)
}

#[inline(always)]
$v fn from_raw(value: nonmax::NonMaxU32) -> Self {
$v const fn from_raw(value: nonmax::NonMaxU32) -> Self {
Self { _raw: value }
}

Expand All @@ -252,11 +252,12 @@ macro_rules! define_nonmax_index_type {
}

#[inline]
$v fn from_usize(value: usize) -> Self {
$v const fn from_usize(value: usize) -> Self {
Self::check_index(value);
nonmax::NonMaxU32::new(value as u32)
.map(|raw| Self { _raw: raw })
.unwrap_or_else(|| panic!("index_vec index overflow: {} is outside the range [0, {})", value, Self::MAX_INDEX))
match nonmax::NonMaxU32::new(value as u32) {
Some(raw) => Self { _raw: raw },
None => panic!("index_vec index overflow"),
}
}

#[inline(always)]
Expand All @@ -270,9 +271,9 @@ macro_rules! define_nonmax_index_type {
}

#[inline]
$v fn check_index(v: usize) {
$v const fn check_index(v: usize) {
if Self::CHECKS_MAX_INDEX && (v > Self::MAX_INDEX) {
$crate::__max_check_fail(v, Self::MAX_INDEX);
panic!("index_vec index overflow");
}
}
}
Expand Down Expand Up @@ -654,13 +655,13 @@ macro_rules! __define_index_type_inner {

/// Construct this index type from a usize. Alias for `from_usize`.
#[inline(always)]
$v fn new(value: usize) -> Self {
$v const fn new(value: usize) -> Self {
Self::from_usize(value)
}

/// Construct this index type from the wrapped integer type.
#[inline(always)]
$v fn from_raw(value: $raw) -> Self {
$v const fn from_raw(value: $raw) -> Self {
Self::from_usize(value as usize)
}

Expand All @@ -686,7 +687,7 @@ macro_rules! __define_index_type_inner {
/// Construct this index type from a usize.
#[expect(clippy::cast_possible_truncation)]
#[inline]
$v fn from_usize(value: usize) -> Self {
$v const fn from_usize(value: usize) -> Self {
Self::check_index(value as usize);
Self { _raw: value as $raw }
}
Expand All @@ -705,9 +706,9 @@ macro_rules! __define_index_type_inner {

/// Asserts `v <= Self::MAX_INDEX` unless Self::CHECKS_MAX_INDEX is false.
#[inline]
$v fn check_index(v: usize) {
$v const fn check_index(v: usize) {
if Self::CHECKS_MAX_INDEX && (v > Self::MAX_INDEX) {
$crate::__max_check_fail(v, Self::MAX_INDEX);
panic!("index_vec index overflow");
}
}
}
Expand Down