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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
// This file is part of GAnarchy - decentralized development hub
// Copyright (C) 2021 Soni L.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! A wrapper for [`DataSource`] that automatically handles
//! [`OverridableKind`].
use std::collections::BTreeSet;
use std::collections::HashSet;
use std::error;
use std::time::Duration;
use impl_trait::impl_trait;
use super::DataSourceBase;
use super::DataSource;
use super::Kind;
use super::OverridableKind;
/// A wrapper for [`DataSource`] that automatically handles
/// [`OverridableKind`].
pub struct EffectiveDataSource<T: DataSourceBase>(T);
/// A wrapper for [`OverridableKind`] that acts like the key for Eq/Hash/Ord.
#[repr(transparent)]
#[derive(Debug)]
pub struct EffectiveKind<T: OverridableKind>(pub T);
impl<T: Kind<Values=BTreeSet<T>> + OverridableKind> Kind for EffectiveKind<T> {
type Values = BTreeSet<Self>;
}
impl_trait! {
impl<T: Kind + OverridableKind> EffectiveKind<T> where Self: Kind {
impl trait OverridableKind {
type Key = T::Key;
fn as_key(&self) -> &Self::Key {
self.0.as_key()
}
}
impl trait PartialEq {
fn eq(&self, other: &Self) -> bool {
self.as_key().eq(other.as_key())
}
fn ne(&self, other: &Self) -> bool {
self.as_key().ne(other.as_key())
}
}
impl trait Eq {
}
impl trait PartialOrd {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl trait Ord {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_key().cmp(other.as_key())
}
}
impl trait std::hash::Hash {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_key().hash(state);
}
}
}
}
/// Hack to provide appropriate impls for [`EffectiveDataSource`].
pub trait EffectiveDataSourceHelper<K: Kind<Values=V>, V> {
/// Returns the values associated with this kind.
fn get_values_impl(&self) -> K::Values;
}
impl_trait! {
impl<T: DataSourceBase> EffectiveDataSource<T> {
/// Wraps the given [`DataSource`].
pub fn new(source: T) -> Self {
Self(source)
}
/// Unwraps and returns the inner [`DataSource`].
pub fn into_inner(self) -> T {
self.0
}
/// Forwards to the inner [`DataSourceBase`].
impl trait DataSourceBase {
fn update(&mut self) -> (
Duration,
Result<(), Box<dyn error::Error + Send + Sync + 'static>>,
) {
self.0.update()
}
fn exists(&self) -> bool {
self.0.exists()
}
}
/// Filters the inner [`DataSource`] using the appropriate impl.
impl trait<K: Kind> DataSource<K>
where
Self: EffectiveDataSourceHelper<K, K::Values>
{
fn get_values(&self) -> K::Values {
self.get_values_impl()
}
}
/// Filters the inner [`DataSource`] such that only the first instance
/// of a given key is returned.
impl trait<K> EffectiveDataSourceHelper<K, Vec<K>>
where
K: Kind<Values=Vec<K>> + OverridableKind,
T: DataSource<K>
{
fn get_values_impl(&self) -> Vec<K> {
let mut values: Vec<K> = self.0.get_values();
let mut seen = HashSet::<&K::Key>::new();
let mut keep = Vec::with_capacity(values.len());
for value in &values {
keep.push(seen.insert(value.as_key()));
}
drop(seen);
let mut keep = keep.into_iter();
values.retain(|_| keep.next().unwrap());
values
}
}
/// Forwards to the inner [`DataSource`] as there's no filtering we
/// can/need to do here.
impl trait<K> EffectiveDataSourceHelper<
EffectiveKind<K>,
BTreeSet<EffectiveKind<K>>,
>
where
K: Kind<Values=BTreeSet<K>> + OverridableKind,
T: DataSource<EffectiveKind<K>>,
EffectiveKind<K>: Kind<Values=BTreeSet<EffectiveKind<K>>>,
EffectiveKind<K>: OverridableKind,
{
fn get_values_impl(&self) -> BTreeSet<EffectiveKind<K>> {
self.0.get_values()
}
}
/// Forwards to the inner [`DataSource`].
impl trait<K> EffectiveDataSourceHelper<K, Option<K>>
where
K: Kind<Values=Option<K>>,
T: DataSource<K>
{
fn get_values_impl(&self) -> Option<K> {
self.0.get_values()
}
}
}
}
|