qcollect-traits 0.4.1

Traits for being generic over collection-types.
use super::*;

use std::borrow::Borrow;
use std::collections::btree_set::{self, BTreeSet};
use std::collections::btree_map::{self, BTreeMap};
use std::collections::hash_set::{self, HashSet};
use std::collections::hash_map::{self, HashMap};
use std::collections::hash_state::HashState;
use std::hash::Hash;
use std::{cmp, slice};

// ++++++++++++++++++++ Vec ++++++++++++++++++++

impl<T> ImmutableCollection for Vec<T> {
    fn len(&self) -> usize { (*self).len() }
}

impl<T> MutableCollection for Vec<T> {}

impl<T:> GrowableCollection for Vec<T> {
    fn capacity(&self) -> usize { (*self).capacity() }
    fn reserve(&mut self, additional: usize) { (*self).reserve(additional) }
    fn reserve_exact(&mut self, additional: usize) { (*self).reserve_exact(additional) }
    fn clear(&mut self){ (*self).clear() }
}

impl<'a, T: 'a> ImmutableSequenceTypes<'a, T> for Vec<T> {
    type Iter = slice::Iter<'a, T>;
}

impl<T: 'static> ImmutableSequence<T> for Vec<T> {
    fn get<'a>(&'a self, idx: usize) -> Option<<Self as ImmutableSequenceTypes<'a, T>>::Output> { (**self).get(idx) }

    fn iter<'a>(&'a self) -> <Self as ImmutableSequenceTypes<'a, T>>::Iter { (**self).iter() }

}

impl<'a, T: 'a> MutableSequenceTypes<'a, T> for Vec<T> {
    type IterMut = slice::IterMut<'a, T>;
}

impl<T: 'static> MutableSequence<T> for Vec<T> {
    fn get_mut<'a>(&'a mut self, idx: usize) -> Option<&mut T>
        //FIXME_REMOVE where Self: ImmutableSequenceTypes<'a, T, Output = &'a T>
    { (**self).get_mut(idx) }

    fn iter_mut<'a>(&'a mut self) -> <Self as MutableSequenceTypes<'a, T>>::IterMut
        //FIXME_REMOVE where Self: ImmutableSequenceTypes<'a, T, Output = &'a T>
    { (**self).iter_mut() }

    fn swap(&mut self, a: usize, b: usize){ (**self).swap(a, b); }

    fn sort_by<F>(&mut self, compare: F)
        where F: FnMut(&T, &T) -> cmp::Ordering
    { (**self).sort_by(compare); }
}

impl<T: 'static> GrowableSequence<T> for Vec<T> {
    fn insert(&mut self, idx: usize, val: T){ (*self).insert(idx, val); }
    fn remove(&mut self, idx: usize) -> T { (*self).remove(idx) }

    fn push(&mut self, val: T){ (*self).push(val) }
    fn pop(&mut self) -> Option<T> { (*self).pop() }
}

// ++++++++++++++++++++ BTreeSet ++++++++++++++++++++

impl<T: Ord> ImmutableCollection for BTreeSet<T> {
    fn len(&self) -> usize { (*self).len() }
}

impl<T: Ord> MutableCollection for BTreeSet<T> {}

impl<T: Ord> GrowableCollection for BTreeSet<T> {
    fn capacity(&self) -> usize { (*self).len() }
    fn reserve(&mut self, _: usize) { /* */ }
    fn reserve_exact(&mut self, _: usize) { /* */  }
    fn clear(&mut self){ (*self).clear() }
}

impl<'a, T: 'a> ImmutableSetTypes<'a, T> for BTreeSet<T> 
    where T: Ord
{
    type Iter = btree_set::Iter<'a, T>;
}

impl<T: 'static, Q: ?Sized> ImmutableSet<T, Q> for BTreeSet<T> 
    where Q: Ord, T: Borrow<Q> + Ord 
{
    fn contains(&self, val: &Q) -> bool { (*self).contains(val) }
    fn iter<'a>(&'a self) -> <Self as ImmutableSetTypes<'a, T>>::Iter { (*self).iter() }
}

