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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Doubly-linked list.
//!
//! The goal here is to create a fast and efficient linked list.
//! Lists use an array of nodes as memory pool, the array must be static.
//!
//! In contrast to [`std::collections::LinkedList`](https://doc.rust-lang.org/alloc/collections/linked_list/struct.LinkedList.html)
//! you will only ever get a reference to a node and never a copy/move.
//!
//! # Atomicity
//! In an attempt to reduce interrupt latency and with multicore systems in
//! mind, the linked list uses atomic operations. However, these are not safe
//! yet. Use a critical section when accessing the linked list.

#![allow(unused)]

use core::{mem, ptr};
use core::ptr::{NonNull};
use core::mem::MaybeUninit;
use core::cell::RefCell;
use core::borrow::BorrowMut;
use crate::mem::boxed::Box;
use core::ops::{Deref, DerefMut};
use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use core::marker::PhantomData;
use crate::alloc::allocator::{Allocator, AllocError};

/******************************************************************************/

type Link<T> = AtomicPtr<Node<T>>;

/// An element/node of a list.
// Copy needed for initialization
#[derive(Debug)]
pub struct Node<T> {
    inner: T,
    prev: Link<T>,
    next: Link<T>,
}

impl<T> Node<T> {
    /// Create a node from an element
    pub const fn new(element: T) -> Self {
        Node {
            inner: element,
            prev: AtomicPtr::new(ptr::null_mut()),
            next: AtomicPtr::new(ptr::null_mut()),
        }
    }
}

impl<T> Deref for Node<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<T> DerefMut for Node<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

/******************************************************************************/

/// A doubly-linked list owning its nodes.
///
/// Based on [std::collections::LinkedList](https://doc.rust-lang.org/alloc/collections/linked_list/struct.LinkedList.html)
/// and <https://rust-unofficial.github.io/too-many-lists>.
///
/// # Examples
///
/// Create a new list:
/// ```ignore
/// let mut list_a = LinkedList::new();
/// let mut list_b = LinkedList::new();
/// ```
///
/// Add element to the end of a list with an allocator:
/// ```ignore
/// static ALLOCATOR: Bump = unsafe { Bump::new(NonNull::new_unchecked(0x2001E000 as *mut u8), 5_000) };
/// list_a.emplace_back(MyStruct { id: 42 }, &ALLOCATOR);
/// list_a.emplace_back(MyStruct { id: 54 }, &ALLOCATOR);
///```
/// Nodes in the same list can be allocated in different memory sections.
///
/// Move an element from one to another list:
/// ```ignore
/// let node = list_a.pop_front();
/// list_a.push_back(node);
///```
#[derive(Debug)]
pub struct LinkedList<T> {
    head: Link<T>,
    tail: Link<T>,
    len: AtomicUsize,
}

impl<T> LinkedList<T> {
    /// Create an empty list
    pub const fn new() -> Self {
        LinkedList {
            head: AtomicPtr::new(ptr::null_mut()),
            tail: AtomicPtr::new(ptr::null_mut()),
            len: AtomicUsize::new(0),
        }
    }

    /// Allocate a new element and move it to the end of the list
    ///
    /// **Note:** This fails when we're out of memory
    pub fn emplace_back(&self, element: T, alloc: &'static dyn Allocator) -> Result<(), AllocError> {
        let node = Box::try_new_in(Node::new(element), alloc);
        // Note(unsafe): map() is only called if the pointer is non-null.
        node.map(|mut n| unsafe {
            self.push_back(n);
        })
    }

    /// Insert a node at the end on the list
    pub fn push_back(&self, mut node: Box<Node<T>>) {
        let mut node_raw = Box::leak(node);
        let mut tail = self.tail.load(Ordering::Acquire);

        // Note(unsafe): Pointer requirements are met.
        unsafe {
            (*node_raw.as_ref()).prev.store(tail, Ordering::Relaxed);
            (*node_raw.as_ref()).next.store(ptr::null_mut(), Ordering::Relaxed);

            match tail.as_mut() {
                None => self.head.store(node_raw.as_ptr(), Ordering::Relaxed),
                Some(tail) => (*tail).next.store(node_raw.as_ptr(), Ordering::Relaxed),
            };
        }

        self.tail.store(node_raw.as_ptr(), Ordering::Release);
        self.len.fetch_add(1, Ordering::Relaxed);
    }

