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
// Copyright (C) 2018  Project Tsukurou!
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use std::iter::FromIterator;
use std::slice;
use std::vec;

use value::Value;
use list::List;

/// A borrowing iterator over the elements in a list.
pub struct Iter<'a: 'b, 'b> {
    iter: slice::Iter<'b, Value<'a>>,
}

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

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}

impl<'a, 'b> IntoIterator for &'b List<'a> {
    type Item = &'b Value<'a>;
    type IntoIter = Iter<'a, 'b>;

    fn into_iter(self) -> Self::IntoIter {
        Iter {
            iter: self.vec.iter(),
        }
    }
}

/// A mutably borrowing iterator over the elements in a list.
pub struct IterMut<'a: 'b, 'b> {
    iter: slice::IterMut<'b, Value<'a>>,
}

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

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}

impl<'a, 'b> IntoIterator for &'b mut List<'a> {
    type Item = &'b mut Value<'a>;
    type IntoIter = IterMut<'a, 'b>;

    fn into_iter(self) -> Self::IntoIter {
        IterMut {
            iter: self.vec.iter_mut(),
        }
    }
}

/// A consuming iterator over the elements in a list.
pub struct IntoIter<'a> {
    iter: vec::IntoIter<Value<'a>>,
}

impl<'a> Iterator for IntoIter<'a> {
    type Item = Value<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next()
    }
}

impl<'a> IntoIterator for List<'a> {
    type Item = Value<'a>;
    type IntoIter = IntoIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter {
            iter: self.vec.into_iter(),
        }
    }
}

impl<'a, V> FromIterator<V> for List<'a>
where
    Value<'a>: From<V>,
{
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item=V>,
    {
        let vec = iter.into_iter()
            .map(Value::from)
            .collect();

        List::with_vec(vec)
    }
}