qcollect 0.3.0

Collections; Collection-Traits.
#![feature(core)]
#![feature(fixed_size_array)]
#![feature(unsafe_no_drop_flag, filling_drop)]
#![feature(associated_type_defaults)]
#![feature(slice_patterns)]
#![feature(unboxed_closures)]

extern crate qcollect_traits;
extern crate qindex_multi;
extern crate raw_vec;
extern crate rustc_serialize;
extern crate core;

pub mod traits { pub use qcollect_traits::*; }

// ++++++++++++++++++++ ArrayVecs ++++++++++++++++++++

pub mod array_vec;
pub use array_vec::ArrayVec;

/// Typedef for an `ArrayVec` with a capacity of 4.
pub type ArrayVec4<T> = ArrayVec<T, [T; 4]>;

/// Typedef for an `ArrayVec` with a capacity of 8.
pub type ArrayVec8<T> = ArrayVec<T, [T; 8]>;

/// Typedef for an `ArrayVec` with a capacity of 16.
pub type ArrayVec16<T> = ArrayVec<T, [T; 16]>;

/// Typedef for an `ArrayVec` with a capacity of 24.
pub type ArrayVec24<T> = ArrayVec<T, [T; 24]>;

/// Typedef for an `ArrayVec` with a capacity of 32.
pub type ArrayVec32<T> = ArrayVec<T, [T; 32]>;

// ++++++++++++++++++++ SmallVecs ++++++++++++++++++++

pub mod small_vec;
pub use small_vec::SmallVec;

/// Typedef for an `SmallVec` with an inline-capacity of 4.
pub type SmallVec4<T> = SmallVec<T, [T; 4]>;

/// Typedef for an `SmallVec` with an inline-capacity of 8.
pub type SmallVec8<T> = SmallVec<T, [T; 8]>;

/// Typedef for an `SmallVec` with an inline-capacity of 16.
pub type SmallVec16<T> = SmallVec<T, [T; 16]>;

/// Typedef for an `SmallVec` with an inline-capacity of 24.
pub type SmallVec24<T> = SmallVec<T, [T; 24]>;

/// Typedef for an `SmallVec` with an inline-capacity of 32.
pub type SmallVec32<T> = SmallVec<T, [T; 32]>;

// ++++++++++++++++++++ FlatMaps ++++++++++++++++++++

pub mod flat_map_adaptor;
pub use flat_map_adaptor::FlatMapAdaptor;

/// Typedef for an ordered map backed by a `Vec`.
pub type FlatMap<K: Ord, V> = FlatMapAdaptor<K, V, Vec<(K, V)>>;

/// Typedef for an ordered, lifetime-bounded map backed by a mutable slice.
pub type SliceMap<'a, K: Ord, V> = FlatMapAdaptor<K, V, &'a mut [(K, V)]>;

/// Typedef for an ordered map backed by a `SmallVec4`.
pub type SmallMap4<K: Ord, V> = FlatMapAdaptor<K, V, SmallVec4<(K, V)>>;

/// Typedef for an ordered map backed by a `SmallVec8`.
pub type SmallMap8<K: Ord, V> = FlatMapAdaptor<K, V, SmallVec8<(K, V)>>;

/// Typedef for an ordered map backed by a `SmallVec16`.
pub type SmallMap16<K: Ord, V> = FlatMapAdaptor<K, V, SmallVec16<(K, V)>>;

// ++++++++++++++++++++ FlatSets ++++++++++++++++++++

pub mod flat_set_adaptor;
pub use flat_set_adaptor::FlatSetAdaptor;

/// Typedef for an ordered map backed by a `Vec`.
pub type FlatSet<T: Ord> = FlatSetAdaptor<T, Vec<T>>;

/// Typedef for an ordered, lifetime-bounded map backed by a mutable slice.
pub type SliceSet<'a, T: Ord> = FlatSetAdaptor<T, &'a mut [T]>;

/// Typedef for an ordered map backed by a `SmallVec4`.
pub type SmallSet4<T: Ord> = FlatSetAdaptor<T, SmallVec4<T>>;

/// Typedef for an ordered map backed by a `SmallVec8`.
pub type SmallSet8<T: Ord> = FlatSetAdaptor<T, SmallVec8<T>>;

/// Typedef for an ordered map backed by a `SmallVec16`.
pub type SmallSet16<T: Ord> = FlatSetAdaptor<T, SmallVec16<T>>;

/// Typedef for an ordered map backed by a `SmallVec24`.
pub type SmallSet24<T: Ord> = FlatSetAdaptor<T, SmallVec24<T>>;

/// Typedef for an ordered map backed by a `SmallVec32`.
pub type SmallSet32<T: Ord> = FlatSetAdaptor<T, SmallVec32<T>>;

// ++++++++++++++++++++ ByValue ++++++++++++++++++++

pub mod by_value;
pub use by_value::ByValue;

// ++++++++++++++++++++ Utility ++++++++++++++++++++

use core::array::FixedSizeArray;

/// Turns all output of an iterator into a fixed array, so pattern matching works.
/// Panics if the returned array is too big.
///
/// This is identical to `iter.collect::<ArrayVec<_, $ret>>().into_fixed()`.
///
/// ```rust
/// let data = vec![2u32, 5, 10, 20];
/// let [a, b, c, d]: [_;4] = self::iter_into_fixed(data.iter());
///
/// ```
pub fn iter_into_fixed<I, A>(iter: I) -> A 
    where I: Iterator, A: FixedSizeArray<I::Item>
{
    iter.collect::<ArrayVec<_, A>>().into_fixed()
}

#[test]
fn test_iter_into_fixed(){
    let data = vec![2u32, 5, 10, 20];
    let [a, b, c, d]: [_;4] = self::iter_into_fixed(data.iter());

    assert_eq!(a, &data[0]);
    assert_eq!(b, &data[1]);
    assert_eq!(c, &data[2]);
    assert_eq!(d, &data[3]);
}