clist 0.1.1

A hairy circular linked list for no_std environments.
Documentation
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
//! This crate contains a circularly and singly linked list implementation.
//!
//! Its operations are:
//!
//! operation             | runtime | description
//! ----------------------|---------|---------------
//! clist::lpush()        | O(1)    | insert as head (leftmost node)
//! clist::lpeek()        | O(1)    | get the head without removing it
//! clist::lpop()         | O(1)    | remove and return head (leftmost node)
//! clist::rpush()        | O(1)    | append as tail (rightmost node)
//! clist::rpeek()        | O(1)    | get the tail without removing it
//! clist::rpop()         | O(n)    | remove and return tail (rightmost node)
//! clist::lpoprpush()    | O(1)    | move first element to the end of the list
//! clist::contains(      | O(n)    | check if list contains element
//! clist::find()         | O(n)    | find and return node
//! clist::find_before()  | O(n)    | find node return node pointing to node
//! clist::remove()       | O(n)    | remove and return node
//! clist::sort()         | O(NlogN)| sort list (stable)
//! clist::count()        | O(n)    | count the number of elements in a list
//!
//! clist can be used as a traditional list, a queue (FIFO) and a stack (LIFO) using
//! fast O(1) operations.
//!

#![cfg_attr(not(test), no_std)]
// features needed by our use of memoffset
#![feature(const_ptr_offset_from)]
#![feature(const_refs_to_cell)]

use core::cell::UnsafeCell;
use core::marker::PhantomPinned;

extern crate memoffset;
pub use memoffset::offset_of;

#[derive(Debug)]
pub struct Link {
    next: UnsafeCell<*const Link>,
    _pin: PhantomPinned,
}

pub struct List {
    last: Option<Link>,
}

unsafe impl Sync for List {}
unsafe impl Send for List {}
unsafe impl Sync for Link {}
unsafe impl Send for Link {}

impl Link {
    pub const fn new() -> Link {
        Link {
            next: UnsafeCell::new(core::ptr::null()),
            _pin: PhantomPinned,
        }
    }

    pub const unsafe fn new_linked(link: *const Link) -> Link {
        Link {
            next: UnsafeCell::new(link),
            _pin: PhantomPinned,
        }
    }

    /// check if this Link is currently part of a list.
    pub fn is_linked(&self) -> bool {
        self.next.get() == core::ptr::null_mut()
    }

    unsafe fn link(&self, next: &Link) {
        *self.next.get() = next as *const Link;
    }

    unsafe fn next_ptr(&self) -> *const Link {
        *self.next.get()
    }

    unsafe fn next(&self) -> &Link {
        &*self.next_ptr()
    }
}

// public
impl List {
    /// creates a new, empty list
    pub const fn new() -> Self {
        List { last: None }
    }

    /// returns true if list does not contain any elements
    pub fn is_empty(&self) -> bool {
        self.last.is_none()
    }

    /// Inserts element at the beginning of the list
    /// Complexity: O(1)
    pub fn lpush(&mut self, element: &mut Link) {
        if self.is_empty() {
            unsafe { self.push_initial_element(element) };
        } else {
            unsafe {
                element.link(self.head());
                self.tail().link(element);
            };
        }
    }

    /// Remove and return element from the beginning of the list
    /// Complexity: O(1)
    pub fn lpop(&mut self) -> Option<&Link> {
        if self.is_empty() {
            None
        } else {
            unsafe {
                let head = self.head_ptr();
                if self.tail_ptr() == head {
                    self.last = None;
                } else {
                    self.tail().link(self.head().next());
                }

                Some(&*head)
            }
        }
    }

    /// Returns the first element in the list without removing it
    /// Complexity: O(1)
    pub fn lpeek(&self) -> Option<&Link> {
        if self.is_empty() {
            None
        } else {
            Some(unsafe { self.head() })
        }
    }

    /// Inserts element at the end of the list
    /// Complexity: O(1)
    pub fn rpush(&mut self, element: &mut Link) {
        self.lpush(element);
        self.last = Some(unsafe { Link::new_linked(element) });
    }

    /// Remove and return element from the end of the list
    /// Complexity: O(1)
    pub fn rpop(&mut self) -> Option<&Link> {
        if self.is_empty() {
            None
        } else {
            let tail = unsafe { &*self.tail_ptr() };
            self.remove(tail)
        }
    }

