1
use persy::{Config, PRes, Persy};
2
use std::sync::{Condvar, Mutex};
3
use tempfile::Builder;
4

5
#[allow(dead_code)]
6
pub struct CountDown {
7
    lock: Mutex<u64>,
8
    cond: Condvar,
9
}
10

11
#[allow(dead_code)]
12
impl CountDown {
13 1
    pub fn new(count: u64) -> CountDown {
14 1
        CountDown {
15 1
            lock: Mutex::new(count),
16 1
            cond: Condvar::new(),
17 0
        }
18 1
    }
19

20 1
    pub fn wait(&self) -> PRes<bool> {
21 1
        let guard = self.lock.lock()?;
22 1
        if *guard != 0 {
23 1
            let _ = self.cond.wait(guard)?;
24
        }
25 1
        Ok(true)
26 1
    }
27

28 1
    pub fn count_down(&self) -> PRes<()> {
29 1
        let mut count = self.lock.lock()?;
30 1
        *count = (*count) - 1;
31 1
        if *count == 0 {
32 1
            self.cond.notify_all();
33
        }
34 1
        Ok(())
35 1
    }
36
}
37

38
#[allow(dead_code)]
39 1
pub fn create_and_drop<F>(name: &str, test: F)
40
where
41
    F: FnOnce(&Persy),
42
{
43 1
    create_and_drop_with_config(name, Config::new(), test);
44 1
}
45

46
#[allow(dead_code)]
47 1
pub fn create_and_drop_with_config<F>(name: &str, config: Config, test: F)
48
where
49
    F: FnOnce(&Persy),
50
{
51 1
    let file = Builder::new()
52
        .prefix(name)
53
        .suffix(".persy")
54
        .tempfile()
55
        .expect("expect temp file creation");
56 1
    Persy::create_from_file(file.reopen().expect("reopen")).expect(&format!("file '{:?}' do not exist", file));
57
    {
58 1
        let persy = Persy::open_from_file(file.reopen().expect("reopen"), config).unwrap();
59 1
        test(&persy);
60 1
    }
61 1
}

Read our documentation on viewing source code .

Loading