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};

/// An immutable, non-owning view over contiguous numerical data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct View<'a, T> {
    data: &'a [T],
}

impl<'a, T> View<'a, T> {
    /// Creates a new `View` referencing the given slice.
    #[inline]
    pub fn new(data: &'a [T]) -> Self {
        Self { data }
    }

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

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

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

impl<'a, T> Index<usize> for View<'a, T> {
    type Output = T;

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

/// A mutable, non-owning view over contiguous numerical data.
#[derive(Debug, PartialEq, Eq)]
pub struct ViewMut<'a, T> {
    data: &'a mut [T],
}

impl<'a, T> ViewMut<'a, T> {
    /// Creates a new `ViewMut` referencing the given mutable slice.
    #[inline]
    pub fn new(data: &'a mut [T]) -> Self {
        Self { data }
    }

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

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

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

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

impl<'a, T> Index<usize> for ViewMut<'a, T> {
    type Output = T;

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

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