    /// Returns the last element in the list without removing it
    /// Complexity: O(1)
    pub fn rpeek(&self) -> Option<&Link> {
        if self.is_empty() {
            None
        } else {
            Some(unsafe { self.tail() })
        }
    }

    /// Rotates list (first becomes last, second becomes first)
    /// Complexity: O(1)
    pub fn lpoprpush(&mut self) {
        if !self.is_empty() {
            unsafe { self.last().link(self.head()) };
        }
    }

    /// Find element
    /// Complexity: O(n)
    pub fn find(&self, element: &Link) -> Option<&Link> {
        unsafe { self.find_previous(element).and_then(|x| Some(x.next())) }
    }

    /// Remove and return element
    /// Complexity: O(n)
    pub fn remove(&mut self, element: &Link) -> Option<&Link> {
        if self.is_empty() {
            None
        } else if unsafe { self.head_ptr() } == element as *const _ {
            // this deals with the case of removing the only element,
            // at the cost of comparing head to element twice
            self.lpop()
        } else {
            unsafe {
                // storing element here so we can return it from the closure
                let res = element as *const _;
                if let Some(prev) = self.find_previous(element) {
                    prev.link(prev.next().next());
                    if self.tail_ptr() == res {
                        self.last().link(prev);
                    }
                    Some(&*res)
                } else {
                    None
                }
            }
        }
    }

    pub fn contains(&mut self, element: &Link) -> bool {
        unsafe { self.find_previous(element).is_some() }
    }

    pub fn iter(&self) -> Iter {
        let empty = self.is_empty();
        Iter {
            list: self,
            pos: if empty {
                core::ptr::null()
            } else {
                unsafe { self.head_ptr() }
            },
            stop: empty,
        }
    }

    pub fn iter_mut(&self) -> IterMut {
        let empty = self.is_empty();
        IterMut {
            list: self,
            pos: if empty {
                core::ptr::null()
            } else {
                unsafe { self.head_ptr() }
            },
            stop: empty,
        }
    }
}

impl core::fmt::Debug for List {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        if self.is_empty() {
            write!(f, "List {{}}")
        } else {
            unsafe {
                write!(
                    f,
                    "List {{ {:x} {:x}:{:x} {:x}:{:x}",
                    self.last().next_ptr() as usize,
                    self.tail() as *const _ as usize,
                    self.tail().next_ptr() as usize,
                    self.head() as *const _ as usize,
                    self.head().next_ptr() as usize,
                )
            }
        }
    }
}

/// internal
impl List {
    unsafe fn last(&self) -> &Link {
        &self.last.as_ref().unwrap_unchecked()
    }

    unsafe fn tail(&self) -> &Link {
        self.last().next()
    }

    unsafe fn tail_ptr(&self) -> *const Link {
        self.last().next_ptr()
    }

    unsafe fn head(&self) -> &Link {
        self.tail().next()
    }

    unsafe fn head_ptr(&self) -> *const Link {
        self.tail().next()
    }

    unsafe fn push_initial_element(&mut self, element: &mut Link) {
        element.link(element);
        self.last = Some(Link::new_linked(element));
    }

    unsafe fn find_previous(&self, element: &Link) -> Option<&Link> {
        if self.is_empty() {
            return None;
        }
        let mut pos = self.tail();
        let tail_ptr = pos as *const Link;
        let element_ptr = element as *const Link;
        loop {
            let next_ptr = pos.next_ptr();
            if next_ptr == element_ptr {
                return Some(pos);
            }
            if next_ptr == tail_ptr {
                return None;
            }
            pos = pos.next();
        }
    }
}

pub struct Iter<'a> {
    list: &'a List,
    pos: *const Link,
    stop: bool,
}

pub struct IterMut<'a> {
    list: &'a List,
    pos: *const Link,
    stop: bool,
}

impl<'a> Iterator for Iter<'a> {
    type Item = &'a Link;
    fn next(&mut self) -> Option<&'a Link> {
        if self.stop {
            None
        } else {
            unsafe {
                if self.list.tail_ptr() as *const _ == self.pos {
                    self.stop = true;
                }
                let res = &*self.pos;
                self.pos = res.next_ptr();
                Some(res)
            }
        }
    }
}

impl<'a> Iterator for IterMut<'a> {
    type Item = &'a Link;
    fn next(&mut self) -> Option<&'a Link> {
        if self.stop {
            None
        } else {
            unsafe {
                if self.list.tail_ptr() as *const _ == self.pos {
                    self.stop = true;
                }
                let res = &*self.pos;
                self.pos = res.next_ptr();
                Some(res)
            }
        }
    }
}

