cl-traits 0.0.1

Provides traits that describe collections
Documentation

Collection Traits (cl-traits)

Yet another library to provide traits that describe collections. This is a best effort without GAT.

Many data structures have unique features that make it difficult or even impossible to create a single trait that fits all. That is why this crate provides a single method for each trait to achieve maximum flexibility and freedom.

use cl_traits::{Push, Slice};

trait GenericVector: Push<i32> + Slice<i32> {}
impl<T> GenericVector for T where T: Push<i32> + Slice<i32> {}

struct SomeCustomVector(Vec<i32>);

impl Push<i32> for SomeCustomVector {
  type PushRet = ();

  fn push(&mut self, elem: i32) -> Self::PushRet {
    self.0.push(elem);
  }
}

impl Slice<i32> for SomeCustomVector {
  type SliceWrapper = [i32];

  fn slice(&self) -> &Self::SliceWrapper {
    self.0.as_slice()
  }
}

fn main() {
  let mut v = SomeCustomVector(vec![1, 2, 3]);
  v.push(4);
  assert_eq!(v.slice(), &[1, 2, 3, 4]);
}

Derives

TODO

Future

In the future, given some context and assurance, this crate might provide more complex traits, e.g., ContiguousIndexedSparseCollection.