    /// Remove and return the first node from the list if there is any
    pub fn pop_front(&self) -> Option<Box<Node<T>>> {
        // Note(unsafe): Pointer requirements are met.
        unsafe {
            self.head.load(Ordering::Relaxed).as_mut().map(|node| {
                let next = (*node).next.load(Ordering::Relaxed);
                self.head.store(next, Ordering::Relaxed);

                if let Some(head) = next.as_mut() {
                    (*head).prev.store(ptr::null_mut(), Ordering::Relaxed);
                }

                if self.tail.load(Ordering::Acquire) == node {
                    self.tail.store(next, Ordering::Release);
                }

                (*node).next.store(ptr::null_mut(), Ordering::Relaxed);
                (*node).prev.store(ptr::null_mut(), Ordering::Relaxed);
                self.len.fetch_sub(1, Ordering::Relaxed);
                Box::from_raw(NonNull::new_unchecked(node))
            })
        }
    }

    /// Insert a node exactly before a given node
    ///
    /// **Note:** prefer [`Self::insert_when()`] if possible
    pub fn insert(&self, node: NonNull<Node<T>>, mut new_node: Box<Node<T>>) {
        let node_ptr = node;
        let new_node_ptr = Box::leak(new_node);

        // Note(unsafe): Pointer requirements are met.
        unsafe {
            match (*node_ptr.as_ref()).prev.load(Ordering::Acquire).as_mut() {
                None => {
                    self.head.store(new_node_ptr.as_ptr(), Ordering::Relaxed);
                    (*new_node_ptr.as_ref()).prev.store(ptr::null_mut(), Ordering::Relaxed);
                },
                Some(prev) => {
                    (*prev).next.store(new_node_ptr.as_ptr(), Ordering::Relaxed);
                    (*new_node_ptr.as_ref()).prev.store(prev, Ordering::Relaxed);
                },
            }

            (*node_ptr.as_ref()).prev.store(new_node_ptr.as_ptr(), Ordering::Release);
            (*new_node_ptr.as_ref()).next.store(node_ptr.as_ptr(), Ordering::Relaxed);
        }

        self.len.fetch_add(1, Ordering::Relaxed);
    }

    /// Insert a node before the first succeeding match given a comparison criteria.
    ///
    /// # Example
    /// Insert task `pausing` before the element where the next wake-up time
    /// `next_wut()` is larger than the one of `pausing`.
    /// ```ignore
    /// /* create and populate list */
    /// let pausing: Task = /* omitted */;
    /// tasks_sleeping.insert_when(
    ///     pausing,
    ///     |pausing, task| {
    ///         pausing.next_wut() > task.next_wut()
    ///     });
    /// ```
    pub fn insert_when(&self, mut node: Box<Node<T>>, criteria: impl Fn(&T, &T) -> bool) {
        // Note(unsafe): Pointer requirements are met.
        let mut current = unsafe { self.head.load(Ordering::Relaxed).as_mut() };
        if let Some(mut current) = current {
            loop {
                // Note(unsafe): current is checked to be non-null above.
                unsafe {
                    if criteria(&(*node).inner, &current.inner) {
                        self.insert(NonNull::new_unchecked(current), node);
                        return;
                    }

                    current = match current.next.load(Ordering::Relaxed).as_mut() {
                        None => break,
                        Some(next) => next,
                    };
                }
            }
        }
        self.push_back(node);
    }

    /// Get a reference to the first value of the list if there is a node
    pub fn front(&self) -> Option<&T> {
        // Note(unsafe): Pointer requirements are met.
        unsafe {
            self.head.load(Ordering::Relaxed).as_mut().map(|head|
                &(*head).inner
            )
        }
    }

    /// Get a reference to last value of the list if there is a node
    pub fn back(&self) -> Option<&T> {
        // Note(unsafe): Pointer requirements are met.
        unsafe {
            self.tail.load(Ordering::Relaxed).as_mut().map(|tail|
                &(*tail).inner
            )
        }
    }

    /// Get the current length of the list
    pub fn len(&self) -> usize {
        self.len.load(Ordering::Relaxed)
    }


    /// Remove a node from any point in the list.
    ///
    /// # Safety
    /// A node is only allowed to be unliked once.
    unsafe fn unlink(&self, node: Box<Node<T>>) -> Box<Node<T>> {
        self.unlink_raw(Box::leak(node))
    }

    /// Remove a node from any point in the list.
    ///
    /// # Safety
    /// - A node is only allowed to be unliked once.
    /// - Must unlinked in the correct list.
    unsafe fn unlink_raw(&self, mut node: NonNull<Node<T>>) -> Box<Node<T>> {
        let prev = (*node.as_mut()).prev.load(Ordering::Relaxed);
        let next = (*node.as_mut()).next.load(Ordering::Relaxed);

        match prev.as_mut() {
            None => self.head.store(next, Ordering::Relaxed),
            Some(prev) => prev.next.store(next, Ordering::Relaxed),
        };

        match next.as_mut() {
            None => self.tail.store(prev, Ordering::Relaxed),
            Some(next) => next.prev.store(prev, Ordering::Relaxed),
        };

        (*node.as_mut()).prev.store(ptr::null_mut(), Ordering::Relaxed);
        (*node.as_mut()).next.store(ptr::null_mut(), Ordering::Relaxed);
        self.len.fetch_sub(1, Ordering::Relaxed);

        Box::from_raw(node)
    }

    /// Provides a forward iterator.
    pub fn iter(&self) -> Iter<'_, T> {
        // Note(unsafe): Pointer requirements are met.
        let next = unsafe {
            self.head.load(Ordering::Relaxed).as_ref()
        };
        Iter { next }
    }

    /// Provides a forward iterator with mutable references.
    pub fn iter_mut(&self) -> IterMut<'_, T> {
        // Note(unsafe): Pointer requirements are met.
        let next = unsafe {
            self.head.load(Ordering::Relaxed).as_mut()
        };
        IterMut { next }
    }

    /// Provides a cursor with editing operation at the front element.
    pub fn cursor_front_mut(&self) -> Cursor<'_, T> {
        Cursor { node: self.head.load(Ordering::Relaxed), list: self }
    }
}

/******************************************************************************/

/// An iterator over the elements of a [`LinkedList`].
///
/// This `struct` is created by [`LinkedList::iter()`].
pub struct Iter<'a, T> {
    next: Option<&'a Node<T>>,
}

impl<'a,T> Iterator for Iter<'a, T>
{
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        self.next.map(|node| unsafe {
            // Note(unsafe): Pointer requirements are met.
            self.next = unsafe {
                (*node).next.load(Ordering::Relaxed).as_ref()
            };
            &(*node).inner
        })
    }
}

/// An mutable iterator over the elements of a [`LinkedList`].
///
/// This `struct` is created by [`LinkedList::iter_mut()`].
pub struct IterMut<'a, T> {
    next: Option<&'a mut Node<T>>,
}

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;

    fn next(&mut self) -> Option<Self::Item> {
        self.next.take().map(|node| unsafe {
            // Note(unsafe): Pointer requirements are met.
            self.next = unsafe {
                (*node).next.load(Ordering::Relaxed).as_mut()
            };
            &mut (*node).inner
        })
    }
}

/******************************************************************************/

/// A cursor over a [`LinkedList`] with editing operations.
///
/// In contrast to an iterator a cursor can move from front to back and take an
/// element out of the list.
#[derive(Debug)]
pub struct Cursor<'a,T> {
    node: *mut Node<T>,
    list: &'a LinkedList<T>,
}

impl<'a, T> Cursor<'a, T> {
    /// Get reference to value of node if there is any
    pub fn inner(&self) -> Option<&T> {
        // Note(unsafe): Pointer requirements are met.
        unsafe {
            self.node.as_ref().map(|node|
                &(*node).inner
            )
        }
    }

    /// Get mutable reference to value of node if there is any
    pub fn inner_mut(&self) -> Option<&mut T> {
        // Note(unsafe): Pointer requirements are met.
        unsafe {
            self.node.as_mut().map(|node|
                &mut (*node).inner
            )
        }
    }

    /// Get raw pointer of node. Only use if you really have to.
    pub(crate) unsafe fn node(&self) -> *mut Node<T> {
        self.node
    }

    /// Move cursor to the next node
    pub fn move_next(&mut self) {
        // Note(unsafe): Pointer requirements are met.
        unsafe {
            if let Some(node) = self.node.as_mut() {
                self.node = (*node).next.load(Ordering::Relaxed);
            }
        }
    }