#[derive(Debug)]
pub struct TypedList<T, const OFFSET: usize> {
    list: List,
    _phantom: core::marker::PhantomData<T>,
}

impl<T, const OFFSET: usize> TypedList<T, { OFFSET }> {
    pub const fn new() -> Self {
        Self {
            list: List::new(),
            _phantom: core::marker::PhantomData {},
        }
    }

    pub fn is_empty(&mut self) -> bool {
        self.list.is_empty()
    }

    pub fn lpush(&mut self, element: &mut T) {
        let element = ((element as *mut T) as usize + OFFSET) as *mut Link;
        self.list.lpush(unsafe { &mut *element })
    }

    pub fn rpush(&mut self, element: &mut T) {
        let element = ((element as *mut T) as usize + OFFSET) as *mut Link;
        self.list.rpush(unsafe { &mut *element })
    }

    pub fn lpop(&mut self) -> Option<&mut T> {
        match self.list.lpop() {
            None => None,
            Some(link) => {
                Some(unsafe { &mut *((link as *const Link as usize - OFFSET) as *mut T) })
            }
        }
    }

    pub fn rpop(&mut self) -> Option<&mut T> {
        match self.list.rpop() {
            None => None,
            Some(link) => {
                Some(unsafe { &mut *((link as *const Link as usize - OFFSET) as *mut T) })
            }
        }
    }

    pub fn lpoprpush(&mut self) {
        self.list.lpoprpush()
    }

    pub fn remove(&mut self, element: &mut T) -> Option<&T> {
        let element = ((element as *mut T) as usize + OFFSET) as *mut Link;
        self.list
            .remove(unsafe { &mut *element })
            .and_then(|x| Some(unsafe { &*((x as *const Link as usize - OFFSET) as *mut T) }))
    }

    pub fn lpeek(&mut self) -> Option<&T> {
        match self.list.lpeek() {
            None => None,
            Some(link) => Some(unsafe { &*((link as *const Link as usize - OFFSET) as *mut T) }),
        }
    }

    pub fn rpeek(&mut self) -> Option<&T> {
        match self.list.rpeek() {
            None => None,
            Some(link) => Some(unsafe { &*((link as *const Link as usize - OFFSET) as *mut T) }),
        }
    }

    pub fn iter(&self) -> TypedIter<T> {
        TypedIter::<T> {
            iterator: self.list.iter(),
            offset: OFFSET,
            _phantom: core::marker::PhantomData::<T> {},
        }
    }

    pub fn iter_mut(&self) -> TypedIterMut<T> {
        TypedIterMut::<T> {
            iterator: self.list.iter(),
            offset: OFFSET,
            _phantom: core::marker::PhantomData::<T> {},
        }
    }
}

pub struct TypedIter<'a, T> {
    iterator: Iter<'a>,
    offset: usize,
    _phantom: core::marker::PhantomData<T>,
}

pub struct TypedIterMut<'a, T> {
    iterator: Iter<'a>,
    offset: usize,
    _phantom: core::marker::PhantomData<T>,
}

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

    fn next(&mut self) -> Option<&'a T> {
        if let Some(link) = self.iterator.next() {
            Some(unsafe { &*((link as *const Link as usize - self.offset) as *mut T) })
        } else {
            None
        }
    }
}

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

    fn next(&mut self) -> Option<&'a mut T> {
        if let Some(link) = self.iterator.next() {
            Some(unsafe { &mut *((link as *const Link as usize - self.offset) as *mut T) })
        } else {
            None
        }
    }
}

// pub struct TypedIter<'a, T, const OFFSET: usize> {
//     iterator: Iter<'a>,
//     _phantom: core::marker::PhantomData<T>,
// }

// impl<'a, T: 'a, const OFFSET: usize> Iterator for TypedIter<'a, T, OFFSET> {
//     type Item = &'a T;

