1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::std_facade::fmt;
use crate::std_facade::Box;
#[cfg(feature = "std")]
use std::collections::HashMap;
use crate::test_runner::errors::TestCaseResult;
#[derive(Debug)]
pub struct ResultCacheKey<'a> {
value: &'a dyn fmt::Debug,
}
impl<'a> ResultCacheKey<'a> {
pub(crate) fn new(value: &'a dyn fmt::Debug) -> Self {
Self { value }
}
pub fn value_debug(&self) -> &dyn fmt::Debug {
self.value
}
}
pub trait ResultCache {
fn key(&self, key: &ResultCacheKey) -> u64;
fn put(&mut self, key: u64, result: &TestCaseResult);
fn get(&self, key: u64) -> Option<&TestCaseResult>;
}
#[cfg(feature = "std")]
#[derive(Debug, Default, Clone)]
struct BasicResultCache {
entries: HashMap<u64, TestCaseResult>,
}
#[cfg(feature = "std")]
impl ResultCache for BasicResultCache {
fn key(&self, val: &ResultCacheKey) -> u64 {
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
use std::io::{self, Write};
struct HashWriter(DefaultHasher);
impl io::Write for HashWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
let mut hash = HashWriter(DefaultHasher::default());
write!(hash, "{:?}", val).expect("Debug format returned Err");
hash.0.finish()
}
fn put(&mut self, key: u64, result: &TestCaseResult) {
self.entries.insert(key, result.clone());
}
fn get(&self, key: u64) -> Option<&TestCaseResult> {
self.entries.get(&key)
}
}
#[cfg(feature = "std")]
pub fn basic_result_cache() -> Box<dyn ResultCache> {
Box::new(BasicResultCache::default())
}
pub(crate) struct NoOpResultCache;
impl ResultCache for NoOpResultCache {
fn key(&self, _: &ResultCacheKey) -> u64 {
0
}
fn put(&mut self, _: u64, _: &TestCaseResult) {}
fn get(&self, _: u64) -> Option<&TestCaseResult> {
None
}
}
pub fn noop_result_cache() -> Box<dyn ResultCache> {
Box::new(NoOpResultCache)
}