embed_collections/
dlist.rs

1//! An intrusive doubly linked list implementation.
2//!
3//! This module provides `DLinkedList`, a doubly linked list where elements
4//! embed the list nodes themselves. This design offers memory efficiency
5//! and explicit control over allocation, suitable for scenarios like
6//! building LRU caches directly within data structures.
7//!
8//! # Features
9//! - O(1) push and pop from both front and back.
10//! - Generic over pointer types (`Box`, `Arc`, `NonNull`, raw pointers).
11//! - Supports multiple lists for the same item via `Tag`.
12//!
13//! # Example
14//! ```rust
15//! use embed_collections::{dlist::{DLinkedList, DListItem, DListNode}, Pointer};
16//! use std::cell::UnsafeCell;
17//!
18//! struct MyItem {
19//!     id: u32,
20//!     data: String,
21//!     node: UnsafeCell<DListNode<MyItem, ()>>, // Node embedded directly
22//! }
23//!
24//! impl MyItem {
25//!     fn new(id: u32, data: &str) -> Self {
26//!         MyItem {
27//!             id,
28//!             data: data.to_string(),
29//!             node: UnsafeCell::new(DListNode::default()),
30//!         }
31//!     }
32//! }
33//!
34//! // Safety: Implementors must ensure `get_node` returns a valid reference
35//! // to the embedded `DListNode`. `UnsafeCell` is used for interior mutability.
36//! unsafe impl DListItem<()> for MyItem {
37//!     fn get_node(&self) -> &mut DListNode<Self, ()> {
38//!         unsafe { &mut *self.node.get() }
39//!     }
40//! }
41//!
42//! let mut list = DLinkedList::<Box<MyItem>, ()>::new();
43//!
44//! list.push_back(Box::new(MyItem::new(1, "First")));
45//! list.push_front(Box::new(MyItem::new(2, "Second")));
46//! list.push_back(Box::new(MyItem::new(3, "Third")));
47//!
48//! assert_eq!(list.len(), 3);
49//!
50//! // List order: (2, "Second") <-> (1, "First") <-> (3, "Third")
51//! assert_eq!(list.pop_front().unwrap().id, 2);
52//! assert_eq!(list.pop_back().unwrap().id, 3);
53//! assert_eq!(list.pop_front().unwrap().id, 1);
54//! assert!(list.is_empty());
55//! ```
56
57use crate::Pointer;
58use core::marker::PhantomData;
59use core::{fmt, mem, ptr::null};
60
61/// A trait to return internal mutable DListNode for specified list.
62///
63/// The tag is used to distinguish different DListNodes within the same item,
64/// allowing an item to belong to multiple lists simultaneously.
65/// For only one ownership, you can use `()`.
66///
67/// # Safety
68///
69/// Implementors must ensure `get_node` returns a valid reference to the `DListNode`
70/// embedded within `Self`. Users must use `UnsafeCell` to hold `DListNode` to support
71/// interior mutability required by list operations.
72pub unsafe trait DListItem<Tag>: Sized {
73    fn get_node(&self) -> &mut DListNode<Self, Tag>;
74}
75
76/// The node structure that must be embedded in items to be stored in a `DLinkedList`.
77#[repr(C)]
78pub struct DListNode<T: Sized, Tag> {
79    prev: *const T,
80    next: *const T,
81    _phan: PhantomData<fn(&Tag)>,
82}
83
84unsafe impl<T, Tag> Send for DListNode<T, Tag> {}
85
86impl<T: DListItem<Tag>, Tag> DListNode<T, Tag> {
87    #[inline]
88    fn get_prev<'a>(&self) -> Option<&'a mut DListNode<T, Tag>> {
89        if self.prev.is_null() { None } else { unsafe { Some((*self.prev).get_node()) } }
90    }
91
92    #[inline]
93    fn get_next<'a>(&self) -> Option<&'a mut DListNode<T, Tag>> {
94        if self.next.is_null() { None } else { unsafe { Some((*self.next).get_node()) } }
95    }
96}
97
98impl<T, Tag> Default for DListNode<T, Tag> {
99    #[inline(always)]
100    fn default() -> Self {
101        Self { prev: null(), next: null(), _phan: Default::default() }
102    }
103}
104
105impl<T: DListItem<Tag> + fmt::Debug, Tag> fmt::Debug for DListNode<T, Tag> {
106    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107        write!(f, "(")?;
108        if !self.prev.is_null() {
109            write!(f, "prev: {:p} ", self.prev)?;
110        } else {
111            write!(f, "prev: none ")?;
112        }
113        if !self.next.is_null() {
114            write!(f, "next: {:p} ", self.next)?;
115        } else {
116            write!(f, "next: none ")?;
117        }
118        write!(f, ")")
119    }
120}
121
122/// An intrusive doubly linked list.
123///
124/// Supports O(1) insertion and removal at both ends.
125#[repr(C)]
126pub struct DLinkedList<P, Tag>
127where
128    P: Pointer,
129    P::Target: DListItem<Tag>,
130{
131    length: u64,
132    head: *const P::Target,
133    tail: *const P::Target,
134    _phan: PhantomData<fn(&Tag)>,
135}
136
137unsafe impl<P, Tag> Send for DLinkedList<P, Tag>
138where
139    P: Pointer,
140    P::Target: DListItem<Tag>,
141{
142}
143
144impl<P: fmt::Debug, Tag> fmt::Debug for DLinkedList<P, Tag>
145where
146    P: Pointer,
147    P::Target: DListItem<Tag>,
148{
149    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
150        write!(f, "{{ length: {} ", self.length)?;
151        if !self.head.is_null() {
152            write!(f, "head: {:?} ", self.head)?;
153        } else {
154            write!(f, "head: none ")?;
155        }
156        if !self.tail.is_null() {
157            write!(f, "tail: {:?} ", self.tail)?;
158        } else {
159            write!(f, "tail: none ")?;
160        }
161        write!(f, "}}")
162    }
163}
164
165impl<P, Tag> DLinkedList<P, Tag>
166where
167    P: Pointer,
168    P::Target: DListItem<Tag>,
169{
170    /// Creates a new, empty doubly linked list.
171    #[inline(always)]
172    pub fn new() -> Self {
173        DLinkedList { length: 0, head: null(), tail: null(), _phan: Default::default() }
174    }
175
176    /// Clears the list pointers. Note: This does NOT drop the elements.
177    /// Use `drain` or `drop` if you need to release owned resources.
178    #[inline]
179    pub fn clear(&mut self) {
180        self.length = 0;
181        self.head = null();
182        self.tail = null();
183    }
184
185    /// Returns the length of the list.
186    #[inline(always)]
187    pub fn get_length(&self) -> u64 {
188        return self.length;
189    }
190
191    /// Returns the length of the list as `usize`.
192    #[inline(always)]
193    pub fn len(&self) -> usize {
194        return self.length as usize;
195    }
196
197    /// Returns `true` if the list contains no elements.
198    #[inline(always)]
199    pub fn is_empty(&self) -> bool {
200        self.length == 0
201    }
202
203    #[inline(always)]
204    fn remove_node(&mut self, item: &P::Target) {
205        let node = item.get_node();
206        if let Some(prev) = node.get_prev() {
207            prev.next = node.next;
208        } else {
209            self.head = node.next;
210        }
211        if let Some(next) = node.get_next() {
212            next.prev = node.prev;
213        } else {
214            self.tail = node.prev;
215        }
216        node.next = null();
217        node.prev = null();
218        self.length -= 1;
219    }
220
221    /// Moves a node to the front of the list (e.g., for LRU updates).
222    ///
223    /// # Safety
224    /// The pointer `ptr` must be valid and pointing to an element currently in the list
225    /// or a valid free element if properly managed (though typically used for re-ordering).
226    #[inline(always)]
227    pub unsafe fn peak(&mut self, ptr: *mut P::Target) {
228        assert!(!self.head.is_null());
229        let item = unsafe { &(*ptr) };
230        if !self.head.is_null() {
231            let head_node = unsafe { (*self.head).get_node() } as *const DListNode<P::Target, Tag>;
232            if head_node == item.get_node() as *const DListNode<P::Target, Tag> {
233                return;
234            }
235        }
236        self.remove_node(item);
237        self.push_front_ptr(ptr);
238    }
239
240    /// Pushes an element to the front of the list.
241    #[inline]
242    pub fn push_front(&mut self, item: P) {
243        let ptr = item.into_raw();
244        self.push_front_ptr(ptr);
245    }
246
247    #[inline]
248    fn push_front_ptr(&mut self, ptr: *const P::Target) {
249        let node = unsafe { (*ptr).get_node() };
250        let head = self.head;
251        node.next = head;
252        node.prev = null();
253
254        if head.is_null() {
255            self.tail = ptr;
256        } else {
257            unsafe {
258                (*head).get_node().prev = ptr;
259            }
260        }
261        self.head = ptr;
262        self.length += 1;
263    }
264
265    /// Pushes an element to the back of the list.
266    #[inline]
267    pub fn push_back(&mut self, item: P) {
268        let node = item.as_ref().get_node();
269        let tail = self.tail;
270        node.prev = tail;
271        node.next = null();
272
273        let ptr = item.into_raw();
274        if tail.is_null() {
275            self.head = ptr;
276        } else {
277            unsafe {
278                (*tail).get_node().next = ptr;
279            }
280        }
281        self.tail = ptr;
282        self.length += 1;
283    }
284
285    /// Removes and returns the element at the front of the list.
286    pub fn pop_front(&mut self) -> Option<P> {
287        if self.head.is_null() {
288            None
289        } else {
290            let head_ptr = self.head;
291            let item = unsafe { &(*head_ptr) };
292            self.remove_node(item);
293            Some(unsafe { P::from_raw(head_ptr) })
294        }
295    }
296
297    /// Removes and returns the element at the back of the list.
298    #[inline]
299    pub fn pop_back(&mut self) -> Option<P> {
300        if self.tail.is_null() {
301            None
302        } else {
303            let tail_ptr = self.tail;
304            let item = unsafe { &(*tail_ptr) };
305            self.remove_node(item);
306            Some(unsafe { P::from_raw(tail_ptr) })
307        }
308    }
309
310    /// Returns a reference to the front element.
311    #[inline]
312    pub fn get_front(&self) -> Option<&P::Target> {
313        if self.head.is_null() { None } else { unsafe { Some(&(*self.head)) } }
314    }
315
316    /// Returns a reference to the back element.
317    #[inline]
318    pub fn get_back(&self) -> Option<&P::Target> {
319        if self.tail.is_null() { None } else { unsafe { Some(&(*self.tail)) } }
320    }
321
322    /// Checks if the given node is the head of the list.
323    #[inline(always)]
324    pub fn is_front(&self, node: &P::Target) -> bool {
325        if self.head.is_null() {
326            false
327        } else {
328            // This comparison is tricky because self.head is *mut HrcWrapper<T>
329            // and node is &mut DListNode<T>.
330            // We need to compare the node address or the wrapper address.
331            // Converting head -> node and comparing addresses of DListNode is safer.
332            self.head == node as *const P::Target
333        }
334    }
335
336    #[cfg(feature = "std")]
337    pub fn print<U: std::fmt::Debug>(&self) {
338        println!("print list begin! length={}", self.length);
339        let mut ptr = self.head;
340        while !ptr.is_null() {
341            unsafe {
342                // Assuming T can be cast to U for printing, or T implements Debug.
343                // The original code had print<T>, here print<U>.
344                // We'll just print the address for now if T is not Debug?
345                // Or assume T is Debug.
346                // println!("node={:?}", item); // Requires T: Debug
347                ptr = (*ptr).get_node().next;
348            }
349        }
350        println!("print list end:");
351    }
352
353    /// Returns an iterator over the list (borrowed).
354    ///
355    /// # NOTE
356    ///
357    /// If you plan on turn the raw pointer to owned, use drain instead
358    ///
359    /// # Safety
360    ///
361    /// The caller must ensure that the list is not modified in a way that can
362    /// invalidate internal pointers (such as removing elements or dropping
363    /// items) for the duration of the iterator's use.
364    #[inline(always)]
365    pub fn iter<'a>(&'a self) -> DLinkedListIterator<'a, P, Tag> {
366        DLinkedListIterator { list: self, cur: null() }
367    }
368
369    /// Returns a draining iterator that removes items from the list.
370    /// Crucial for cleaning up lists containing owned pointers (like `Box`).
371    #[inline(always)]
372    pub fn drain<'a>(&'a mut self) -> DLinkedListDrainer<'a, P, Tag> {
373        DLinkedListDrainer { list: self }
374    }
375}
376
377impl<P, Tag> Drop for DLinkedList<P, Tag>
378where
379    P: Pointer,
380    P::Target: DListItem<Tag>,
381{
382    fn drop(&mut self) {
383        // Calling drain will remove all elements from the list and drop them.
384        // The DLinkedListDrainer iterator returns P, which will be dropped
385        // when the iterator is consumed.
386        if mem::needs_drop::<P>() {
387            self.drain().for_each(drop);
388        }
389    }
390}
391
392pub struct DLinkedListIterator<'a, P, Tag>
393where
394    P: Pointer,
395    P::Target: DListItem<Tag>,
396{
397    list: &'a DLinkedList<P, Tag>,
398    cur: *const P::Target,
399}
400
401unsafe impl<'a, P, Tag> Send for DLinkedListIterator<'a, P, Tag>
402where
403    P: Pointer,
404    P::Target: DListItem<Tag>,
405{
406}
407
408impl<'a, P, Tag> Iterator for DLinkedListIterator<'a, P, Tag>
409where
410    P: Pointer,
411    P::Target: DListItem<Tag>,
412{
413    type Item = &'a P::Target;
414
415    fn next(&mut self) -> Option<Self::Item> {
416        if self.cur.is_null() {
417            if self.list.head.is_null() {
418                return None;
419            } else {
420                self.cur = self.list.head;
421            }
422        } else {
423            let next = unsafe { (*self.cur).get_node().next };
424            if next.is_null() {
425                return None;
426            } else {
427                self.cur = next;
428            }
429        }
430        unsafe { Some(&(*self.cur)) }
431    }
432}
433
434pub struct DLinkedListDrainer<'a, P, Tag>
435where
436    P: Pointer,
437    P::Target: DListItem<Tag>,
438{
439    list: &'a mut DLinkedList<P, Tag>,
440}
441
442unsafe impl<'a, P, Tag> Send for DLinkedListDrainer<'a, P, Tag>
443where
444    P: Pointer,
445    P::Target: DListItem<Tag>,
446{
447}
448
449impl<'a, P, Tag> Iterator for DLinkedListDrainer<'a, P, Tag>
450where
451    P: Pointer,
452    P::Target: DListItem<Tag>,
453{
454    type Item = P;
455
456    #[inline]
457    fn next(&mut self) -> Option<P> {
458        self.list.pop_back()
459    }
460}
461
462#[cfg(test)]
463mod tests {
464    use super::*;
465    use std::cell::UnsafeCell;
466    use std::ptr::NonNull;
467    use std::sync::Arc;
468    use std::sync::atomic::{AtomicUsize, Ordering};
469
470    pub struct TestTag;
471
472    #[derive(Debug)]
473    pub struct TestNode {
474        pub value: i64,
475        pub node: UnsafeCell<DListNode<Self, TestTag>>,
476    }
477
478    static ACTIVE_NODE_COUNT: AtomicUsize = AtomicUsize::new(0);
479
480    impl Drop for TestNode {
481        fn drop(&mut self) {
482            ACTIVE_NODE_COUNT.fetch_sub(1, Ordering::SeqCst);
483        }
484    }
485
486    unsafe impl Send for TestNode {}
487
488    unsafe impl DListItem<TestTag> for TestNode {
489        fn get_node(&self) -> &mut DListNode<Self, TestTag> {
490            unsafe { &mut *self.node.get() }
491        }
492    }
493
494    fn new_node(v: i64) -> TestNode {
495        ACTIVE_NODE_COUNT.fetch_add(1, Ordering::SeqCst);
496        TestNode { value: v, node: UnsafeCell::new(DListNode::default()) }
497    }
498
499    #[test]
500    fn test_push_back_box() {
501        let mut l = DLinkedList::<Box<TestNode>, TestTag>::new();
502
503        let node1 = Box::new(new_node(1));
504        l.push_back(node1);
505
506        let node2 = Box::new(new_node(2));
507        l.push_back(node2);
508
509        let node3 = Box::new(new_node(3));
510        l.push_back(node3);
511
512        assert_eq!(3, l.get_length());
513
514        let mut iter = l.iter();
515        assert_eq!(iter.next().unwrap().value, 1);
516        assert_eq!(iter.next().unwrap().value, 2);
517        assert_eq!(iter.next().unwrap().value, 3);
518        assert!(iter.next().is_none());
519
520        {
521            let mut drain = l.drain();
522            assert_eq!(drain.next().unwrap().value, 3);
523            assert_eq!(drain.next().unwrap().value, 2);
524            assert_eq!(drain.next().unwrap().value, 1);
525            assert!(drain.next().is_none());
526        }
527        assert_eq!(l.get_length(), 0);
528    }
529
530    #[test]
531    fn test_push_back_arc() {
532        let mut l = DLinkedList::<Arc<TestNode>, TestTag>::new();
533
534        let node1 = Arc::new(new_node(1));
535        l.push_back(node1);
536
537        let node2 = Arc::new(new_node(2));
538        l.push_back(node2);
539
540        let node3 = Arc::new(new_node(3));
541        l.push_back(node3);
542
543        assert_eq!(3, l.get_length());
544
545        let mut iter = l.iter();
546        assert_eq!(iter.next().unwrap().value, 1);
547        assert_eq!(iter.next().unwrap().value, 2);
548        assert_eq!(iter.next().unwrap().value, 3);
549        assert!(iter.next().is_none());
550
551        {
552            let mut drain = l.drain();
553            assert_eq!(drain.next().unwrap().value, 3);
554            assert_eq!(drain.next().unwrap().value, 2);
555            assert_eq!(drain.next().unwrap().value, 1);
556            assert!(drain.next().is_none());
557        }
558        assert_eq!(l.get_length(), 0);
559    }
560
561    #[test]
562    fn test_push_front_box() {
563        let mut l = DLinkedList::<Box<TestNode>, TestTag>::new();
564
565        let node3 = Box::new(new_node(3));
566        l.push_front(node3);
567
568        let node2 = Box::new(new_node(2));
569        l.push_front(node2);
570
571        let node1 = Box::new(new_node(1));
572        l.push_front(node1);
573
574        assert_eq!(3, l.get_length());
575
576        let mut iter = l.iter();
577        assert_eq!(iter.next().unwrap().value, 1);
578        assert_eq!(iter.next().unwrap().value, 2);
579        assert_eq!(iter.next().unwrap().value, 3);
580        assert!(iter.next().is_none());
581
582        {
583            let mut drain = l.drain();
584            assert_eq!(drain.next().unwrap().value, 3);
585            assert_eq!(drain.next().unwrap().value, 2);
586            assert_eq!(drain.next().unwrap().value, 1);
587            assert!(drain.next().is_none());
588        }
589        assert_eq!(l.get_length(), 0);
590    }
591
592    #[test]
593    fn test_push_front_arc() {
594        let mut l = DLinkedList::<Arc<TestNode>, TestTag>::new();
595
596        let node3 = Arc::new(new_node(3));
597        l.push_front(node3);
598
599        let node2 = Arc::new(new_node(2));
600        l.push_front(node2);
601
602        let node1 = Arc::new(new_node(1));
603        l.push_front(node1);
604
605        assert_eq!(3, l.get_length());
606
607        let mut iter = l.iter();
608        assert_eq!(iter.next().unwrap().value, 1);
609        assert_eq!(iter.next().unwrap().value, 2);
610        assert_eq!(iter.next().unwrap().value, 3);
611        assert!(iter.next().is_none());
612
613        {
614            let mut drain = l.drain();
615            assert_eq!(drain.next().unwrap().value, 3);
616            assert_eq!(drain.next().unwrap().value, 2);
617            assert_eq!(drain.next().unwrap().value, 1);
618            assert!(drain.next().is_none());
619        }
620        assert_eq!(l.get_length(), 0);
621    }
622
623    #[test]
624    fn test_pop_back_box() {
625        let mut l = DLinkedList::<Box<TestNode>, TestTag>::new();
626
627        let node1 = Box::new(new_node(1));
628        l.push_back(node1);
629
630        let node2 = Box::new(new_node(2));
631        l.push_back(node2);
632
633        let node3 = Box::new(new_node(3));
634        l.push_back(node3);
635
636        let mut iter = l.iter();
637        assert_eq!(iter.next().unwrap().value, 1);
638        assert_eq!(iter.next().unwrap().value, 2);
639        assert_eq!(iter.next().unwrap().value, 3);
640        assert!(iter.next().is_none());
641
642        let del_node = l.pop_back();
643        assert_eq!(2, l.get_length());
644        assert!(del_node.is_some());
645        assert_eq!(del_node.unwrap().value, 3);
646
647        let mut iter_remaining = l.iter();
648        assert_eq!(iter_remaining.next().unwrap().value, 1);
649        assert_eq!(iter_remaining.next().unwrap().value, 2);
650        assert!(iter_remaining.next().is_none());
651
652        {
653            let mut drain = l.drain();
654            assert_eq!(drain.next().unwrap().value, 2);
655            assert_eq!(drain.next().unwrap().value, 1);
656            assert!(drain.next().is_none());
657        }
658        assert_eq!(l.get_length(), 0);
659    }
660
661    #[test]
662    fn test_pop_back_arc() {
663        let mut l = DLinkedList::<Arc<TestNode>, TestTag>::new();
664
665        let node1 = Arc::new(new_node(1));
666        l.push_back(node1);
667
668        let node2 = Arc::new(new_node(2));
669        l.push_back(node2);
670
671        let node3 = Arc::new(new_node(3));
672        l.push_back(node3);
673
674        let mut iter = l.iter();
675        assert_eq!(iter.next().unwrap().value, 1);
676        assert_eq!(iter.next().unwrap().value, 2);
677        assert_eq!(iter.next().unwrap().value, 3);
678        assert!(iter.next().is_none());
679
680        let del_node = l.pop_back();
681        assert_eq!(2, l.get_length());
682        assert!(del_node.is_some());
683        // Note: The value returned by Arc::from_raw must still be used.
684        assert!(del_node.is_some());
685
686        // Check the order of remaining elements
687        let mut iter = l.iter();
688        assert_eq!(iter.next().unwrap().value, 1);
689        assert_eq!(iter.next().unwrap().value, 2);
690        assert!(iter.next().is_none());
691
692        {
693            let mut drain = l.drain();
694            assert_eq!(drain.next().unwrap().value, 2);
695            assert_eq!(drain.next().unwrap().value, 1);
696            assert!(drain.next().is_none());
697        }
698        assert_eq!(l.get_length(), 0);
699    }
700
701    #[test]
702    fn test_iter_box() {
703        let mut l = DLinkedList::<Box<TestNode>, TestTag>::new();
704
705        let mut count = 0;
706        for _item in l.iter() {
707            count += 1;
708        }
709        assert_eq!(count, 0);
710
711        let node1 = Box::new(new_node(1));
712        l.push_back(node1);
713
714        let node2 = Box::new(new_node(2));
715        l.push_back(node2);
716
717        let node3 = Box::new(new_node(3));
718        l.push_back(node3);
719
720        count = 0;
721        for item in l.iter() {
722            count += 1;
723            println!("{}", item.value);
724        }
725        assert_eq!(count, 3);
726
727        {
728            let mut drain = l.drain();
729            assert_eq!(drain.next().unwrap().value, 3);
730            assert_eq!(drain.next().unwrap().value, 2);
731            assert_eq!(drain.next().unwrap().value, 1);
732            assert!(drain.next().is_none());
733        }
734        assert_eq!(l.get_length(), 0);
735    }
736
737    #[test]
738    fn test_iter_arc() {
739        let mut l = DLinkedList::<Arc<TestNode>, TestTag>::new();
740
741        let mut count = 0;
742        for _item in l.iter() {
743            count += 1;
744        }
745        assert_eq!(count, 0);
746
747        let node1 = Arc::new(new_node(1));
748        l.push_back(node1);
749
750        let node2 = Arc::new(new_node(2));
751        l.push_back(node2);
752
753        let node3 = Arc::new(new_node(3));
754        l.push_back(node3);
755
756        // Check order
757        let mut iter = l.iter();
758        assert_eq!(iter.next().unwrap().value, 1);
759        assert_eq!(iter.next().unwrap().value, 2);
760        assert_eq!(iter.next().unwrap().value, 3);
761        assert!(iter.next().is_none());
762
763        {
764            let mut drain = l.drain();
765            assert_eq!(drain.next().unwrap().value, 3);
766            assert_eq!(drain.next().unwrap().value, 2);
767            assert_eq!(drain.next().unwrap().value, 1);
768            assert!(drain.next().is_none());
769        }
770        assert_eq!(l.get_length(), 0);
771    }
772
773    #[test]
774    fn test_single_element_box() {
775        let mut l = DLinkedList::<Box<TestNode>, TestTag>::new();
776        let node1 = Box::new(new_node(1));
777        l.push_front(node1);
778        let del_node = l.pop_back();
779        assert!(del_node.is_some());
780        assert_eq!(del_node.unwrap().value, 1);
781        assert_eq!(0, l.get_length());
782        assert!(l.pop_back().is_none());
783
784        let mut l2 = DLinkedList::<Box<TestNode>, TestTag>::new();
785        let node2 = Box::new(new_node(2));
786        l2.push_back(node2);
787        let del_node2 = l2.pop_back();
788        assert!(del_node2.is_some());
789        assert_eq!(del_node2.unwrap().value, 2);
790        assert_eq!(0, l2.get_length());
791        assert!(l2.pop_back().is_none());
792
793        {
794            let mut drain = l.drain();
795            assert!(drain.next().is_none());
796        }
797        assert_eq!(l.get_length(), 0);
798
799        {
800            let mut drain = l2.drain();
801            assert!(drain.next().is_none());
802        }
803        assert_eq!(l2.get_length(), 0);
804    }
805
806    #[test]
807    fn test_single_element_arc() {
808        let mut l = DLinkedList::<Arc<TestNode>, TestTag>::new();
809        let node1 = Arc::new(new_node(1));
810        l.push_front(node1);
811        let del_node = l.pop_back();
812        assert!(del_node.is_some());
813        assert_eq!(0, l.get_length());
814        assert!(l.pop_back().is_none());
815
816        let mut l2 = DLinkedList::<Arc<TestNode>, TestTag>::new();
817        let node2 = Arc::new(new_node(2));
818        l2.push_back(node2);
819        let del_node2 = l2.pop_back();
820        assert!(del_node2.is_some());
821        assert_eq!(0, l2.get_length());
822        assert!(l2.pop_back().is_none());
823
824        {
825            let mut drain = l.drain();
826            assert!(drain.next().is_none());
827        }
828        assert_eq!(l.get_length(), 0);
829
830        {
831            let mut drain = l2.drain();
832            assert!(drain.next().is_none());
833        }
834        assert_eq!(l2.get_length(), 0);
835    }
836
837    #[test]
838    fn test_drop_box_implementation() {
839        // Reset the counter before the test
840        ACTIVE_NODE_COUNT.store(0, Ordering::SeqCst);
841
842        {
843            let mut l = DLinkedList::<Box<TestNode>, TestTag>::new();
844
845            let node1 = Box::new(new_node(1));
846            l.push_back(node1);
847
848            let node2 = Box::new(new_node(2));
849            l.push_back(node2);
850
851            let node3 = Box::new(new_node(3));
852            l.push_back(node3);
853
854            assert_eq!(l.get_length(), 3);
855            assert_eq!(ACTIVE_NODE_COUNT.load(Ordering::SeqCst), 3);
856        } // `l` goes out of scope here, triggering DLinkedList's Drop, which drains and drops nodes.
857
858        // All nodes should have been dropped
859        assert_eq!(ACTIVE_NODE_COUNT.load(Ordering::SeqCst), 0);
860    }
861
862    #[test]
863    fn test_raw_pointer_list() {
864        // Reset the counter before the test
865        ACTIVE_NODE_COUNT.store(0, Ordering::SeqCst);
866
867        // Manually create nodes as raw pointers
868        let node1 = Box::into_raw(Box::new(new_node(10)));
869        let node2 = Box::into_raw(Box::new(new_node(20)));
870
871        assert_eq!(ACTIVE_NODE_COUNT.load(Ordering::SeqCst), 2);
872
873        {
874            let mut l = DLinkedList::<*const TestNode, TestTag>::new();
875            l.push_back(node1);
876            l.push_back(node2);
877
878            let mut iter = l.iter();
879            assert_eq!(iter.next().unwrap().value, 10);
880            assert_eq!(iter.next().unwrap().value, 20);
881            assert!(iter.next().is_none());
882        } // l dropped here. Because P is *const TestNode, needs_drop is false, so drain is NOT called.
883
884        // Nodes should still exist
885        assert_eq!(ACTIVE_NODE_COUNT.load(Ordering::SeqCst), 2);
886
887        unsafe {
888            // Check values
889            assert_eq!((*node1).value, 10);
890            assert_eq!((*node2).value, 20);
891
892            // Clean up
893            drop(Box::from_raw(node1));
894            drop(Box::from_raw(node2));
895        }
896
897        assert_eq!(ACTIVE_NODE_COUNT.load(Ordering::SeqCst), 0);
898    }
899
900    #[test]
901    fn test_non_null_list() {
902        // Reset the counter before the test
903        ACTIVE_NODE_COUNT.store(0, Ordering::SeqCst);
904
905        // Manually create nodes
906        // Box::leak returns &mut T, which helps creating NonNull
907        let node1 = Box::leak(Box::new(new_node(100)));
908        let node2 = Box::leak(Box::new(new_node(200)));
909
910        let ptr1 = NonNull::from(node1);
911        let ptr2 = NonNull::from(node2);
912
913        assert_eq!(ACTIVE_NODE_COUNT.load(Ordering::SeqCst), 2);
914
915        {
916            let mut l = DLinkedList::<NonNull<TestNode>, TestTag>::new();
917            l.push_back(ptr1);
918            l.push_back(ptr2);
919
920            let mut iter = l.iter();
921            assert_eq!(iter.next().unwrap().value, 100);
922            assert_eq!(iter.next().unwrap().value, 200);
923            assert!(iter.next().is_none());
924        } // l dropped here. NonNull doesn't need drop, so no drain.
925
926        // Nodes should still exist
927        assert_eq!(ACTIVE_NODE_COUNT.load(Ordering::SeqCst), 2);
928
929        unsafe {
930            // Clean up
931            drop(Box::from_raw(ptr1.as_ptr()));
932            drop(Box::from_raw(ptr2.as_ptr()));
933        }
934
935        assert_eq!(ACTIVE_NODE_COUNT.load(Ordering::SeqCst), 0);
936    }
937}