rspace_traits/
space.rs

1/*
2    Appellation: space <module>
3    Created At: 2025.12.26:14:12:46
4    Contrib: @FL03
5*/
6//! This module defines the [`RawSpace`] trait alongside other interfaces for immutable and
7//! mutable access to the inner elements of a container.
8
9/// The [`RawSpace`] trait is used to define a base interface for all containers whose elements
10/// are of **one** specific type.
11pub trait RawSpace {
12    /// The type of elements associated with the space
13    type Elem;
14}
15/// [`ScalarSpace`] defins a type of space that consists of a single element. This trait is
16/// useful in that it generally allows for a tensor-like treatment of scalar values within
17/// more complex mathematical structures.
18pub trait ScalarSpace: RawSpace<Elem = Self> {
19    private! {}
20}
21/// [`RawSpaceRef`] is a trait that provides various read-only methods for accessing elements.
22pub trait RawSpaceRef: RawSpace {
23    fn as_ptr(&self) -> *const Self::Elem;
24}
25/// [`RawSpaceMut`] is a trait that provides various mutable methods for accessing elements.
26pub trait RawSpaceMut: RawSpace {
27    /// returns a mutable pointer to the element currently within scope
28    fn as_ptr_mut(&mut self) -> *mut Self::Elem;
29}
30
31/// [`SliceSpace`] is used to define sequential collections, spaces, or containers that can be
32/// viewed as slices.
33pub trait SliceSpace: RawSpaceRef {
34    fn as_slice(&self) -> &[Self::Elem];
35
36    fn len(&self) -> usize {
37        self.as_slice().len()
38    }
39
40    fn is_empty(&self) -> bool {
41        self.len() == 0
42    }
43}
44/// [`SliceSpaceMut`] is used to define sequential collections, spaces, or containers that can be
45pub trait SliceSpaceMut: SliceSpace + RawSpaceMut {
46    /// returns a mutable slice of the elements
47    fn as_mut_slice(&mut self) -> &mut [Self::Elem];
48}
49
50/*
51 ************* Implementations *************
52*/
53
54impl<T> ScalarSpace for T
55where
56    T: RawSpace<Elem = Self>,
57{
58    seal! {}
59}
60
61impl<C, T> RawSpace for &C
62where
63    C: RawSpace<Elem = T>,
64{
65    type Elem = C::Elem;
66}
67
68impl<C, T> RawSpace for &mut C
69where
70    C: RawSpace<Elem = T>,
71{
72    type Elem = C::Elem;
73}
74
75impl<U, T> RawSpaceRef for &U
76where
77    U: RawSpaceRef<Elem = T>,
78{
79    fn as_ptr(&self) -> *const Self::Elem {
80        U::as_ptr(*self)
81    }
82}
83
84impl<U, T> RawSpaceRef for &mut U
85where
86    U: RawSpaceRef<Elem = T>,
87{
88    fn as_ptr(&self) -> *const Self::Elem {
89        U::as_ptr(*self)
90    }
91}