diff --git a/src/guard.rs b/src/guard.rs index b59a9d4..3191726 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -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() } } diff --git a/src/lib.rs b/src/lib.rs index 1ca253d..1f0746d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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() } diff --git a/src/parts.rs b/src/parts.rs index a4eca95..f14c6f5 100644 --- a/src/parts.rs +++ b/src/parts.rs @@ -22,13 +22,13 @@ impl VecParts { } #[must_use] - pub(super) unsafe fn as_slice(&self) -> &[T] { + pub(super) const unsafe fn as_slice(&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(&mut self) -> &mut [T] { + pub(super) const unsafe fn as_slice_mut(&mut self) -> &mut [T] { // SAFETY: Ensured by the caller unsafe { std::slice::from_raw_parts_mut(self.ptr.as_ptr().cast(), self.len) } } diff --git a/src/send.rs b/src/send.rs index 0b69809..a253cdf 100644 --- a/src/send.rs +++ b/src/send.rs @@ -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() }