1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use crate::ImpVec;
use orx_pinned_vec::{
    self_referential_elements::{SelfRefNext, SelfRefPrev},
    PinnedVec,
};

impl<'a, T, P> ImpVec<T, P>
where
    P: PinnedVec<T> + 'a,
    T: SelfRefNext<'a> + 'a,
{
    /// Using interior mutability peforms the following:
    ///
    /// * `element.set_next(next)`
    ///
    /// so that `element` will point to the `next` element after the operation.
    ///
    /// # Panics
    ///
    /// Panics:
    ///
    /// * if `element` does not belong to this vector, or
    /// * if `next` is of Some variant and underlying reference does not belong to this vector.
    ///
    /// # Safety
    ///
    /// Due to the guards defined in the Panics section, `element` and `next` (if some) belong to the same vector living together and sharing the same lifetime.
    ///
    /// # Example
    ///
    /// This is one of the specialized methods for building self-referential-collections when the elements implement `orx_pinned_vec::SelfRefNext`.
    ///
    /// It is not trivial to provide a partial and trivial example.
    /// However, the usefulness of this method can be demonstrated by its usage within the [`orx_linked_list::LinkedList`](https://crates.io/crates/orx-linked-list) implementation.
    ///
    /// The crate defines a double-linked-list `Node` with optional `next` and `prev` references.
    /// `Node` implements `orx_pinned_vec::SelfRefNext` and `orx_pinned_vec::SelfRefPrev`; therefore, we can use `ImpVec::set_next` method.
    /// The following code block is the `push_back` method implementation of the `LinkedList`:
    ///
    /// ```rust ignore
    /// pub fn push_back(&mut self, value: T) {
    ///     match self.back_node() {
    ///         None => self.push_first_node(value),
    ///         Some(old_back) => {
    ///             let node = Node::active(value, Some(old_back), None);
    ///             let back = unsafe { self.vec.push_get_ref(node) };
    ///             self.vec.set_next(old_back, Some(back));
    ///             self.slice = LinkedListSlice::new(self.len() + 1, self.front_node(), Some(back));
    ///         }
    ///     }
    /// }
    /// ```
    ///
    /// In the trivial case when there is no `back_node` (the list is empty), we simply push the `value` as the first node.
    ///
    /// Otherwise:
    /// * We get a reference to the back node, we'll now call it `old_back` since it will no longer be the back of the list.
    /// * We create a new `node` for the new `value`:
    ///   * previous node of `node` is set to `Some(old_back)`,
    ///   * next node of `node` is `None` since it is the new back of the list.
    /// * We use the `push_get_ref` method to push `node` to the storage vector and get a reference to it.
    ///   * SAFETY: we must make sure that the reference `back` does not outlive `self`; which is satisfied here.
    /// * We tell our `self.vec` which is an `ImpVec` to set the next of `old_back` to `Some(back)` to link the old back to the new back.
    /// * Then, we set that our list now is one element longer, with the old `self.front_node()` and new back node which is `Some(back)`.
    pub fn set_next(&mut self, element: &T, next: Option<&'a T>) {
        if let Some(next) = next {
            self.index_of(next).expect(INVALID_NEXT);
        }

        let node = self
            .index_of(element)
            .and_then(|idx| unsafe { self.get_mut(idx) })
            .expect(INVALID_ELEM);

        node.set_next(next)
    }
}

impl<'a, T, P> ImpVec<T, P>
where
    P: PinnedVec<T> + 'a,
    T: SelfRefPrev<'a> + 'a,
{
    /// Using interior mutability peforms the following:
    ///
    /// * `element.set_prev(prev)`
    ///
    /// so that `element` will point to the `prev` element after the operation.
    ///
    /// # Panics
    ///
    /// Panics:
    ///
    /// * if `element` does not belong to this vector, or
    /// * if `prev` is of Some variant and underlying reference does not belong to this vector.
    ///
    /// # Safety
    ///
    /// Due to the guards defined in the Panics section, `element` and `prev` (if some) belong to the same vector living together and sharing the same lifetime.
    ///
    /// # Example
    ///
    /// This is one of the specialized methods for building self-referential-collections when the elements implement `orx_pinned_vec::SelfRefNext`.
    ///
    /// It is not trivial to provide a partial and trivial example.
    /// However, the usefulness of this method can be demonstrated by its usage within the [`orx_linked_list::LinkedList`](https://crates.io/crates/orx-linked-list) implementation.
    ///
    /// The crate defines a double-linked-list `Node` with optional `next` and `prev` references.
    /// `Node` implements `orx_pinned_vec::SelfRefNext` and `orx_pinned_vec::SelfRefPrev`; therefore, we can use `ImpVec::set_next` method.
    /// The following code block is the `push_front` method implementation of the `LinkedList`:
    ///
    /// ```rust ignore
    /// pub fn push_front(&mut self, value: T) {
    ///     match self.front_node() {
    ///         None => self.push_first_node(value),
    ///         Some(old_front) => {
    ///             let node = Node::active(value, None, Some(old_front));
    ///             let front = unsafe { self.vec.push_get_ref(node) };
    ///             self.vec.set_prev(old_front, Some(front));
    ///             self.slice = LinkedListSlice::new(self.len() + 1, Some(front), self.back_node());
    ///         }
    ///     }
    /// }
    /// ```
    ///
    /// In the trivial case when there is no `front_node` (the list is empty), we simply push the `value` as the first node.
    ///
    /// Otherwise:
    /// * We get a reference to the front node, we'll now call it `old_front` since it will no longer be the front of the list.
    /// * We create a new `node` for the new `value`:
    ///   * next node of `node` is set to `Some(old_front)`,
    ///   * previous node of `node` is `None` since it is the new front of the list.
    /// * We use the `push_get_ref` method to push `node` to the storage vector and get a reference to it.
    ///   * SAFETY: we must make sure that the reference `front` does not outlive `self`, which is satisfied here.
    /// * We tell our `self.vec` which is an `ImpVec` to set the prev of `old_front` to `Some(front)` to link the old front node to the new front ndoe.
    /// * Then, we set that our list now is one element longer, with the old `self.back_node()` and new front node which is `Some(front)`.
    pub fn set_prev(&mut self, element: &T, prev: Option<&'a T>) {
        if let Some(prev) = prev {
            self.index_of(prev).expect(INVALID_PREV);
        }

        let node = self
            .index_of(element)
            .and_then(|idx| unsafe { self.get_mut(idx) })
            .expect(INVALID_ELEM);

        node.set_prev(prev)
    }
}