    /// Take the current node if there is one. Also moves the cursor before
    /// removing a node.
    pub fn take(&mut self) -> Option<Box<Node<T>>> {
        let node = self.node;
        self.move_next();
        // Note(unsafe): Node is checked be non-null.
        unsafe {
            node.as_mut().map(move |node|
                self.list.unlink_raw(NonNull::new_unchecked(node))
            )
        }
    }
}

/******************************************************************************/

#[cfg(all(test, not(target_os = "none")))]
mod tests {
    use super::*;
    use core::borrow::Borrow;
    use std::mem::size_of;
    use crate::alloc::bump::Bump;

    #[derive(Debug, Copy, Clone)]
    struct MyStruct {
        pub id: u32,
    }

    #[test]
    fn one_node() {
        static mut BUFFER: [u8; 128] = [0; 128];
        static ALLOCATOR: Bump = unsafe {
            Bump::new(
                NonNull::new_unchecked(BUFFER.as_ptr() as *mut _),
                NonNull::new_unchecked(BUFFER.as_ptr().add(BUFFER.len()) as *mut _)
            )};

        let mut list = LinkedList::new();
        assert_eq!(list.head.load(Ordering::Relaxed), ptr::null_mut());
        assert_eq!(list.tail.load(Ordering::Relaxed), ptr::null_mut());

        list.emplace_back(MyStruct { id: 42 }, &ALLOCATOR);
        let head = list.head.load(Ordering::Relaxed);
        let tail = list.tail.load(Ordering::Relaxed);
        assert_ne!(head, ptr::null_mut());
        assert_eq!(tail, head);
        unsafe {
            assert_eq!((*head).prev.load(Ordering::Relaxed), ptr::null_mut());
            assert_eq!((*tail).prev.load(Ordering::Relaxed), ptr::null_mut());
        }

        let node = list.pop_front().unwrap();
        let head = list.head.load(Ordering::Relaxed);
        let tail = list.tail.load(Ordering::Relaxed);
        assert_eq!(head, ptr::null_mut());
        assert_eq!(tail, ptr::null_mut());
        unsafe {
            assert_eq!((*node).prev.load(Ordering::Relaxed), ptr::null_mut());
            assert_eq!((*node).next.load(Ordering::Relaxed), ptr::null_mut());
        }

        list.push_back(node);
    }

    #[test]
    fn length() {
        static mut BUFFER: [u8; 128] = [0; 128];
        static ALLOCATOR: Bump = unsafe {
            Bump::new(
                NonNull::new_unchecked(BUFFER.as_ptr() as *mut _),
                NonNull::new_unchecked(BUFFER.as_ptr().add(BUFFER.len()) as *mut _)
            )};

        let mut list = LinkedList::new();
        assert_eq!(list.len(), 0);
        let node = list.pop_front();
        assert!(node.is_none());
        assert_eq!(list.len(), 0);
        list.emplace_back(MyStruct { id: 42 }, &ALLOCATOR);
        assert_eq!(list.len(), 1);
        Box::leak(list.pop_front().unwrap());
        assert_eq!(list.len(), 0);
    }

    #[test]
    fn pushing_and_popping() {
        static mut BUFFER: [u8; 128] = [0; 128];
        static ALLOCATOR: Bump = unsafe {
            Bump::new(
                NonNull::new_unchecked(BUFFER.as_ptr() as *mut _),
                NonNull::new_unchecked(BUFFER.as_ptr().add(BUFFER.len()) as *mut _)
            )};

        let mut list = LinkedList::new();
        list.emplace_back(MyStruct { id: 42 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 43 }, &ALLOCATOR);

        let mut another_list = LinkedList::new();
        list.emplace_back(MyStruct { id: 44 }, &ALLOCATOR);

        let mut front = list.pop_front().unwrap();
        assert_eq!((*front).id, 42);
        another_list.push_back(front);

        assert_eq!(another_list.back().unwrap().id, 42);
    }

