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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//! A doubly-linked list with owned nodes and a unique constraint enforced by a `HashMap`.
//!
//! As with the `HashMap` type, a `UniqueLinkedList` requires that the elements
//! implement the `Eq` and `Hash` traits. This can frequently be achieved by
//! using `#[derive(PartialEq, Eq, Hash)]`. If you implement these yourself,
//! it is important that the following property holds:
//!
//! ```text
//! k1 == k2 -> hash(k1) == hash(k2)
//! ```
//!
//! In other words, if two keys are equal, their hashes must be equal.
//!
//!
//! It is a logic error for an item to be modified in such a way that the
//! item's hash, as determined by the `Hash` trait, or its equality, as
//! determined by the `Eq` trait, changes while it is in the set. This is
//! normally only possible through `Cell`, `RefCell`, global state, I/O, or
//! unsafe code.
//!
//! The `UniqueLinkedList` allows pushing and popping elements at either end
//! in amortized constant time.

use std::borrow::Borrow;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
use std::iter::FromIterator;
use std::iter::FusedIterator;
use std::ptr::NonNull;
use std::rc::Rc;
use std::convert::AsRef;

use crate::linked_list::{IntoIter as LinkedListIntoIter, Iter as LinkedListIter, LinkedList, Node};

/// A doubly-linked list with owned nodes and a unique constraint enforced by a HashMap.
///
/// As with the `HashMap` type, a `UniqueLinkedList` requires that the elements
/// implement the `Eq` and `Hash` traits. This can frequently be achieved by
/// using `#[derive(PartialEq, Eq, Hash)]`. If you implement these yourself,
/// it is important that the following property holds:
///
/// ```text
/// k1 == k2 -> hash(k1) == hash(k2)
/// ```
///
/// In other words, if two keys are equal, their hashes must be equal.
///
///
/// It is a logic error for an item to be modified in such a way that the
/// item's hash, as determined by the `Hash` trait, or its equality, as
/// determined by the `Eq` trait, changes while it is in the set. This is
/// normally only possible through `Cell`, `RefCell`, global state, I/O, or
/// unsafe code.
///
/// The `UniqueLinkedList` allows pushing and popping elements at either end
/// in amortized constant time.
pub struct UniqueLinkedList<T> {
    pub(super) list: LinkedList<Rc<T>>,
    pub(super) map: HashMap<Rc<T>, NonNull<Node<Rc<T>>>>,
}

/// An iterator over the elements of a `UniqueLinkedList`.
///
/// This `struct` is created by the [`iter`] method on [`UniqueLinkedList`]. See its
/// documentation for more.
///
/// [`iter`]: struct.UniqueLinkedList.html#method.iter
/// [`UniqueLinkedList`]: struct.UniqueLinkedList.html
#[derive(Clone, Debug)]
pub struct Iter<'a, T: 'a> {
    pub(super) iter: LinkedListIter<'a, Rc<T>>,
}

/// An owning iterator over the elements of a `UniqueLinkedList`.
///
/// This `struct` is created by the [`into_iter`] method on [`UniqueLinkedList`][`UniqueLinkedList`]
/// (provided by the `IntoIterator` trait). See its documentation for more.
///
/// [`into_iter`]: struct.UniqueLinkedList.html#method.into_iter
/// [`UniqueLinkedList`]: struct.UniqueLinkedList.html
#[derive(Clone)]
pub struct IntoIter<T> {
    pub(super) iter: LinkedListIntoIter<Rc<T>>,
}