//     fn next(&mut self) -> Option<&'a T> {
//         if let Some(link) = self.iterator.next() {
//             Some(unsafe { &*((link as *const Link as usize - OFFSET) as *const T) })
//         } else {
//             None
//         }
//     }
// }

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_lpush_lpop_1() {
        let mut list = List::new();
        assert!(list.lpop().is_none());

        let mut node = Link::new();

        list.lpush(&mut node);

        assert!(unsafe { node.next_ptr() } == &node as *const Link);
        assert!(list.lpop().unwrap() as *const Link == &node as *const Link);
        assert!(list.lpop().is_none());
    }

    #[test]
    fn test_lpush_lpop_2() {
        let mut list = List::new();
        assert!(list.lpop().is_none());

        let mut node = Link::new();
        list.lpush(&mut node);
        assert!(unsafe { node.next_ptr() } == &node as *const Link);

        let mut node2 = Link::new();
        list.lpush(&mut node2);

        assert!(unsafe { node2.next_ptr() } == &node as *const Link);
        assert!(unsafe { node.next_ptr() } == &node2 as *const Link);
        assert!(unsafe { list.last().next_ptr() == &node as *const Link });

        assert!(list.lpop().unwrap() as *const Link == &node2 as *const Link);
        assert!(list.lpop().unwrap() as *const Link == &node as *const Link);
        assert!(list.lpop().is_none());
    }

    #[test]
    fn test_lpush_lpop_3() {
        let mut list = List::new();
        assert!(list.lpop().is_none());

        let mut node = Link::new();
        list.lpush(&mut node);
        assert!(unsafe { node.next_ptr() } == &node as *const Link);

        let mut node2 = Link::new();
        list.lpush(&mut node2);

        let mut node3 = Link::new();
        list.lpush(&mut node3);

        assert!(unsafe { node.next_ptr() } == &node3 as *const Link);
        assert!(unsafe { node2.next_ptr() } == &node as *const Link);
        assert!(unsafe { node3.next_ptr() } == &node2 as *const Link);
        assert!(unsafe { list.tail_ptr() == &node as *const Link });

        assert!(list.lpop().unwrap() as *const Link == &node3 as *const Link);
        assert!(unsafe { node.next_ptr() } == &node2 as *const Link);
        assert!(unsafe { node2.next_ptr() } == &node as *const Link);
        assert!(unsafe { list.tail_ptr() == &node as *const Link });
        //assert!(unsafe { node3.next_ptr() } == core::ptr::null());

        assert!(list.lpop().unwrap() as *const Link == &node2 as *const Link);
        assert!(unsafe { node.next_ptr() } == &node as *const Link);
        assert!(unsafe { list.tail_ptr() == &node as *const Link });
        //assert!(unsafe { node2.next_ptr() } == core::ptr::null());

        assert!(list.lpop().unwrap() as *const Link == &node as *const Link);
        //assert!(unsafe { node.next_ptr() } == core::ptr::null());
        assert!(list.last.is_none());

        assert!(list.lpop().is_none());
    }

    #[test]
    fn test_lpoprpush() {
        let mut list = List::new();

        let mut node = Link::new();
        let mut node2 = Link::new();

        list.lpush(&mut node);
        list.lpush(&mut node2);
        list.lpoprpush();

        assert!(list.lpop().unwrap() as *const _ == &node as *const _);
        assert!(list.lpop().unwrap() as *const _ == &node2 as *const _);
        assert!(list.lpop().is_none());
    }

    #[test]
    fn test_rpush() {
        let mut list = List::new();

        let mut node = Link::new();
        let mut node2 = Link::new();

        list.rpush(&mut node);
        list.rpush(&mut node2);

        assert!(list.lpop().unwrap() as *const _ == &mut node as *const _);
        assert!(list.lpop().unwrap() as *const _ == &mut node2 as *const _);
        assert!(list.lpop().is_none());
    }

    #[test]
    fn test_rpop() {
        let mut list = List::new();

        let mut node = Link::new();
        let mut node2 = Link::new();
        let mut node3 = Link::new();

        list.rpush(&mut node);
        list.rpush(&mut node2);
        list.rpush(&mut node3);

        assert!(unsafe { node.next_ptr() } == &node2 as *const Link);
        assert!(unsafe { node2.next_ptr() } == &node3 as *const Link);
        assert!(unsafe { node3.next_ptr() } == &node as *const Link);
        assert!(unsafe { list.tail_ptr() == &node3 as *const Link });

        assert!(list.rpop().unwrap() as *const _ == &mut node3 as *const _);
        assert!(unsafe { node.next_ptr() } == &node2 as *const Link);
        assert!(unsafe { node2.next_ptr() } == &node as *const Link);
        assert!(unsafe { list.tail_ptr() == &node2 as *const Link });

        assert!(list.rpop().unwrap() as *const _ == &mut node2 as *const _);
        assert!(unsafe { node.next_ptr() } == &node as *const Link);
        assert!(unsafe { list.tail_ptr() == &node as *const Link });

        assert!(list.rpop().unwrap() as *const _ == &mut node as *const _);
        assert!(list.is_empty());
        assert!(list.rpop().is_none());
    }

    #[test]
    fn test_remove_first() {
        let mut list = List::new();

        let mut node = Link::new();
        let mut node2 = Link::new();
        let mut node3 = Link::new();

        list.rpush(&mut node);
        list.rpush(&mut node2);
        list.rpush(&mut node3);

        assert!(list.remove(&node).is_some());

        assert!(list.rpop().unwrap() as *const _ == &mut node3 as *const _);
        assert!(list.rpop().unwrap() as *const _ == &mut node2 as *const _);
        assert!(list.rpop().is_none());
        assert!(list.lpop().is_none());
    }

    #[test]
    fn test_remove_mid() {
        let mut list = List::new();

        let mut node = Link::new();
        let mut node2 = Link::new();
        let mut node3 = Link::new();

        list.rpush(&mut node);
        list.rpush(&mut node2);
        list.rpush(&mut node3);

        assert!(list.remove(&node2).is_some());

        assert!(list.rpop().unwrap() as *const _ == &mut node3 as *const _);
        assert!(list.rpop().unwrap() as *const _ == &mut node as *const _);
        assert!(list.rpop().is_none());
        assert!(list.lpop().is_none());
    }

    #[test]
    fn test_remove_last() {
        let mut list = List::new();

        let mut node = Link::new();
        let mut node2 = Link::new();
        let mut node3 = Link::new();

        list.rpush(&mut node);
        list.rpush(&mut node2);
        list.rpush(&mut node3);

        assert!(list.remove(&node3).is_some());

        assert!(list.rpop().unwrap() as *const _ == &mut node2 as *const _);
        assert!(list.rpop().unwrap() as *const _ == &mut node as *const _);
        assert!(list.rpop().is_none());
        assert!(list.lpop().is_none());
    }

    #[test]
    fn test_iterator() {
        let mut list = List::new();

        let mut node = Link::new();
        let mut node2 = Link::new();
        let mut node3 = Link::new();

        list.rpush(&mut node);
        list.rpush(&mut node2);
        list.rpush(&mut node3);

        let pointers = [
            &mut node as *const Link,
            &mut node2 as *const Link,
            &mut node3 as *const Link,
        ];

        println!("pointers:");
        for entry in pointers.iter() {
            println!("{:x}", *entry as usize);
        }

        println!("list entries:");
        let mut i = 0;
        for entry in list.iter() {
            println!("{:x}", entry as *const Link as usize);
            assert_eq!(entry as *const Link, pointers[i]);
            i += 1;
        }
        assert_eq!(i, 3);
    }

    #[test]
    fn test_iterator_mut() {
        let mut list = List::new();

        let mut node = Link::new();
        let mut node2 = Link::new();
        let mut node3 = Link::new();

        list.rpush(&mut node);
        list.rpush(&mut node2);
        list.rpush(&mut node3);

        let pointers = [
            &mut node as *const Link,
            &mut node2 as *const Link,
            &mut node3 as *const Link,
        ];

        println!("pointers:");
        for entry in pointers.iter() {
            println!("{:x}", *entry as usize);
        }

        println!("list entries:");
        let mut i = 0;
        for entry in list.iter_mut() {
            println!("{:x}", entry as *const Link as usize);
            assert_eq!(entry as *const Link, pointers[i]);
            i += 1;
        }
        assert_eq!(i, 3);
    }

    #[test]
    fn test_iterator_empty() {
        let list = List::new();

        for _ in list.iter() {
            assert!(false);
        }
    }

    #[test]
    fn test_typed_iterator() {
        struct Data {
            data: u32,
            list_entry: Link,
        }

        let mut list: TypedList<Data, { offset_of!(Data, list_entry) }> = TypedList::new();

        let mut node = Data {
            data: 0,
            list_entry: Link::new(),
        };

        let mut node2 = Data {
            data: 1,
            list_entry: Link::new(),
        };

        let mut node3 = Data {
            data: 2,
            list_entry: Link::new(),
        };

        list.rpush(&mut node);
        list.rpush(&mut node2);
        list.rpush(&mut node3);

        let expected = [0 as u32, 1, 2];

        println!("list entries:");
        let mut i = 0;
        for entry in list.iter() {
            println!("{}", entry.data);
            assert_eq!(entry.data, expected[i]);
            i += 1;
        }
        assert_eq!(i, 3);
    }
}