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
16/// [`RawSpaceMut`] is a trait that provides various mutable methods for accessing elements.
17pub trait RawSpaceMut: RawSpace {}
18
19/// [`RawSpaceRef`] is a trait that provides various read-only methods for accessing elements.
20pub trait RawSpaceRef: RawSpace {}
21/// [`SliceSpace`] is used to define sequential collections, spaces, or containers that can be
22/// viewed as slices.
23pub trait SliceSpace: RawSpace {
24 fn as_slice(&self) -> &[Self::Elem];
25 /// returns a raw pointer to the underlying elements
26 fn as_ptr(&self) -> *const Self::Elem {
27 self.as_slice().as_ptr()
28 }
29
30 fn len(&self) -> usize {
31 self.as_slice().len()
32 }
33}
34
35pub trait SliceSpaceMut: SliceSpace {
36 fn as_mut_slice(&mut self) -> &mut [Self::Elem];
37
38 fn as_mut_ptr(&mut self) -> *mut Self::Elem {
39 self.as_mut_slice().as_mut_ptr()
40 }
41}
42
43/*
44 ************* Implementations *************
45*/
46impl<C, T> RawSpace for &C
47where
48 C: RawSpace<Elem = T>,
49{
50 type Elem = C::Elem;
51}
52
53impl<C, T> RawSpace for &mut C
54where
55 C: RawSpace<Elem = T>,
56{
57 type Elem = C::Elem;
58}