Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate rayon;
- use rayon::prelude::*;
- #[derive(Clone)]
- struct Store(u64);
- struct Tx(u64);
- impl Tx {
- fn verify_and_store(&self, store: &mut Store) -> u64 {
- store.0 += self.0;
- self.0
- }
- }
- fn verify_and_store(store: &mut Store, txs: Vec<Tx>) {
- use std::cell::RefCell;
- thread_local!(static STORE: RefCell<Option<Store>> = RefCell::new(None));
- let mut result = Vec::new();
- txs.par_iter().map(|tx| {
- STORE.with(|cell| {
- let mut local_store = cell.borrow_mut();
- if local_store.is_none() {
- *local_store = Some(store.clone());
- }
- tx.verify_and_store(local_store.as_mut().unwrap())
- })
- }).collect_into(&mut result);
- }
- fn main() {
- let txs = vec![Tx(0), Tx(1)];
- let mut store = Store(0);
- verify_and_store(&mut store, txs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement