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
use std::iter::repeat_with;
use std::sync::{RwLock, RwLockReadGuard};
pub struct FunnyBot<'a, A, R>
where
A: Send + Sync + Clone,
R: Send + Sync + 'a,
{
arguments: RwLock<Vec<A>>,
returns: RwLock<Box<dyn Iterator<Item = R> + Send + Sync + 'a>>,
}
impl<'a, A, R> FunnyBot<'a, A, R>
where
A: Send + Sync + Clone,
R: Send + Sync + 'a + Clone,
{
pub fn repeat(elt: R) -> Self {
FunnyBot::new(Box::new(std::iter::repeat(elt)))
}
}
impl<'a, A, R> FunnyBot<'a, A, R>
where
A: Send + Sync + Clone,
R: Send + Sync + 'a,
{
#[must_use]
pub fn new(returns: Box<dyn Iterator<Item = R> + Send + Sync + 'a>) -> Self {
FunnyBot {
arguments: RwLock::new(Vec::new()),
returns: RwLock::new(returns),
}
}
#[must_use]
pub fn from_single(returns: R) -> Self {
Self::from_list(vec![returns])
}
#[must_use]
pub fn from_list(returns: Vec<R>) -> Self {
Self::new(Box::new(returns.into_iter()))
}
pub fn repeat_with<F: 'a + Sync + Send + FnMut() -> R>(repeater: F) -> Self {
Self::new(Box::new(repeat_with(repeater)))
}
pub fn call(&self, a: A) -> R {
self.arguments.write().unwrap().push(a);
self.returns
.write()
.unwrap()
.next()
.expect("missing pre-programmed value for call")
}
pub fn into_args(self) -> Vec<A> {
self.arguments.into_inner().unwrap()
}
pub fn args(&self) -> RwLockReadGuard<Vec<A>> {
self.arguments.read().unwrap()
}
}