table 0.4.0

A specialized map for storing values of varying types.
Documentation
// 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)
    }
}