Commit f87de691 authored by Onur Özkan's avatar Onur Özkan Committed by Danilo Krummrich
Browse files

rust: make `kvec::Vec` functions `const fn`



Makes various `kvec::Vec` functions `const fn`
to allow compile-time evaluation.

Signed-off-by: default avatarOnur Özkan <work@onurozkan.dev>
Reviewed-by: default avatarAlice Ryhl <aliceryhl@google.com>
Reviewed-by: default avatarBenno Lossin <lossin@kernel.org>
Link: https://lore.kernel.org/r/20250720094838.29530-4-work@onurozkan.dev


Signed-off-by: default avatarDanilo Krummrich <dakr@kernel.org>
parent 8d3e8057
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -175,7 +175,7 @@ const fn is_zst() -> bool {

    /// Returns the number of elements that can be stored within the vector without allocating
    /// additional memory.
    pub fn capacity(&self) -> usize {
    pub const fn capacity(&self) -> usize {
        if const { Self::is_zst() } {
            usize::MAX
        } else {
@@ -185,7 +185,7 @@ pub fn capacity(&self) -> usize {

    /// Returns the number of elements stored within the vector.
    #[inline]
    pub fn len(&self) -> usize {
    pub const fn len(&self) -> usize {
        self.len
    }

@@ -196,7 +196,7 @@ pub fn len(&self) -> usize {
    /// - `additional` must be less than or equal to `self.capacity - self.len`.
    /// - All elements within the interval [`self.len`,`self.len + additional`) must be initialized.
    #[inline]
    pub unsafe fn inc_len(&mut self, additional: usize) {
    pub const unsafe fn inc_len(&mut self, additional: usize) {
        // Guaranteed by the type invariant to never underflow.
        debug_assert!(additional <= self.capacity() - self.len());
        // INVARIANT: By the safety requirements of this method this represents the exact number of
@@ -255,7 +255,7 @@ pub fn as_mut_ptr(&mut self) -> *mut T {
    /// Returns a raw pointer to the vector's backing buffer, or, if `T` is a ZST, a dangling raw
    /// pointer.
    #[inline]
    pub fn as_ptr(&self) -> *const T {
    pub const fn as_ptr(&self) -> *const T {
        self.ptr.as_ptr()
    }

@@ -271,7 +271,7 @@ pub fn as_ptr(&self) -> *const T {
    /// assert!(!v.is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
    pub const fn is_empty(&self) -> bool {
        self.len() == 0
    }