kyron-foundation 0.0.1

kyron: foundational components for runtime
Documentation
//
// Copyright (c) 2025 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache License Version 2.0 which is available at
// <https://www.apache.org/licenses/LICENSE-2.0>
//
// SPDX-License-Identifier: Apache-2.0
//

use crate::containers::vector_extension::VectorExtension;
use ::core::{
    alloc::Layout,
    mem::MaybeUninit,
    ops::{Deref, DerefMut},
    slice::{Iter, IterMut},
};

use crate::containers::Vec;
use crate::containers::Vector;

///
/// [`GrowableVec`] is extension to iceoryx2 [`Vec`] with has one time dynamically allocated size. This implementation will grow the size of container when
/// there is no more space while adding data until the call to `lock()` method that does not allow it to grow any-longer. This is useful when You don't know
/// size at compile time, but You are fine with allocations until `init` phase is done. Primary use case is to use in builders when not knowing size in advance.
///
/// The [`GrowableVec`] will grow by `2` each time there is no more space to push value into.
///
pub struct GrowableVec<T> {
    inner: Vec<T>,
    is_locked: bool,
}

impl<T> Default for GrowableVec<T> {
    fn default() -> Self {
        Self::new(1)
    }
}

impl<T> GrowableVec<T> {
    pub fn new(init_size: usize) -> Self {
        assert_eq!(Layout::new::<T>(), Layout::new::<MaybeUninit<T>>());

        Self {
            inner: Vec::new_in_global(init_size),
            is_locked: false,
        }
    }

    /// Locks vector, that it will not grow anymore
    pub fn lock(&mut self) {
        self.is_locked = true;
    }

    /// Returns lock status
    pub fn is_locked(&self) -> bool {
        self.is_locked
    }

    /// Returns the capacity of the vector
    pub fn capacity(&self) -> usize {
        self.inner.capacity()
    }

    /// Returns the number of elements stored inside the vector
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns true if the vector is empty, otherwise false
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Returns true if the vector is full, otherwise false
    pub fn is_full(&self) -> bool {
        self.inner.is_full()
    }

    /// Returns a  slice to the contents of the vector
    pub fn as_slice(&self) -> &[T] {
        let len = self.inner.len();
        unsafe { ::core::slice::from_raw_parts(self.inner.as_slice().as_ptr().cast(), len) }
    }

    /// Returns a mutable slice to the contents of the vector
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        let len = self.inner.len();
        unsafe { ::core::slice::from_raw_parts_mut(self.inner.as_mut_slice().as_mut_ptr().cast(), len) }
    }

    /// Remove and return last elem in container
    pub fn pop(&mut self) -> Option<T> {
        self.inner.pop()
    }

    /// Removes the element at the specified index, and returns the element if the index is valid.
    pub fn remove(&mut self, index: usize) -> Option<T> {
        self.inner.remove(index)
    }

    // Adds `value` to end of vector. Return true if action succeeded
    pub fn push(&mut self, value: T) -> bool {
        if self.inner.is_full() && !self.is_locked {
            self.reallocate_internal();
        }

        self.inner.push(value).is_ok()
    }

    /// Remove all elements in container
    pub fn clear(&mut self) {
        self.inner.clear();
    }

    ///
    /// Simple and naive impl of move by copy, as this is for init phase, we don't care about performance here.
    ///
    fn reallocate_internal(&mut self) {
        // TODO: This is workaround, proper impl is simple but requires access to MetaVec internals which is not possible currently.
        // We can copy a code from MetaVec and adapt if we need something better.
        let mut new_container = Vec::new_in_global(self.inner.capacity() * 2);

        while let Some(v) = self.inner.remove(0) {
            new_container.push(v).expect("Failed to push value during reallocation");
        }

        // move into self
        self.inner = new_container;
    }
}

impl<T> Drop for GrowableVec<T> {
    fn drop(&mut self) {
        self.clear();
    }
}

impl<T> Deref for GrowableVec<T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

impl<T> DerefMut for GrowableVec<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_slice()
    }
}

///
/// Inefficient implementation with double copy
///
/// We only allow conversion into Vec and not vice versa. Hence 'from' is not supported
///
#[allow(clippy::from_over_into)]
impl<T> Into<Vec<T>> for GrowableVec<T> {
    fn into(mut self) -> Vec<T> {
        let mut first = Vec::new_in_global(self.len());

        // Reverse order
        for _ in 0..self.len() {
            first.push(self.pop().unwrap()).expect("Failed to push value during conversion");
        }

        let len = first.len();

        // Correct order...
        for i in 0..(len / 2) {
            first.swap(i, len - i - 1);
        }

        first
    }
}

unsafe impl<T: Send> Send for GrowableVec<T> {} // if type is send, so we are

impl<'a, T> IntoIterator for &'a GrowableVec<T> {
    type Item = &'a T;
    type IntoIter = Iter<'a, T>;

