take-some
A simple library that provides a way to obtain some value from various collections.
Sometimes one simply needs to "take" an element from a collection, no matter which element will it be. If you are that one, please feel free to use the crate that aims to make your (yes, yours!) life a tiny bit easier.
Now let's see how it works.
Let's say you want to implement a hash map that is statically guaranteed by the rust's type
system to be non-empy at all the times.
The most straightforward way to do so is to declare it as a pair of an item and the rest of the
items, like struct NonEmptyHashMap<K, V, S> (((K, V), ::std::collections::HashMap<K, V,S>))
.
So far, so good. You will obviously want to create some nice user API for it, and that will
include an instantiation of you type with a normal HashMap
from the standard library. And
here shines the take-some
crate!
extern crate take_some;
use HashMap;
use ;
use TakeSome;
And that's it! Yes, it is that simple.
If you'd like to implement the trait for your custom collections, have a look at the source
code of the crate (btree_map.rs
, hash_set.rs
, vec.rs
and so forth), or take a look at an
example in the examples
directory where we implement the trait for a "foreign" type.
License: MIT/Apache-2.0