const INVALID_ELEM: &str = "element does not belong to this vector";
const INVALID_NEXT: &str = "next does not belong to this vector; ImpVec allows inter-element references only if both elements belong to the same ImpVec";
const INVALID_PREV: &str = "prev does not belong to this vector; ImpVec allows inter-element references only if both elements belong to the same ImpVec";

#[cfg(test)]
mod tests {
    use crate::{prelude::*, test_all_pinned_types};

    struct Node<'a> {
        data: char,
        prev: Option<&'a Self>,
        next: Option<&'a Self>,
    }

    impl<'a> SelfRefNext<'a> for Node<'a> {
        #[inline(always)]
        fn next(&self) -> Option<&'a Self> {
            self.next
        }
        #[inline(always)]
        fn set_next(&mut self, next: Option<&'a Self>) {
            self.next = next;
        }
    }

    impl<'a> SelfRefPrev<'a> for Node<'a> {
        #[inline(always)]
        fn prev(&self) -> Option<&'a Self> {
            self.prev
        }
        #[inline(always)]
        fn set_prev(&mut self, prev: Option<&'a Self>) {
            self.prev = prev;
        }
    }

    #[test]
    fn set_next_prev() {
        fn test<'a, P: PinnedVec<Node<'a>> + 'a>(pinned_vec: P) {
            let mut imp: ImpVec<_, _> = pinned_vec.into();

            let node = Node {
                data: 'a',
                prev: None,
                next: None,
            };
            let a = unsafe { imp.push_get_ref(node) };

            let node = Node {
                data: 'b',
                prev: None,
                next: None,
            };
            let b = unsafe { imp.push_get_ref(node) };

            for x in imp.iter() {
                assert!(x.next().is_none());
                assert!(x.prev().is_none());
            }

            imp.set_next(a, Some(b));
            imp.set_prev(b, Some(a));

            assert_eq!(a.next().map(|x| &x.data), Some(&'b'));
            assert_eq!(b.prev().map(|x| &x.data), Some(&'a'));

            imp.set_next(a, None);
            imp.set_prev(b, None);

            for x in imp.iter() {
                assert!(x.next().is_none());
                assert!(x.prev().is_none());
            }
        }

        test_all_pinned_types!(test);
    }

    #[test]
    #[should_panic]
    fn set_next_of_another_impvec() {
        let mut imp1: ImpVec<_> = Default::default();
        let mut imp2: ImpVec<_> = Default::default();

        let node = Node {
            data: 'a',
            prev: None,
            next: None,
        };
        let a = unsafe { imp1.push_get_ref(node) };

        let node = Node {
            data: 'b',
            prev: None,
            next: None,
        };
        let b = unsafe { imp2.push_get_ref(node) };

        imp2.set_next(a, Some(b));
    }

    #[test]
    #[should_panic]
    fn set_next_to_another_impvec() {
        let mut imp1: ImpVec<_> = Default::default();
        let mut imp2: ImpVec<_> = Default::default();

        let node = Node {
            data: 'a',
            prev: None,
            next: None,
        };
        let a = unsafe { imp1.push_get_ref(node) };

        let node = Node {
            data: 'b',
            prev: None,
            next: None,
        };
        let b = unsafe { imp2.push_get_ref(node) };

        imp1.set_next(a, Some(b));
    }

    #[test]
    #[should_panic]
    fn set_prev_to_another_impvec() {
        let mut imp1: ImpVec<_> = Default::default();
        let mut imp2: ImpVec<_> = Default::default();

        let node = Node {
            data: 'a',
            prev: None,
            next: None,
        };
        let a = unsafe { imp1.push_get_ref(node) };

        let node = Node {
            data: 'b',
            prev: None,
            next: None,
        };
        let b = unsafe { imp2.push_get_ref(node) };

        imp1.set_prev(a, Some(b));
    }

    #[test]
    #[should_panic]
    fn set_prev_of_another_impvec() {
        let mut imp1: ImpVec<_> = Default::default();
        let mut imp2: ImpVec<_> = Default::default();

        let node = Node {
            data: 'a',
            prev: None,
            next: None,
        };
        let a = unsafe { imp1.push_get_ref(node) };

        let node = Node {
            data: 'b',
            prev: None,
            next: None,
        };
        let b = unsafe { imp2.push_get_ref(node) };

        imp2.set_prev(a, Some(b));
    }
}