    fn into_iter(self) -> Iter<'a, T> {
        self.iter()
    }
}

impl<'a, T> IntoIterator for &'a mut GrowableVec<T> {
    type Item = &'a mut T;
    type IntoIter = IterMut<'a, T>;

    fn into_iter(self) -> IterMut<'a, T> {
        self.iter_mut()
    }
}

#[cfg(test)]
#[cfg(not(loom))]
mod tests {

    use super::*;

    #[test]
    fn test_for_loop_support() {
        {
            let mut data = GrowableVec::<u8>::new(10);
            data.push(1);
            data.push(2);
            data.push(3);

            let mut i: u8 = 1;
            for e in &data {
                assert_eq!(i, *e);
                i += 1;
            }
        }

        {
            let mut data = GrowableVec::<u8>::new(10);
            data.push(1);
            data.push(2);
            data.push(3);

            let mut i: u8 = 1;
            for e in &mut data {
                assert_eq!(i, *e);

                i += 1;
                *e += 1;
                assert_eq!(i, *e);
            }
        }
    }

    #[test]
    fn test_created_with_correct_size() {
        assert_eq!(100, GrowableVec::<u8>::new(100).capacity());
        assert_eq!(77, GrowableVec::<u8>::new(77).capacity());
        assert_eq!(1234, GrowableVec::<u8>::new(1234).capacity());
    }

    #[test]
    fn test_when_no_more_space_and_not_locked_grows() {
        let mut data = GrowableVec::<u8>::new(3);

        data.push(1);
        data.push(2);
        data.push(3);

        assert!(data.push(1));
        assert_eq!(6, data.capacity()); // double the size
        assert_eq!(4, data.len()); // still 4 items are there
    }

    #[test]
    fn test_when_no_more_space_and_not_locked_grows_and_preserves_order() {
        let mut data = GrowableVec::<u8>::new(3);

        data.push(0);
        data.push(1);
        data.push(2);

        assert!(data.push(3));
        assert_eq!(6, data.capacity()); // double the size
        assert_eq!(4, data.len()); // still 4 items are there

        for i in 0..data.len() {
            assert_eq!(data[i], i as u8);
        }
    }

    #[test]
    fn test_when_no_more_space_and_locked_not_grows() {
        let mut data = GrowableVec::<u8>::new(3);

        data.push(1);
        data.push(2);
        data.push(3);

        data.lock();

        assert!(!data.push(1));
        assert_eq!(3, data.capacity()); // no grow
        assert_eq!(3, data.len()); // same items

        assert!(data.is_locked());
    }

    #[test]
    fn test_is_empty() {
        let mut data = GrowableVec::<u8>::new(1);

        assert!(data.is_empty());
        data.push(1);

        assert!(!data.is_empty());

        data.push(1);
        data.push(1);
        data.push(1);

        data.clear();
        assert!(data.is_empty());
    }

    fn check_data(data: &[usize], count: usize, start_val: usize) {
        for (i, item) in data.iter().enumerate().take(count) {
            assert_eq!(*item, start_val + i);
        }
    }

    #[test]
    fn test_data_is_preserved() {
        fn add_data(data: &mut GrowableVec<usize>, count: usize, start_val: usize) {
            for i in start_val..count + start_val {
                data.push(i);
            }
        }

        let mut data = GrowableVec::<usize>::new(1);

        assert!(data.is_empty());

        add_data(&mut data, 10, 0);
        assert_eq!(10, data.len());
        check_data(&data, 10, 0);

        add_data(&mut data, 20, 10);
        assert_eq!(30, data.len());
        check_data(&data, 30, 0);

        add_data(&mut data, 3, 30);
        assert_eq!(33, data.len());
        check_data(&data, 33, 0);
    }

    #[test]
    fn test_no_double_drop() {
        let mut is_used = false;
        let validator = move || {
            assert!(!is_used);
            is_used = true;
        };

        struct TestData<T: FnMut()> {
            v: T,
        }

        impl<T: FnMut()> Drop for TestData<T> {
            fn drop(&mut self) {
                (self.v)();
            }
        }

        let mut data = GrowableVec::new(1);
        data.push(TestData { v: validator });
        data.push(TestData { v: validator });
    }

    #[test]
    fn test_into_vec() {
        // even
        {
            let mut data = GrowableVec::<usize>::new(1);
            data.push(1);
            data.push(2);
            data.push(3);
            data.push(4);

            let iv: Vec<usize> = data.into();

            assert_eq!(iv.len(), 4);

            check_data(&iv, 4, 1);
        }

        //uneven
        {
            let mut data = GrowableVec::<usize>::new(1);
            data.push(1);
            data.push(2);
            data.push(3);
            data.push(4);
            data.push(5);

            let iv: Vec<usize> = data.into();

            assert_eq!(iv.len(), 5);

            check_data(&iv, 5, 1);
        }
    }
}