17 releases (2 stable)

2.0.0 May 22, 2023
1.0.0 Nov 7, 2022
0.8.1 Jul 21, 2022
0.8.0 Mar 29, 2022
0.4.0 Mar 1, 2021

#310 in Data structures

Download history 2422/week @ 2024-08-15 4160/week @ 2024-08-22 2637/week @ 2024-08-29 3147/week @ 2024-09-05 1863/week @ 2024-09-12 2631/week @ 2024-09-19 2916/week @ 2024-09-26 2914/week @ 2024-10-03 3337/week @ 2024-10-10 3126/week @ 2024-10-17 3246/week @ 2024-10-24 4293/week @ 2024-10-31 3626/week @ 2024-11-07 3309/week @ 2024-11-14 3535/week @ 2024-11-21 2793/week @ 2024-11-28

14,480 downloads per month
Used in 94 crates (5 directly)

MIT/Apache

61KB
1.5K SLoC

Common Collection Traits

Documentation Crate informations Repository

This crate provide traits to describe common operations available on data structures. This is particularly useful when building new types on top of generic data structures without relying on the actual implementation of the underlying data structure.

Here is an example of the kind of traits provided by this crate:

/// Mutable collection where new elements can be inserted.
pub trait Insert: Collection {
    /// The output of the insertion function.
    type Output;

    /// Insert a new element in the collection.
    fn insert(&mut self, element: Self::Item) -> Self::Output;
}

Usage

Such traits can be used to define collections with special properties, independently of the actual internal data structure. For instance the following code defines an Ordered<S> stack collection, guarantying the well-sortedness of the elements in the stack.

use cc_traits::{
    Collection,
    Back,
    PushBack
};

/// Ordered stack.
pub struct Ordered<S> {
    inner: S
}

impl<S> Ordered<S> {
    pub fn new() -> Self where S: Default {
        Ordered {
            inner: S::default()
        }
    }
}

impl<S> Ordered<S> {
    /// Push the given element on the stack iff it is grater or equal
    /// to every other element already in the stack.
    pub fn try_push<T>(&mut self, element: T) -> Result<(), T>
    where
        T: PartialOrd,
        S: Collection<Item=T> + Back + PushBack, // `S` must be a stack providing `back` and `push_back`.
        for<'a> S::ItemRef<'a>: PartialOrd<&'a T> // The reference type must be comparable with other reference types.
    {
        if self.inner.back().map(|back| back <= &element).unwrap_or(true) {
            self.inner.push_back(element);
            Ok(())
        } else {
            Err(element)
        }
    }
}

let mut vec: Ordered<Vec<i32>> = Ordered::new(); // a `Vec` is a stack so it works.

assert!(vec.try_push(1).is_ok());
assert!(vec.try_push(2).is_ok());
assert!(vec.try_push(0).is_err());

use std::collections::VecDeque;
let mut deque: Ordered<VecDeque<i32>> = Ordered::new(); // a `VecDeque` is also a stack.

assert!(deque.try_push(1).is_ok());
assert!(deque.try_push(2).is_ok());
assert!(deque.try_push(0).is_err());

Trait aliases

By enabling the nightly you can get access to some trait alias definitions that can be useful to reduce the verbosity of your code. Here is an example of such aliases defining the common interface of stacks:

pub trait Stack<T> = Collection<Item=T> + Len + Back;
pub trait StackMut<T> = Stack<T> + BackMut + PushBack + PopBack;

As of version 0.8.0, those traits are also available without the nightly feature as regular trait definitions.

Standard library

By default, all the traits defined in this crate are implemented (when relevent) for the standard library collections. You can disable it by using the nostd feature.

Foreign implementations

In addition to the standard library, traits are implemented for some popular crates if you enable the feature of the same name. Here are the supported crates:

  • slab providing the Slab collection.
  • smallvec providing the SmallVec collection.
  • serde_json providing the Map<String, Value> collection for JSON objects.
  • ijson providing the IObject and IArray collections.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–5MB
~11K SLoC