consumable_vec

Crates.ioconsumable_vec
lib.rsconsumable_vec
version0.4.0
sourcesrc
created_at2021-09-16 15:36:45.6205
updated_at2021-09-16 15:36:45.6205
descriptionConsumable_Vec is a generic approach to create a mutual database for multiple producers and consumers
homepage
repositoryhttps://github.com/tacdom/consumable_vec
max_upload_size
id452318
size20,519
(tacdom)

documentation

README

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.

Commit count: 27

cargo fmt