1extern crate syncpool;
2
3use std::collections::HashMap;
4use std::mem::MaybeUninit;
5use std::pin::Pin;
6use std::sync::mpsc;
7use std::sync::mpsc::SyncSender;
8use std::thread;
9use std::time::Duration;
10use syncpool::prelude::*;
11
12const COUNT: usize = 128;
14
15static mut POOL: MaybeUninit<SyncPool<ComplexStruct>> = MaybeUninit::uninit();
18
19#[derive(Default, Debug)]
20struct ComplexStruct {
21 id: usize,
22 name: String,
23 body: Vec<String>,
24 flags: Vec<usize>,
25 children: Vec<usize>,
26 index: HashMap<usize, String>,
27 rev_index: HashMap<String, usize>,
28}
29
30unsafe fn pool_setup() -> (
32 Pin<&'static mut SyncPool<ComplexStruct>>,
33 Pin<&'static mut SyncPool<ComplexStruct>>,
34) {
35 POOL.as_mut_ptr().write(SyncPool::with_size(COUNT / 2));
36
37 (
38 Pin::new(&mut *POOL.as_mut_ptr()),
39 Pin::new(&mut *POOL.as_mut_ptr()),
40 )
41}
42
43fn main() {
45 let (pinned_producer, pinned_consumer) = unsafe { pool_setup() };
48
49 let (tx, rx) = mpsc::sync_channel(64);
51
52 thread::spawn(move || {
54 let producer = pinned_producer.get_mut();
55
56 for i in 0..COUNT {
57 run(producer, &tx, i);
58 }
59 });
60
61 let handler = thread::spawn(move || {
63 let consumer = pinned_consumer.get_mut();
64
65 for content in rx {
66 println!("Receiving struct with id: {}", content.id);
67 consumer.put(content);
68 }
69 });
70
71 handler.join().unwrap_or_default();
73
74 println!("All done...");
75}
76
77fn run(pool: &mut SyncPool<ComplexStruct>, chan: &SyncSender<Box<ComplexStruct>>, id: usize) {
78 let mut content = pool.get();
80 content.id = id;
81
82 thread::sleep(Duration::from_nanos(32));
84
85 chan.send(content).unwrap_or_default();
87}