impl<T> Default for UniqueLinkedList<T>
    where T: Hash + Eq {
    /// Creates an empty `UniqueLinkedList<T>`.
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<T> UniqueLinkedList<T>
    where T: Hash + Eq {
    /// Creates an empty `UniqueLinkedList`.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let list: UniqueLinkedList<u32> = UniqueLinkedList::new();
    /// ```
    #[inline]
    pub fn new() -> Self {
        UniqueLinkedList {
            list: LinkedList::new(),
            map: HashMap::new(),
        }
    }

    /// Moves all elements from `other` to the end of the list.
    ///
    /// This reuses all the nodes from `other` and moves them into `self`. After
    /// this operation, `other` becomes empty.
    ///
    /// This operation computes in O(n) time and O(1) memory.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut list1 = UniqueLinkedList::new();
    /// list1.push_back('a');
    ///
    /// let mut list2 = UniqueLinkedList::new();
    /// list2.push_back('b');
    /// list2.push_back('c');
    ///
    /// list1.append(&mut list2);
    ///
    /// let mut iter = list1.iter();
    /// assert_eq!(iter.next(), Some(&'a'));
    /// assert_eq!(iter.next(), Some(&'b'));
    /// assert_eq!(iter.next(), Some(&'c'));
    /// assert!(iter.next().is_none());
    ///
    /// assert!(list2.is_empty());
    /// ```
    pub fn append(&mut self, other: &mut Self) {
        while let Some(elt) = other.pop_front() {
            self.push_back(elt);
        }
    }

    /// Provides a forward iterator.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut list: UniqueLinkedList<u32> = UniqueLinkedList::new();
    ///
    /// list.push_back(0);
    /// list.push_back(1);
    /// list.push_back(2);
    ///
    /// let mut iter = list.iter();
    /// assert_eq!(iter.next(), Some(&0));
    /// assert_eq!(iter.next(), Some(&1));
    /// assert_eq!(iter.next(), Some(&2));
    /// assert_eq!(iter.next(), None);
    /// ```
    #[inline]
    pub fn iter(&self) -> Iter<T> {
        Iter { iter: self.list.iter() }
    }

    /// Returns `true` if the `UniqueLinkedList` is empty.
    ///
    /// This operation should compute in O(1) time.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut dl = UniqueLinkedList::new();
    /// assert!(dl.is_empty());
    ///
    /// dl.push_front("foo");
    /// assert!(!dl.is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.list.is_empty()
    }

    /// Returns the length of the `UniqueLinkedList`.
    ///
    /// This operation should compute in O(1) time.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut dl = UniqueLinkedList::new();
    ///
    /// dl.push_front(2);
    /// assert_eq!(dl.len(), 1);
    ///
    /// dl.push_front(1);
    /// assert_eq!(dl.len(), 2);
    ///
    /// dl.push_back(3);
    /// assert_eq!(dl.len(), 3);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.list.len()
    }

    /// Removes all elements from the `UniqueLinkedList`.
    ///
    /// This operation should compute in O(n) time.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut dl = UniqueLinkedList::new();
    ///
    /// dl.push_front(2);
    /// dl.push_front(1);
    /// assert_eq!(dl.len(), 2);
    /// assert_eq!(dl.front(), Some(&1));
    ///
    /// dl.clear();
    /// assert_eq!(dl.len(), 0);
    /// assert_eq!(dl.front(), None);
    /// ```
    #[inline]
    pub fn clear(&mut self) {
        self.list.clear();
        self.map.clear();
    }

    /// Returns `true` if the `UniqueLinkedList` contains an element equal to the
    /// given value.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut list: UniqueLinkedList<u32> = UniqueLinkedList::new();
    ///
    /// list.push_back(0);
    /// list.push_back(1);
    /// list.push_back(2);
    ///
    /// assert_eq!(list.contains(&0), true);
    /// assert_eq!(list.contains(&10), false);
    /// ```
    pub fn contains<Q: ?Sized>(&self, x: &Q) -> bool
        where Rc<T>: Borrow<Q>, Q: Hash + Eq
    {
        self.map.contains_key(x)
    }

    /// Removes and returns the element equal to the
    /// given value if present, otherwise returns `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut list: UniqueLinkedList<u32> = UniqueLinkedList::new();
    ///
    /// list.push_back(0);
    ///
    /// assert_eq!(list.remove(&0), Some(0));
    /// assert_eq!(list.remove(&10), None);
    /// ```
    pub fn remove<Q: ?Sized>(&mut self, x: &Q) -> Option<T>
        where Rc<T>: Borrow<Q>, Q: Hash + Eq
    {
        // Remove and drop node.
        let elt = unsafe {
            let (elt, node) = self.map.remove_entry(x)?;
            self.list.unlink_node(node);
            Box::from_raw(node.as_ptr()); // This drops the node's Box.
            elt
        };
        let elt = Rc::try_unwrap(elt).ok()
            .expect("Internal contract requires a single strong reference");
        Some(elt)
    }

    /// Provides a reference to the front element, or `None` if the list is
    /// empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut dl = UniqueLinkedList::new();
    /// assert_eq!(dl.front(), None);
    ///
    /// dl.push_front(1);
    /// assert_eq!(dl.front(), Some(&1));
    /// ```
    #[inline]
    pub fn front(&self) -> Option<&T> {
        self.list.front().map(AsRef::as_ref)
    }

    /// Provides a reference to the back element, or `None` if the list is
    /// empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut dl = UniqueLinkedList::new();
    /// assert_eq!(dl.back(), None);
    ///
    /// dl.push_back(1);
    /// assert_eq!(dl.back(), Some(&1));
    /// ```
    #[inline]
    pub fn back(&self) -> Option<&T> {
        self.list.back().map(AsRef::as_ref)
    }

    /// Adds an element first in the list if it is not yet present in the list
    ///
    /// This operation should compute in amortized O(1) time.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut dl = UniqueLinkedList::new();
    ///
    /// dl.push_front(2);
    /// assert_eq!(dl.front().unwrap(), &2);
    ///
    /// dl.push_front(1);
    /// assert_eq!(dl.front().unwrap(), &1);
    /// ```
    pub fn push_front(&mut self, elt: T) {
        if self.contains(&elt) {
            return;
        }

        let elt = Rc::new(elt);
        self.list.push_front(elt.clone());
        let ptr = self.list.head.unwrap();
        self.map.insert(elt, ptr);
    }

    /// Removes the first element and returns it, or `None` if the list is
    /// empty.
    ///
    /// This operation should compute in amortized O(1) time.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut d = UniqueLinkedList::new();
    /// assert_eq!(d.pop_front(), None);
    ///
    /// d.push_front(1);
    /// d.push_front(3);
    /// assert_eq!(d.pop_front(), Some(3));
    /// assert_eq!(d.pop_front(), Some(1));
    /// assert_eq!(d.pop_front(), None);
    /// ```
    pub fn pop_front(&mut self) -> Option<T> {
        let elt = self.list.pop_front()?;
        self.map.remove(&elt)
            .expect("Internal contract requires value to be present");
        let elt = Rc::try_unwrap(elt).ok()
            .expect("Internal contract requires a single strong reference");
        Some(elt)
    }

    /// Appends an element to the back of a list if it is not yet present in the list
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut d = UniqueLinkedList::new();
    /// d.push_back(1);
    /// d.push_back(3);
    /// assert_eq!(3, *d.back().unwrap());
    /// ```
    pub fn push_back(&mut self, elt: T) {
        if self.contains(&elt) {
            return;
        }

        let elt = Rc::new(elt);
        self.list.push_back(elt.clone());
        let ptr = self.list.tail.unwrap();
        self.map.insert(elt, ptr);
    }

    /// Removes the last element from a list and returns it, or `None` if
    /// it is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use nimiq_collections::UniqueLinkedList;
    ///
    /// let mut d = UniqueLinkedList::new();
    /// assert_eq!(d.pop_back(), None);
    /// d.push_back(1);
    /// d.push_back(3);
    /// assert_eq!(d.pop_back(), Some(3));
    /// ```
    pub fn pop_back(&mut self) -> Option<T> {
        let elt = self.list.pop_back()?;
        self.map.remove(&elt)
            .expect("Internal contract requires value to be present");
        let elt = Rc::try_unwrap(elt).ok()
            .expect("Internal contract requires a single strong reference");
        Some(elt)
    }
}