impl<T: 'static, Q: ?Sized> GrowableSet<T, Q> for BTreeSet<T> 
    where Q: Ord, T: Borrow<Q> + Ord
{
    fn insert(&mut self, val: T) -> bool { (*self).insert(val) }
    fn remove(&mut self, val: &Q) -> bool { (*self).remove(val) }
}

// ++++++++++++++++++++ HashSet ++++++++++++++++++++

impl<T, S> ImmutableCollection for HashSet<T, S>
    where T: Hash + Eq, S: HashState
{
    fn len(&self) -> usize { (*self).len() }
}

impl<T, S> MutableCollection for HashSet<T, S> 
    where T: Hash + Eq, S: HashState
{}

impl<T, S> GrowableCollection for HashSet<T, S>
    where T: Hash + Eq, S: HashState
{
    fn capacity(&self) -> usize { (*self).capacity() }
    fn reserve(&mut self, additional: usize) { (*self).reserve(additional) }
    fn reserve_exact(&mut self, additional: usize) { (*self).reserve(additional) }
    fn clear(&mut self){ (*self).clear() }
}

impl<'a, T: 'a> ImmutableSetTypes<'a, T> for HashSet<T> 
    where T: Hash + Eq
{
    type Iter = hash_set::Iter<'a, T>;
}

impl<T: 'static, Q: ?Sized> ImmutableSet<T, Q> for HashSet<T> 
    where Q: Hash + Eq, T: Borrow<Q> + Hash + Eq 
{
    fn contains(&self, val: &Q) -> bool { (*self).contains(val) }
    fn iter<'a>(&'a self) -> <Self as ImmutableSetTypes<'a, T>>::Iter { (*self).iter() }
}

impl<T: 'static, Q: ?Sized> GrowableSet<T, Q> for HashSet<T> 
    where Q: Hash + Eq, T: Borrow<Q> + Hash + Eq
{
    fn insert(&mut self, val: T) -> bool { (*self).insert(val) }
    fn remove(&mut self, val: &Q) -> bool { (*self).remove(val) }
}

// ++++++++++++++++++++ BTreeMap ++++++++++++++++++++

impl<K: Ord, V> ImmutableCollection for BTreeMap<K, V> {
    fn len(&self) -> usize { (*self).len() }
}

impl<K: Ord, V> MutableCollection for BTreeMap<K, V> {}

impl<K: Ord, V> GrowableCollection for BTreeMap<K, V> {
    fn capacity(&self) -> usize { (*self).len() }
    fn reserve(&mut self, _: usize) { /* */ }
    fn reserve_exact(&mut self, _: usize) { /* */  }
    fn clear(&mut self){ (*self).clear() }
}

impl<'a, K: 'a, V: 'a> ImmutableMapTypes<'a, K, V> for BTreeMap<K, V> 
    where K: Ord
{
    type Iter = btree_map::Iter<'a, K, V>;
}

impl<K: 'static, V: 'static, Q: ?Sized> ImmutableMap<K, V, Q> for BTreeMap<K, V> 
    where Q: Ord, K: Borrow<Q> + Ord
{
    fn get<'a>(&'a self, key: &Q) -> Option<<Self as ImmutableMapTypes<'a, K, V>>::OutputVal> { (*self).get(key) }
    fn iter<'a>(&'a self) -> <Self as ImmutableMapTypes<'a, K, V>>::Iter { (*self).iter() }
}

impl<'a, K: 'a, V: 'a> MutableMapTypes<'a, K, V> for BTreeMap<K, V> 
    where K: Ord
{
    type IterMut = btree_map::IterMut<'a, K, V>;
}

