Make metadata and slice accessors const

This commit is contained in:
2026-07-31 03:27:45 +02:00
parent 0b72219db3
commit a3612a320f
4 changed files with 19 additions and 19 deletions
+7 -7
View File
@@ -69,7 +69,7 @@ impl<'vec, T> ContentGuard<'vec, T> {
/// Returns the allocation's capacity in elements.
#[must_use]
pub fn capacity(&self) -> usize {
pub const fn capacity(&self) -> usize {
self.erased.capacity()
}
@@ -80,26 +80,26 @@ impl<'vec, T> ContentGuard<'vec, T> {
/// Returns the number of initialized elements.
#[must_use]
pub fn length(&self) -> usize {
pub const fn length(&self) -> usize {
self.erased.length()
}
/// Returns `true` when there are no initialized elements.
#[must_use]
pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.erased.is_empty()
}
/// Borrows the initialized elements as a slice.
#[must_use]
pub fn as_slice(&self) -> &[T] {
pub const fn as_slice(&self) -> &[T] {
// SAFETY: The pointer and length correctly represent the currently initialized elements.
unsafe { self.erased.parts.as_slice() }
}
/// Mutably borrows the initialized elements as a slice.
#[must_use]
pub fn as_slice_mut(&mut self) -> &mut [T] {
pub const fn as_slice_mut(&mut self) -> &mut [T] {
// SAFETY: The pointer and length correctly represent the currently initialized elements.
unsafe { self.erased.parts.as_slice_mut() }
}
@@ -107,7 +107,7 @@ impl<'vec, T> ContentGuard<'vec, T> {
/// Consumes the guard and borrows the initialized elements for the erased
/// vector's lifetime.
#[must_use]
pub fn into_slice(self) -> &'vec [T] {
pub const fn into_slice(self) -> &'vec [T] {
// SAFETY: The pointer and length correctly represent the currently initialized elements.
unsafe { self.erased.parts.as_slice() }
}
@@ -115,7 +115,7 @@ impl<'vec, T> ContentGuard<'vec, T> {
/// Consumes the guard and mutably borrows the initialized elements for the
/// erased vector's lifetime.
#[must_use]
pub fn into_slice_mut(self) -> &'vec mut [T] {
pub const fn into_slice_mut(self) -> &'vec mut [T] {
// SAFETY: The pointer and length correctly represent the currently initialized elements.
unsafe { self.erased.parts.as_slice_mut() }
}
+5 -5
View File
@@ -101,25 +101,25 @@ impl TypeErasedVec {
/// Returns the element layout associated with the allocation.
#[must_use]
pub fn layout(&self) -> Layout {
pub const fn layout(&self) -> Layout {
self.layout
}
/// Returns the allocation's capacity in elements.
#[must_use]
pub fn capacity(&self) -> usize {
pub const fn capacity(&self) -> usize {
self.parts.cap
}
/// Returns the number of initialized elements.
#[must_use]
pub fn length(&self) -> usize {
pub const fn length(&self) -> usize {
self.parts.len
}
/// Returns `true` when there are no initialized elements.
#[must_use]
pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.parts.len == 0
}
@@ -128,7 +128,7 @@ impl TypeErasedVec {
/// Zero-sized elements always report zero bytes, regardless of their
/// logical capacity.
#[must_use]
pub fn capacity_bytes(&self) -> usize {
pub const fn capacity_bytes(&self) -> usize {
self.parts.cap * self.layout.size()
}
+2 -2
View File
@@ -22,13 +22,13 @@ impl VecParts {
}
#[must_use]
pub(super) unsafe fn as_slice<T>(&self) -> &[T] {
pub(super) const unsafe fn as_slice<T>(&self) -> &[T] {
// SAFETY: Ensured by the caller
unsafe { std::slice::from_raw_parts(self.ptr.as_ptr().cast(), self.len) }
}
#[must_use]
pub(super) unsafe fn as_slice_mut<T>(&mut self) -> &mut [T] {
pub(super) const unsafe fn as_slice_mut<T>(&mut self) -> &mut [T] {
// SAFETY: Ensured by the caller
unsafe { std::slice::from_raw_parts_mut(self.ptr.as_ptr().cast(), self.len) }
}
+5 -5
View File
@@ -44,31 +44,31 @@ macro_rules! define_thread_safe_erased_vec {
/// Returns the element layout associated with the allocation.
#[must_use]
pub fn layout(&self) -> Layout {
pub const fn layout(&self) -> Layout {
self.0.layout()
}
/// Returns the allocation's capacity in elements.
#[must_use]
pub fn capacity(&self) -> usize {
pub const fn capacity(&self) -> usize {
self.0.capacity()
}
/// Returns the number of initialized elements.
#[must_use]
pub fn length(&self) -> usize {
pub const fn length(&self) -> usize {
self.0.length()
}
/// Returns `true` when there are no initialized elements.
#[must_use]
pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Returns the allocation's capacity in bytes.
#[must_use]
pub fn capacity_bytes(&self) -> usize {
pub const fn capacity_bytes(&self) -> usize {
self.0.capacity_bytes()
}