orx_linked_list/list/common_traits/
index.rs

1use crate::{
2    DoublyEnds, DoublyEndsMut, DoublyIdx, List, ListSlice, ListSliceMut, Singly, SinglyEnds,
3    SinglyEndsMut, SinglyIdx, type_aliases::OOB, variant::Doubly,
4};
5use core::ops::{Index, IndexMut};
6use orx_pinned_vec::PinnedVec;
7use orx_selfref_col::{MemoryPolicy, Node};
8
9// doubly
10
11impl<'i, T, M, P> Index<&'i DoublyIdx<T>> for List<Doubly<T>, M, P>
12where
13    M: MemoryPolicy<Doubly<T>>,
14    P: PinnedVec<Node<Doubly<T>>>,
15{
16    type Output = T;
17
18    /// Returns the element at the given `index`.
19    ///
20    /// # Panics
21    ///
22    /// Panics if the `index` is invalid; i.e.,
23    /// * `list.is_valid(index)` returns false, equivalently,
24    /// * `list.idx_err(index)` returns the detail of the error.
25    fn index(&self, index: &'i DoublyIdx<T>) -> &Self::Output {
26        self.get(index).expect(OOB)
27    }
28}
29
30impl<'i, T, M, P> Index<&'i DoublyIdx<T>> for ListSlice<'_, Doubly<T>, M, P>
31where
32    M: MemoryPolicy<Doubly<T>>,
33    P: PinnedVec<Node<Doubly<T>>>,
34{
35    type Output = T;
36
37    /// Returns the element at the given `index`.
38    ///
39    /// # Panics
40    ///
41    /// Panics if the `index` is invalid; i.e.,
42    /// * `list.is_valid(index)` returns false, equivalently,
43    /// * `list.idx_err(index)` returns the detail of the error.
44    fn index(&self, index: &'i DoublyIdx<T>) -> &Self::Output {
45        self.get(index).expect(OOB)
46    }
47}
48
49impl<'i, T, M, P> Index<&'i DoublyIdx<T>> for ListSliceMut<'_, Doubly<T>, M, P>
50where
51    M: MemoryPolicy<Doubly<T>>,
52    P: PinnedVec<Node<Doubly<T>>>,
53{
54    type Output = T;
55
56    /// Returns the element at the given `index`.
57    ///
58    /// # Panics
59    ///
60    /// Panics if the `index` is invalid; i.e.,
61    /// * `list.is_valid(index)` returns false, equivalently,
62    /// * `list.idx_err(index)` returns the detail of the error.
63    fn index(&self, index: &'i DoublyIdx<T>) -> &Self::Output {
64        self.get(index).expect(OOB)
65    }
66}
67
68impl<'i, T, M, P> IndexMut<&'i DoublyIdx<T>> for List<Doubly<T>, M, P>
69where
70    M: MemoryPolicy<Doubly<T>>,
71    P: PinnedVec<Node<Doubly<T>>>,
72{
73    /// Returns a mutable reference to the element at the given `index`.
74    ///
75    /// # Panics
76    ///
77    /// Panics if the `index` is invalid; i.e.,
78    /// * `list.is_valid(index)` returns false, equivalently,
79    /// * `list.idx_err(index)` returns the detail of the error.
80    fn index_mut(&mut self, index: &'i DoublyIdx<T>) -> &mut Self::Output {
81        self.get_mut(index).expect(OOB)
82    }
83}
84
85impl<'i, T, M, P> IndexMut<&'i DoublyIdx<T>> for ListSliceMut<'_, Doubly<T>, M, P>
86where
87    M: MemoryPolicy<Doubly<T>>,
88    P: PinnedVec<Node<Doubly<T>>>,
89{
90    /// Returns a mutable reference to the element at the given `index`.
91    ///
92    /// # Panics
93    ///
94    /// Panics if the `index` is invalid; i.e.,
95    /// * `list.is_valid(index)` returns false, equivalently,
96    /// * `list.idx_err(index)` returns the detail of the error.
97    fn index_mut(&mut self, index: &'i DoublyIdx<T>) -> &mut Self::Output {
98        self.get_mut(index).expect(OOB)
99    }
100}
101
102// singly
103
104impl<'i, T, M, P> Index<&'i SinglyIdx<T>> for List<Singly<T>, M, P>
105where
106    M: MemoryPolicy<Singly<T>>,
107    P: PinnedVec<Node<Singly<T>>>,
108{
109    type Output = T;
110
111    /// Returns the element at the given `index`.
112    ///
113    /// # Panics
114    ///
115    /// Panics if the `index` is invalid; i.e.,
116    /// * `list.is_valid(index)` returns false, equivalently,
117    /// * `list.idx_err(index)` returns the detail of the error.
118    fn index(&self, index: &'i SinglyIdx<T>) -> &Self::Output {
119        self.get(index).expect(OOB)
120    }
121}
122
123impl<'i, T, M, P> Index<&'i SinglyIdx<T>> for ListSlice<'_, Singly<T>, M, P>
124where
125    M: MemoryPolicy<Singly<T>>,
126    P: PinnedVec<Node<Singly<T>>>,
127{
128    type Output = T;
129
130    /// Returns the element at the given `index`.
131    ///
132    /// # Panics
133    ///
134    /// Panics if the `index` is invalid; i.e.,
135    /// * `list.is_valid(index)` returns false, equivalently,
136    /// * `list.idx_err(index)` returns the detail of the error.
137    fn index(&self, index: &'i SinglyIdx<T>) -> &Self::Output {
138        self.get(index).expect(OOB)
139    }
140}
141
142impl<'i, T, M, P> Index<&'i SinglyIdx<T>> for ListSliceMut<'_, Singly<T>, M, P>
143where
144    M: MemoryPolicy<Singly<T>>,
145    P: PinnedVec<Node<Singly<T>>>,
146{
147    type Output = T;
148
149    /// Returns the element at the given `index`.
150    ///
151    /// # Panics
152    ///
153    /// Panics if the `index` is invalid; i.e.,
154    /// * `list.is_valid(index)` returns false, equivalently,
155    /// * `list.idx_err(index)` returns the detail of the error.
156    fn index(&self, index: &'i SinglyIdx<T>) -> &Self::Output {
157        self.get(index).expect(OOB)
158    }
159}
160
161impl<'i, T, M, P> IndexMut<&'i SinglyIdx<T>> for List<Singly<T>, M, P>
162where
163    M: MemoryPolicy<Singly<T>>,
164    P: PinnedVec<Node<Singly<T>>>,
165{
166    /// Returns a mutable reference to the element at the given `index`.
167    ///
168    /// # Panics
169    ///
170    /// Panics if the `index` is invalid; i.e.,
171    /// * `list.is_valid(index)` returns false, equivalently,
172    /// * `list.idx_err(index)` returns the detail of the error.
173    fn index_mut(&mut self, index: &'i SinglyIdx<T>) -> &mut Self::Output {
174        self.get_mut(index).expect(OOB)
175    }
176}
177
178impl<'i, T, M, P> IndexMut<&'i SinglyIdx<T>> for ListSliceMut<'_, Singly<T>, M, P>
179where
180    M: MemoryPolicy<Singly<T>>,
181    P: PinnedVec<Node<Singly<T>>>,
182{
183    /// Returns a mutable reference to the element at the given `index`.
184    ///
185    /// # Panics
186    ///
187    /// Panics if the `index` is invalid; i.e.,
188    /// * `list.is_valid(index)` returns false, equivalently,
189    /// * `list.idx_err(index)` returns the detail of the error.
190    fn index_mut(&mut self, index: &'i SinglyIdx<T>) -> &mut Self::Output {
191        self.get_mut(index).expect(OOB)
192    }
193}