impl<'a, T> Iterator for Iter<'a, T>
    where T: Hash + Eq {
    type Item = &'a T;

    #[inline]
    fn next(&mut self) -> Option<&'a T> {
        self.iter.next().map(AsRef::as_ref)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a, T> DoubleEndedIterator for Iter<'a, T>
    where T: Hash + Eq {
    #[inline]
    fn next_back(&mut self) -> Option<&'a T> {
        self.iter.next_back().map(AsRef::as_ref)
    }
}

impl<'a, T> ExactSizeIterator for Iter<'a, T>
    where T: Hash + Eq {}

impl<'a, T> FusedIterator for Iter<'a, T>
    where T: Hash + Eq {}

impl<T> Iterator for IntoIter<T>
    where T: Hash + Eq {
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<T> {
        self.iter.next().map(|elt| {
            Rc::try_unwrap(elt).ok()
                .expect("Internal contract requires a single strong reference")
        })
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<T> DoubleEndedIterator for IntoIter<T>
    where T: Hash + Eq {
    #[inline]
    fn next_back(&mut self) -> Option<T> {
        self.iter.next_back().map(|elt| {
            Rc::try_unwrap(elt).ok()
                .expect("Internal contract requires a single strong reference")
        })
    }
}

impl<T> ExactSizeIterator for IntoIter<T>
    where T: Hash + Eq {}

impl<T> FusedIterator for IntoIter<T>
    where T: Hash + Eq {}

impl<T> FromIterator<T> for UniqueLinkedList<T>
    where T: Hash + Eq {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut list = Self::new();
        list.extend(iter);
        list
    }
}

impl<T> IntoIterator for UniqueLinkedList<T>
    where T: Hash + Eq {
    type Item = T;
    type IntoIter = IntoIter<T>;

    /// Consumes the list into an iterator yielding elements by value.
    #[inline]
    fn into_iter(mut self) -> IntoIter<T> {
        self.map.clear();
        IntoIter { iter: self.list.into_iter() }
    }
}

impl<'a, T> IntoIterator for &'a UniqueLinkedList<T>
    where T: Hash + Eq {
    type Item = &'a T;
    type IntoIter = Iter<'a, T>;

    fn into_iter(self) -> Iter<'a, T> {
        self.iter()
    }
}

