pub mod iteration;
pub mod query;
pub mod transform;
pub mod operation;
pub mod async_support;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Collection<T> {
data: Vec<T>,
}
impl<T> Collection<T> {
pub fn new(data: impl IntoIterator<Item = T>) -> Self {
Self {
data: data.into_iter().collect(),
}
}
#[must_use]
pub fn empty() -> Self {
Self { data: Vec::new() }
}
#[must_use]
pub fn len(&self) -> usize {
self.data.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
#[must_use]
pub fn data(&self) -> &Vec<T> {
&self.data
}
pub fn data_mut(&mut self) -> &mut Vec<T> {
&mut self.data
}
#[must_use]
pub fn get(&self, index: usize) -> Option<&T> {
self.data.get(index)
}
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
self.data.get_mut(index)
}
#[must_use]
pub fn first(&self) -> Option<&T> {
self.data.first()
}
#[must_use]
pub fn last(&self) -> Option<&T> {
self.data.last()
}
#[must_use]
pub fn into_vec(self) -> Vec<T> {
self.data
}
#[must_use]
#[allow(clippy::should_implement_trait)]
pub fn into_iter(self) -> std::vec::IntoIter<T> {
self.data.into_iter()
}
pub fn iter(&self) -> std::slice::Iter<'_, T> {
self.data.iter()
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> {
self.data.iter_mut()
}
}
impl<T> From<Vec<T>> for Collection<T> {
fn from(data: Vec<T>) -> Self {
Self { data }
}
}
impl<T> From<Collection<T>> for Vec<T> {
fn from(collection: Collection<T>) -> Self {
collection.data
}
}
impl<T> std::ops::Index<usize> for Collection<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
&self.data[index]
}
}
impl<T> std::ops::IndexMut<usize> for Collection<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.data[index]
}
}
impl<T> IntoIterator for Collection<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}
impl<'a, T> IntoIterator for &'a Collection<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.data.iter()
}
}
impl<'a, T> IntoIterator for &'a mut Collection<T> {
type Item = &'a mut T;
type IntoIter = std::slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.data.iter_mut()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_collection_creation() {
let collection = Collection::new(vec![1, 2, 3]);
assert_eq!(collection.len(), 3);
assert!(!collection.is_empty());
}
#[test]
fn test_empty_collection() {
let collection: Collection<i32> = Collection::empty();
assert_eq!(collection.len(), 0);
assert!(collection.is_empty());
}
#[test]
fn test_get() {
let collection = Collection::new(vec![1, 2, 3]);
assert_eq!(collection.get(1), Some(&2));
assert_eq!(collection.get(5), None);
}
#[test]
fn test_first_last() {
let collection = Collection::new(vec![1, 2, 3]);
assert_eq!(collection.first(), Some(&1));
assert_eq!(collection.last(), Some(&3));
}
#[test]
fn test_indexing() {
let collection = Collection::new(vec![1, 2, 3]);
assert_eq!(collection[1], 2);
}
#[test]
fn test_into_iter() {
let collection = Collection::new(vec![1, 2, 3]);
let sum: i32 = collection.into_iter().sum();
assert_eq!(sum, 6);
}
#[test]
fn test_iter() {
let collection = Collection::new(vec![1, 2, 3]);
let sum: i32 = collection.iter().sum();
assert_eq!(sum, 6);
}
}