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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::{fmt::Debug, ops::Add, time::SystemTime};
pub trait Resource : Debug {
fn timestamp(&self) -> Option<SystemTime>;
fn mk_from<F, R, S>(&self, description: &str, src: S, by: F)
where R:Resource, S:AsResource<R>, F: FnOnce() -> ()
{
let src = src.as_res();
let target_time = self.timestamp();
if target_time == None || src.timestamp() > target_time {
println!("Building: {:?} from {:?}: {}", self, src, description);
by();
}
}
fn mk_from_result<E, F, R, S>(&self, description: &str, src: S, by: F) -> Result<(), E>
where R:Resource, S:AsRef<R>, F: FnOnce() -> Result<(), E>
{
let src = src.as_ref();
let target_time = self.timestamp();
if target_time == None || src.timestamp() > target_time {
println!("Building: {:?} from {:?}: {}", self, src, description);
return by()
}
Ok(())
}
}
pub trait AsResource<R> {
fn as_res(&self) -> &R;
}
impl<R> AsResource<R> for R where R:Resource {
fn as_res(&self) -> &R {
&self
}
}
impl<R> AsResource<R> for &R where R:Resource {
fn as_res(&self) -> &R {
self
}
}
impl<R> Resource for Vec<R>
where R:Resource
{
fn timestamp(&self) -> Option<SystemTime> {
timestamp(self.iter())
}
}
pub fn timestamp<T: AsResource<R>, R: Resource>(iter: impl Iterator<Item=T>) -> Option<SystemTime> {
iter.fold(None, |result, entry| {
let timestamp = entry.as_res().timestamp();
if timestamp > result {
return timestamp;
}
result
})
}
#[derive(Debug,Clone)]
pub struct Set<T> {
items: Vec<T>
}
impl<R> Add<&R> for Set<R> where R: Clone {
type Output = Set<R>;
fn add(mut self, rhs: &R) -> Self::Output {
self.items.push(rhs.clone());
self
}
}
impl<R> Add<R> for Set<R> {
type Output = Set<R>;
fn add(mut self, rhs: R) -> Self::Output {
self.items.push(rhs);
self
}
}
impl<T> AsRef<Set<T>> for Set<T> {
fn as_ref(&self) -> &Set<T> {
self
}
}
impl<T> From<Vec<T>> for Set<T> {
fn from(val: Vec<T>) -> Self {
Set { items: val }
}
}
impl<T> IntoIterator for Set<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.items.into_iter()
}
}
impl<R> Resource for Set<R> where R:Resource {
fn timestamp(&self) -> Option<SystemTime> {
self.items.timestamp()
}
}