impl<T> Extend<T> for UniqueLinkedList<T>
    where T: Hash + Eq {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        for elt in iter {
            self.push_back(elt);
        }
    }
}

impl<'a, T: 'a + Copy> Extend<&'a T> for UniqueLinkedList<T>
    where T: Hash + Eq {
    fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
        self.extend(iter.into_iter().cloned());
    }
}

impl<T: PartialEq> PartialEq for UniqueLinkedList<T>
    where T: Hash + Eq {
    fn eq(&self, other: &Self) -> bool {
        self.len() == other.len() && self.iter().eq(other)
    }
}

impl<T: Eq> Eq for UniqueLinkedList<T>
    where T: Hash + Eq {}

impl<T: PartialOrd> PartialOrd for UniqueLinkedList<T>
    where T: Hash + Eq {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.iter().partial_cmp(other)
    }
}

impl<T: Ord> Ord for UniqueLinkedList<T>
    where T: Hash + Eq {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        self.iter().cmp(other)
    }
}

impl<T: Clone> Clone for UniqueLinkedList<T>
    where T: Hash + Eq {
    fn clone(&self) -> Self {
        self.iter().cloned().collect()
    }
}

impl<T: fmt::Debug> fmt::Debug for UniqueLinkedList<T>
    where T: Hash + Eq {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_list().entries(self).finish()
    }
}

impl<T: Hash> Hash for UniqueLinkedList<T>
    where T: Hash + Eq {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.len().hash(state);
        for elt in self {
            elt.hash(state);
        }
    }
}

// Ensure that `LinkedList` and its read-only iterators are covariant in their type parameters.
#[allow(dead_code)]
fn assert_covariance() {
    fn a<'a>(x: UniqueLinkedList<&'static str>) -> UniqueLinkedList<&'a str> {
        x
    }
    fn b<'i, 'a>(x: Iter<'i, &'static str>) -> Iter<'i, &'a str> {
        x
    }
    fn c<'a>(x: IntoIter<&'static str>) -> IntoIter<&'a str> {
        x
    }
}

unsafe impl<T: Send> Send for UniqueLinkedList<T> {}

unsafe impl<T: Sync> Sync for UniqueLinkedList<T> {}

unsafe impl<'a, T: Sync> Send for Iter<'a, T> {}

unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {}

#[cfg(test)]
mod tests {
    use std::hash::Hash;
    use std::rc::Rc;
    use std::thread;
    use std::vec::Vec;

    use super::{Node, UniqueLinkedList};

    #[cfg(test)]
    fn list_from<T: Clone + Hash + Eq>(v: &[T]) -> UniqueLinkedList<T> {
        v.iter().cloned().collect()
    }

    pub fn check_links<T>(list: &UniqueLinkedList<T>) {
        let list = &list.list;
        unsafe {
            let mut len = 0;
            let mut last_ptr: Option<&Node<Rc<T>>> = None;
            let mut node_ptr: &Node<Rc<T>>;
            match list.head {
                None => {
                    // tail node should also be None.
                    assert!(list.tail.is_none());
                    assert_eq!(0, list.len);
                    return;
                }
                Some(node) => node_ptr = &*node.as_ptr(),
            }
            loop {
                match (last_ptr, node_ptr.prev) {
                    (None, None) => {}
                    (None, _) => panic!("prev link for head"),
                    (Some(p), Some(pptr)) => {
                        assert_eq!(p as *const Node<Rc<T>>, pptr.as_ptr() as *const Node<Rc<T>>);
                    }
                    _ => panic!("prev link is none, not good"),
                }
                match node_ptr.next {
                    Some(next) => {
                        last_ptr = Some(node_ptr);
                        node_ptr = &*next.as_ptr();
                        len += 1;
                    }
                    None => {
                        len += 1;
                        break;
                    }
                }
            }

            // verify that the tail node points to the last node.
            let tail = list.tail.as_ref().expect("some tail node").as_ref();
            assert_eq!(tail as *const Node<Rc<T>>, node_ptr as *const Node<Rc<T>>);
            // check that len matches interior links.
            assert_eq!(len, list.len);
        }
    }

    #[test]
    fn test_append() {
        // Empty to empty
        {
            let mut m = UniqueLinkedList::<i32>::new();
            let mut n = UniqueLinkedList::new();
            m.append(&mut n);
            check_links(&m);
            assert_eq!(m.len(), 0);
            assert_eq!(n.len(), 0);
        }
        // Non-empty to empty
        {
            let mut m = UniqueLinkedList::new();
            let mut n = UniqueLinkedList::new();
            n.push_back(2);
            m.append(&mut n);
            check_links(&m);
            assert_eq!(m.len(), 1);
            assert_eq!(m.pop_back(), Some(2));
            assert_eq!(n.len(), 0);
            check_links(&m);
        }
        // Empty to non-empty
        {
            let mut m = UniqueLinkedList::new();
            let mut n = UniqueLinkedList::new();
            m.push_back(2);
            m.append(&mut n);
            check_links(&m);
            assert_eq!(m.len(), 1);
            assert_eq!(m.pop_back(), Some(2));
            check_links(&m);
        }

        // Non-empty to non-empty
        let v = vec![1, 2, 3, 4, 5];
        let u = vec![9, 8, 1, 2, 3, 4, 5];
        let mut m = list_from(&v);
        let mut n = list_from(&u);
        m.append(&mut n);
        check_links(&m);
        let sum = vec![1, 2, 3, 4, 5, 9, 8];
        assert_eq!(sum.len(), m.len());
        for elt in sum {
            assert_eq!(m.pop_front(), Some(elt))
        }
        assert_eq!(n.len(), 0);
        // let's make sure it's working properly, since we
        // did some direct changes to private members
        n.push_back(7);
        assert_eq!(n.len(), 1);
        assert_eq!(n.pop_front(), Some(7));
        check_links(&n);
    }

