Advertisement
Guest User

Untitled

a guest
Mar 7th, 2017
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.88 KB | None | 0 0
  1. extern crate rayon;
  2. use rayon::prelude::*;
  3.  
  4. #[derive(Clone)]
  5. struct Store(u64);
  6.  
  7. struct Tx(u64);
  8.  
  9. impl Tx {
  10.     fn verify_and_store(&self, store: &mut Store) -> u64 {
  11.         store.0 += self.0;
  12.         self.0
  13.     }
  14. }
  15.  
  16. fn verify_and_store(store: &mut Store, txs: Vec<Tx>) {
  17.     use std::cell::RefCell;
  18.     thread_local!(static STORE: RefCell<Option<Store>> = RefCell::new(None));
  19.  
  20.     let mut result = Vec::new();
  21.  
  22.     txs.par_iter().map(|tx| {
  23.         STORE.with(|cell| {
  24.             let mut local_store = cell.borrow_mut();
  25.             if local_store.is_none() {
  26.                 *local_store = Some(store.clone());
  27.             }
  28.             tx.verify_and_store(local_store.as_mut().unwrap())
  29.         })
  30.     }).collect_into(&mut result);
  31. }
  32.  
  33. fn main() {
  34.     let txs = vec![Tx(0), Tx(1)];
  35.     let mut store = Store(0);
  36.     verify_and_store(&mut store, txs);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement