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
use super::{Node,Link};
use rust::*;

/// An iterator over the sub `Node`s of a `Node` or `Forest`.
///
/// This `struct` is created by [`Node::iter`] and [`Forest::iter`].
/// See its document for more.
///
/// [`Node::iter`]: struct.Node.html#method.iter
/// [`Forest::iter`]: struct.Forest.html#method.iter
pub struct Iter<'a, T:'a> {
    head : *const Link,
    tail : *const Link,
    len  : usize,
    mark : PhantomData<&'a Node<T>>,
}

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

    #[inline] fn next( &mut self ) -> Option<&'a Node<T>> {
        if self.head.is_null() {
             None
        } else { unsafe {
            let node = self.head;
            self.head = if self.head == self.tail {
                null()
            } else {
                (*node).next
            };
            self.len -= 1;
            Some( &*( node as *mut Node<T> ))
        }}
    }

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

impl<'a,T> ExactSizeIterator for Iter<'a, T> {}
impl<'a,T> FusedIterator for Iter<'a, T> {}

impl<'a, T:'a> Iter<'a, T> {
    #[inline] pub(crate) fn new( head: *const Link, tail: *const Link, len: usize ) -> Self {
        Iter{ head, tail, len, mark: PhantomData }
    }
}

impl<'a, T> Clone for Iter<'a, T> {
    fn clone(&self) -> Self {
        Iter { ..*self }
    }
}

/// A mutable iterator over the sub `Node`s of a `Node` or `Forest`.
///
/// This `struct` is created by [`Node::iter_mut`] and [`Forest::iter_mut`].
///  See its document for more.
///
/// [`Node::iter_mut`]: struct.Node.html#method.iter_mut
/// [`Forest::iter_mut`]: struct.Forest.html#method.iter_mut
pub struct IterMut<'a, T:'a> {
    head : *mut Link,
    tail : *mut Link,
    len  : usize,
    mark : PhantomData<&'a mut Node<T>>,
}

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

    #[inline] fn next( &mut self ) -> Option<&'a mut Node<T>> {
        if self.head.is_null() {
             None
        } else { unsafe {
            let node = self.head;
            self.head = if self.head == self.tail {
                null_mut()
            } else {
                (*node).next
            };
            self.len -= 1;
            Some( &mut *( node as *mut Node<T> ))
        }}
    }

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

impl<'a,T> ExactSizeIterator for IterMut<'a, T> {}
impl<'a, T> FusedIterator for IterMut<'a, T> {}

impl<'a, T:'a> IterMut<'a, T> {
    #[inline] pub(crate) fn new( head: *mut Link, tail: *mut Link, len: usize ) -> Self {
        IterMut{ head, tail, len, mark: PhantomData }
    }
}

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

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