rusanta-core 0.1.0

Core infrastructure crate for the Rusanta scientific computing ecosystem. Provides shared traits, errors, utilities, and backend abstractions.
Documentation
use std::ops::{Index, IndexMut};
use crate::traits::numeric::Numeric;

/// Contiguous, heap-allocated buffer for storing raw numerical data.
///
/// `Buffer<T>` is the fundamental storage engine used across Rusanta constructs
/// like NdArray, Tensor, and Series.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Buffer<T> {
    data: Vec<T>,
}

impl<T: Numeric> Buffer<T> {
    /// Creates a new `Buffer` with given element values.
    #[inline]
    pub fn new(data: Vec<T>) -> Self {
        Self { data }
    }

    /// Creates a new `Buffer` of specified size filled with a default value.
    #[inline]
    pub fn filled(value: T, len: usize) -> Self {
        Self {
            data: vec![value; len],
        }
    }

    /// Returns the total number of elements stored in the buffer.
    #[inline]
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns `true` if the buffer contains no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Provides an immutable reference to the underlying contiguous slice.
    #[inline]
    pub fn as_slice(&self) -> &[T] {
        &self.data
    }

    /// Provides a mutable reference to the underlying contiguous slice.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        &mut self.data
    }

    /// Converts the buffer into its underlying `Vec<T>`.
    #[inline]
    pub fn into_vec(self) -> Vec<T> {
        self.data
    }
}

// ======================================================
// Standard Trait Implementations
// ======================================================

impl<T> From<Vec<T>> for Buffer<T> {
    #[inline]
    fn from(data: Vec<T>) -> Self {
        Self { data }
    }
}

impl<T> Index<usize> for Buffer<T> {
    type Output = T;

    #[inline]
    fn index(&self, idx: usize) -> &Self::Output {
        &self.data[idx]
    }
}

impl<T> IndexMut<usize> for Buffer<T> {
    #[inline]
    fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
        &mut self.data[idx]
    }
}