    #[test]
    fn memory_overflow() {
        const ELEMENT_LEN: usize = size_of::<Node<MyStruct>>();
        // Add some extra space for alignment
        static mut BUFFER: [u8; ELEMENT_LEN*16 + 8] = [0; ELEMENT_LEN*16 + 8];
        static ALLOCATOR: Bump = unsafe {
            Bump::new(
                NonNull::new_unchecked(BUFFER.as_ptr() as *mut _),
                NonNull::new_unchecked(BUFFER.as_ptr().add(BUFFER.len()) as *mut _)
            )};

        let mut list = LinkedList::new();
        for i in 0..16 {
            assert!(list.emplace_back(MyStruct { id: i }, &ALLOCATOR).is_ok());
        }
        assert!(list.emplace_back(MyStruct { id: 16 }, &ALLOCATOR).is_err());
    }


    #[test]
    fn iterate() {
        static mut BUFFER: [u8; 128] = [0; 128];
        static ALLOCATOR: Bump = unsafe {
            Bump::new(
                NonNull::new_unchecked(BUFFER.as_ptr() as *mut _),
                NonNull::new_unchecked(BUFFER.as_ptr().add(BUFFER.len()) as *mut _)
            )};

        let mut list = LinkedList::new();
        list.emplace_back(MyStruct { id: 42 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 43 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 44 }, &ALLOCATOR);

        let truth = vec![42,43,44,45];
        for (i, element) in list.iter().enumerate() {
            assert_eq!(element.id, truth[i]);
        }
        // everything should still work fine
        for (i, element) in list.iter().enumerate() {
            assert_eq!(element.id, truth[i]);
        }
    }

    #[test]
    fn iterate_mut() {
        static mut BUFFER: [u8; 128] = [0; 128];
        static ALLOCATOR: Bump = unsafe {
            Bump::new(
                NonNull::new_unchecked(BUFFER.as_ptr() as *mut _),
                NonNull::new_unchecked(BUFFER.as_ptr().add(BUFFER.len()) as *mut _)
            )};

        let mut list = LinkedList::new();
        list.emplace_back(MyStruct { id: 42 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 43 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 44 }, &ALLOCATOR);

        let truth = vec![42,43,44,45];
        for (i, element) in list.iter_mut().enumerate() {
            assert_eq!(element.id, truth[i]);
            element.id = i as u32;
        }
        // values should have changed
        let truth = vec![0,1,2,3];
        for (i, element) in list.iter().enumerate() {
            assert_eq!(element.id, truth[i]);
        }
    }

    #[test]
    fn find_and_take() {
        static mut BUFFER: [u8; 128] = [0; 128];
        static ALLOCATOR: Bump = unsafe {
            Bump::new(
                NonNull::new_unchecked(BUFFER.as_ptr() as *mut _),
                NonNull::new_unchecked(BUFFER.as_ptr().add(BUFFER.len()) as *mut _)
            )};

        let mut list = LinkedList::new();
        list.emplace_back(MyStruct { id: 42 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 43 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 44 }, &ALLOCATOR);

        let mut another_list = LinkedList::new();

        let mut cursor = list.cursor_front_mut();
        let mut target: Option<Box<Node<MyStruct>>> = None;
        while let Some(element) = cursor.inner() {
            if element.id == 43 {
                target = cursor.take();
                break;
            }
            cursor.move_next();
        }
        another_list.push_back(target.unwrap());

        let truth = vec![42,44];
        for (i, element) in list.iter().enumerate() {
            assert_eq!(element.id, truth[i]);
        }

        for element in another_list.iter() {
            assert_eq!(element.id, 43);
        }
    }

    #[test]
    fn insert_at_condition() {
        static mut BUFFER: [u8; 256] = [0; 256];
        static ALLOCATOR: Bump = unsafe {
            Bump::new(
                NonNull::new_unchecked(BUFFER.as_ptr() as *mut _),
                NonNull::new_unchecked(BUFFER.as_ptr().add(BUFFER.len()) as *mut _)
            )};

        let mut list = LinkedList::new();
        list.emplace_back(MyStruct { id: 42 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 43 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 44 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 45 }, &ALLOCATOR);
        list.emplace_back(MyStruct { id: 48 }, &ALLOCATOR);

        let mut head = list.pop_front().unwrap();
        head.id = 47;

        list.insert_when(head, |head, node| {
            head.id < node.id
        });

        let truth = vec![43, 44, 45, 47, 48];
        for (i, element) in list.iter().enumerate() {
            assert_eq!(element.id, truth[i]);
        }
    }
}