    #[test]
    #[cfg_attr(target_os = "emscripten", ignore)]
    fn test_send() {
        let n = list_from(&[1, 2, 3]);
        thread::spawn(move || {
            check_links(&n);
            let a: &[_] = &[&1, &2, &3];
            assert_eq!(a, &*n.iter().collect::<Vec<_>>());
        })
            .join()
            .ok()
            .unwrap();
    }

    #[test]
    fn it_can_correctly_pop_elements() {
        let mut q = UniqueLinkedList::new();

        assert_eq!(q.len(), 0);

        q.push_back(3);
        q.push_back(1);
        q.push_back(2);

        assert_eq!(q.len(), 3);
        assert_eq!(q.pop_front(), Some(3));
        assert_eq!(q.len(), 2);
        assert_eq!(q.pop_front(), Some(1));
        assert_eq!(q.len(), 1);
        assert_eq!(q.pop_front(), Some(2));
        assert_eq!(q.len(), 0);
    }

    #[test]
    fn it_can_clear_itself() {
        let mut q = UniqueLinkedList::new();

        assert_eq!(q.len(), 0);

        q.push_front(3);
        q.push_front(1);
        q.push_front(2);

        assert_eq!(q.len(), 3);
        assert_eq!(q.is_empty(), false);
        q.clear();
        assert_eq!(q.len(), 0);
        assert_eq!(q.pop_front(), None);
        assert_eq!(q.pop_back(), None);
        assert_eq!(q.is_empty(), true);
    }

    #[test]
    fn it_can_peek() {
        let mut q = UniqueLinkedList::new();

        q.push_back(1);
        q.push_front(3);

        assert_eq!(q.front(), Some(&3));
        assert_eq!(q.back(), Some(&1));
        q.pop_front();
        assert_eq!(q.front(), Some(&1));
        assert_eq!(q.back(), Some(&1));
        q.pop_back();
        assert_eq!(q.front(), None);
        assert_eq!(q.back(), None);
    }

    #[test]
    fn it_can_push_unique() {
        let mut q = UniqueLinkedList::new();

        q.push_front(3);
        q.push_back(1);
        q.push_front(2);
        q.push_back(3);
        q.push_front(2);

        assert_eq!(q.len(), 3);
        assert_eq!(q.pop_front(), Some(2));
        assert_eq!(q.pop_front(), Some(3));
        assert_eq!(q.pop_front(), Some(1));
    }

    #[test]
    fn it_can_remove() {
        let mut q = UniqueLinkedList::new();
        q.extend(vec![3, 1, 2, 4, 5]);
        assert_eq!(q.remove(&2), Some(2));
        assert_eq!(q.remove(&3), Some(3));
        assert_eq!(q.remove(&5), Some(5));
        assert_eq!(q.remove(&9), None);
        assert_eq!(q.len(), 2);

        assert_eq!(q.pop_front(), Some(1));
        assert_eq!(q.pop_front(), Some(4));
    }

    #[test]
    fn it_can_test_contains() {
        let mut q = UniqueLinkedList::new();
        q.extend(vec![3, 1, 2, 4, 5]);

        assert_eq!(q.contains(&1), true);
        assert_eq!(q.contains(&8), false);
    }
}