impl<K: 'static, V: 'static, Q: ?Sized> MutableMap<K, V, Q> for BTreeMap<K, V> 
    where Q: Ord, K: Borrow<Q> + Ord
{
    fn get_mut<'a>(&'a mut self, key: &Q) -> Option<&mut V>
        //FIXME_REMOVE where Self: ImmutableMapTypes<'a, K, V, OutputVal = &'a V>
    { (*self).get_mut(key) }

    fn iter_mut<'a>(&'a mut self) -> <Self as MutableMapTypes<'a, K, V>>::IterMut
        //FIXME_REMOVE where Self: ImmutableMapTypes<'a, K, V, OutputVal = &'a V>
    { (*self).iter_mut() }
}

impl<K: 'static, V: 'static, Q: ?Sized> GrowableMap<K, V, Q> for BTreeMap<K, V> 
    where Q: Ord, K: Borrow<Q> + Ord
{
    fn insert(&mut self, key: K, val: V) -> Option<V> { (*self).insert(key, val) }
    fn remove(&mut self, key: &Q) -> Option<V> { (*self).remove(key) }
}

// ++++++++++++++++++++ HashMap ++++++++++++++++++++

impl<K, V, S> ImmutableCollection for HashMap<K, V, S> 
    where K: Hash + Eq, S: HashState
{
    fn len(&self) -> usize { (*self).len() }
}

impl<K, V, S> MutableCollection for HashMap<K, V, S> 
    where K: Hash + Eq, S: HashState
{}

impl<K, V, S> GrowableCollection for HashMap<K, V, S> 
    where K: Hash + Eq, S: HashState
{
    fn capacity(&self) -> usize { (*self).len() }
    fn reserve(&mut self, _: usize) { /* */ }
    fn reserve_exact(&mut self, _: usize) { /* */  }
    fn clear(&mut self){ (*self).clear() }
}

impl<'a, K: 'a, V: 'a> ImmutableMapTypes<'a, K, V> for HashMap<K, V> 
    where K: Hash + Eq
{
    type Iter = hash_map::Iter<'a, K, V>;
}

impl<K: 'static, V: 'static, Q: ?Sized> ImmutableMap<K, V, Q> for HashMap<K, V> 
    where Q: Hash + Eq, K: Borrow<Q> + Hash + Eq
{
    fn get<'a>(&'a self, key: &Q) -> Option<<Self as ImmutableMapTypes<'a, K, V>>::OutputVal> { (*self).get(key) }
    fn iter<'a>(&'a self) -> <Self as ImmutableMapTypes<'a, K, V>>::Iter { (*self).iter() }
}

impl<'a, K: 'a, V: 'a> MutableMapTypes<'a, K, V> for HashMap<K, V> 
    where K: Hash + Eq
{
    type IterMut = hash_map::IterMut<'a, K, V>;
}

impl<K: 'static, V: 'static, Q: ?Sized> MutableMap<K, V, Q> for HashMap<K, V> 
    where Q: Hash + Eq, K: Borrow<Q> + Hash + Eq
{
    fn get_mut<'a>(&'a mut self, key: &Q) -> Option<&mut V>
        //FIXME_REMOVE where Self: ImmutableMapTypes<'a, K, V, OutputVal = &'a V>
    { (*self).get_mut(key) }

    fn iter_mut<'a>(&'a mut self) -> <Self as MutableMapTypes<'a, K, V>>::IterMut
        //FIXME_REMOVE where Self: ImmutableMapTypes<'a, K, V, OutputVal = &'a V>
    { (*self).iter_mut() }
}

impl<K: 'static, V: 'static, Q: ?Sized> GrowableMap<K, V, Q> for HashMap<K, V> 
    where Q: Hash + Eq, K: Borrow<Q> + Hash + Eq
{
    fn insert(&mut self, key: K, val: V) -> Option<V> { (*self).insert(key, val) }
    fn remove(&mut self, key: &Q) -> Option<V> { (*self).remove(key) }
}