Debian Codegraph @

README.md

raw symbols SCIP
1 # Common Collection Traits
2
3 <table><tr>
4 <td><a href="https://docs.rs/cc-traits">Documentation</a></td>
5 <td><a href="https://crates.io/crates/cc-traits">Crate informations</a></td>
6 <td><a href="https://github.com/timothee-haudebourg/cc-traits">Repository</a></td>
7 </tr></table>
8
9 This crate provide traits to describe common operations available on data structures.
10 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.
11
12 Here is an example of the kind of traits provided by this crate:
13 ```rust
14 /// Mutable collection where new elements can be inserted.
15 pub trait Insert: Collection {
16 /// The output of the insertion function.
17 type Output;
18
19 /// Insert a new element in the collection.
20 fn insert(&mut self, element: Self::Item) -> Self::Output;
21 }
22 ```
23
24 ## Usage
25
26 Such traits can be used to define collections with special properties,
27 independently of the actual internal data structure.
28 For instance the following code defines an `Ordered<S>` stack collection,
29 guarantying the well-sortedness of the elements in the stack.
30
31 ```rust
32 use cc_traits::{
33 Collection,
34 Back,
35 PushBack
36 };
37
38 /// Ordered stack.
39 pub struct Ordered<S> {
40 inner: S
41 }
42
43 impl<S> Ordered<S> {
44 pub fn new() -> Self where S: Default {
45 Ordered {
46 inner: S::default()
47 }
48 }
49 }
50
51 impl<S> Ordered<S> {
52 /// Push the given element on the stack iff it is grater or equal
53 /// to every other element already in the stack.
54 pub fn try_push<T>(&mut self, element: T) -> Result<(), T>
55 where
56 T: PartialOrd,
57 S: Collection<Item=T> + Back + PushBack, // `S` must be a stack providing `back` and `push_back`.
58 for<'a> S::ItemRef<'a>: PartialOrd<&'a T> // The reference type must be comparable with other reference types.
59 {
60 if self.inner.back().map(|back| back <= &element).unwrap_or(true) {
61 self.inner.push_back(element);
62 Ok(())
63 } else {
64 Err(element)
65 }
66 }
67 }
68
69 let mut vec: Ordered<Vec<i32>> = Ordered::new(); // a `Vec` is a stack so it works.
70
71 assert!(vec.try_push(1).is_ok());
72 assert!(vec.try_push(2).is_ok());
73 assert!(vec.try_push(0).is_err());
74
75 use std::collections::VecDeque;
76 let mut deque: Ordered<VecDeque<i32>> = Ordered::new(); // a `VecDeque` is also a stack.
77
78 assert!(deque.try_push(1).is_ok());
79 assert!(deque.try_push(2).is_ok());
80 assert!(deque.try_push(0).is_err());
81 ```
82
83 ## Trait aliases
84
85 By enabling the `nightly` you can get access to
86 some trait alias definitions that can be useful to reduce the
87 verbosity of your code.
88 Here is an example of such aliases defining the common interface of stacks:
89 ```rust
90 pub trait Stack<T> = Collection<Item=T> + Len + Back;
91 pub trait StackMut<T> = Stack<T> + BackMut + PushBack + PopBack;
92 ```
93
94 As of version 0.8.0, those traits are also available without the `nightly`
95 feature as regular trait definitions.
96
97 ## Standard library
98
99 By default, all the traits defined in this crate are implemented (when relevent)
100 for the standard library collections.
101 You can disable it by using the `nostd` feature.
102
103 ## Foreign implementations
104
105 In addition to the standard library,
106 traits are implemented for
107 some popular crates if you enable the feature of the same name.
108 Here are the supported crates:
109
110 - [`slab`](https://crates.io/crates/slab) providing the `Slab` collection.
111 - [`smallvec`](https://crates.io/crates/smallvec) providing the `SmallVec` collection.
112 - [`serde_json`](https://crates.io/crates/serde_json) providing the `Map<String, Value>` collection for JSON objects.
113 - [`ijson`](https://crates.io/crates/ijson) providing the `IObject` and `IArray` collections.
114
115 ## License
116
117 Licensed under either of
118
119 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
120 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
121
122 at your option.
123
124 ## Contribution
125
126 Unless you explicitly state otherwise, any contribution intentionally submitted
127 for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
128 additional terms or conditions.