#utility

consumable_vec

Consumable_Vec is a generic approach to create a mutual database for multiple producers and consumers

1 unstable release

0.4.0 Sep 16, 2021

#162 in Concurrency

Custom license

13KB
199 lines

Consumable_Vec   Build Status

Consumable_Vec is a generic approach to create a mutual database for multiple producers and consumers


Concept

The crate offers a trait for data consumption as well as an implementation for shared and unshared consumable vectors. In both cases the idea is that producers can add data to a Vector at any time. When this data is consumed it is then removed from the datapool. When using the unshared implementation, the caller has to take care of the ownership of the data to allow mutable access to it. When using the shared implementation, every user does get a Clone of the consumable_vec to add or consume data.

 use consumable_vec::{SharedConsumableVec, Consumable};
 use std::thread;


 int main() {

 let con_vec = SharedConsumableVec::default();

 let producer = con_vec.clone();
 let consumer = con_vec.clone();
 
 thread::spawn(move || {
     for n in 1..100 {
         producer.add(format!("Produced: {}", n));
     }   
 });

 thread::spawn(move || {
     loop {
     if let Some(consumed) = consumer.consume("Produced".to_string()) {
         println!("{:?}", consumed);
         if consumed.inner().iter().filter(|c| c.contains("99")).count() > 0 {
             break;
         }
     }   
     }
});


 }

In the examplesfolder you will find a brief example on how to use SharedConsumableVecacross threads.

To run the example:

cargo run --example shared_vec

License

Licensed under MIT license

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Consumable_Vec by you, shall be licensed as above, without any additional terms or conditions.

Dependencies

~37KB