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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use core::fmt;
use core::ops::RangeBounds;
#[cfg(feature = "dev")]
use arbitrary::Arbitrary;
use order_theory::GreatestElement;
use willow_data_model::prelude as wdm;
use crate::prelude::*;
wrapper! {
/// An arbitrary non-empty box in three-dimensional willow space, consisting of a [`SubspaceRange`](super::SubspaceRange), a [`PathRange`](super::PathRange), and a [`TimeRange`](super::TimeRange).
///
/// As an application developer, you probably do not need to interact with 3d ranges, you should prefer [`Areas`](super::Area) — the latter work well with encrypted data, whereas 3d ranges do not define human-meaningful subsets of data when working with encryption.
///
/// ```
/// use willow25::prelude::*;
///
/// let r = Range3d::new(
/// SubspaceRange::full(),
/// PathRange::full(),
/// TimeRange::new_closed(0.into(), 17.into()),
/// );
///
/// assert!(r.includes(&([5; 32].into(), Path::new(), Timestamp::from(9))));
/// assert_eq!(r.subspaces(), &SubspaceRange::full());
///
/// let r2 = Range3d::new(
/// SubspaceRange::full(),
/// PathRange::full(),
/// TimeRange::new_open(15.into()),
/// );
/// assert_eq!(
/// r.intersection(&r2),
/// Ok(Range3d::new(
/// SubspaceRange::full(),
/// PathRange::full(),
/// TimeRange::new_closed(15.into(), 17.into()),
/// )),
/// );
/// ```
///
/// [Specification](https://willowprotocol.org/specs/grouping-entries/index.html#D3Range)
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd)]
#[cfg_attr(feature = "dev", derive(Arbitrary))]
Range3d; wdm::Range3d<MCL, MCC, MPL, SubspaceId>
}
impl fmt::Debug for Range3d {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl Grouping for Range3d {
fn includes<Coord>(&self, coord: &Coord) -> bool
where
Coord: Coordinatelike + ?Sized,
{
wdm::Grouping::wdm_includes(&self.0, coord)
}
fn intersection(
&self,
other: &Self,
) -> Result<Self, willow_data_model::prelude::EmptyGrouping> {
wdm::Grouping::wdm_intersection(&self.0, &other.0).map(Into::into)
}
}
impl RangeBounds<SubspaceId> for Range3d {
fn start_bound(&self) -> core::ops::Bound<&SubspaceId> {
self.0.subspaces().start_bound()
}
fn end_bound(&self) -> core::ops::Bound<&SubspaceId> {
self.0.subspaces().end_bound()
}
}
impl RangeBounds<Path> for Range3d {
fn start_bound(&self) -> core::ops::Bound<&Path> {
self.0.paths().start_bound().map(Into::into)
}
fn end_bound(&self) -> core::ops::Bound<&Path> {
self.0.paths().end_bound().map(Into::into)
}
}
impl RangeBounds<Timestamp> for Range3d {
fn start_bound(&self) -> core::ops::Bound<&Timestamp> {
self.0.times().start_bound()
}
fn end_bound(&self) -> core::ops::Bound<&Timestamp> {
self.0.times().end_bound()
}
}
impl Range3d {
/// Creates a new `Range3d` from its constituent [`SubspaceRange`](super::SubspaceRange), [`PathRange`](super::PathRange), and [`TimeRange`](super::TimeRange).
///
/// ```
/// use willow25::prelude::*;
///
/// let r = Range3d::new(
/// SubspaceRange::new_open([5; 32].into()),
/// PathRange::full(),
/// TimeRange::new_closed(0.into(), 17.into()),
/// );
///
/// assert!(r.includes(&([6; 32].into(), Path::new(), Timestamp::from(9))));
/// assert_eq!(r.subspaces(), &SubspaceRange::new_open([5; 32].into()));
/// ```
pub fn new<SR, PR, TR>(subspaces: SR, paths: PR, times: TR) -> Self
where
SR: Into<SubspaceRange>,
PR: Into<PathRange>,
TR: Into<TimeRange>,
{
wdm::Range3d::<MCL, MCC, MPL, SubspaceId>::new(
subspaces,
paths.into().map(Into::into),
times,
)
.into()
}
/// Returns a reference to the inner [`SubspaceRange`](super::SubspaceRange).
///
/// ```
/// use willow25::prelude::*;
///
/// let r = Range3d::new(
/// SubspaceRange::new_open([5; 32].into()),
/// PathRange::full(),
/// TimeRange::new_closed(0.into(), 17.into()),
/// );
/// assert_eq!(r.subspaces(), &SubspaceRange::new_open([5; 32].into()));
/// ```
///
/// [Definition](https://willowprotocol.org/specs/grouping-entries/index.html#D3RangeSubspace).
pub fn subspaces(&self) -> &SubspaceRange {
self.0.subspaces()
}
/// Returns a reference to the inner [`PathRange`](super::PathRange).
///
/// ```
/// use willow25::prelude::*;
///
/// let r = Range3d::new(
/// SubspaceRange::new_open([5; 32].into()),
/// PathRange::full(),
/// TimeRange::new_closed(0.into(), 17.into()),
/// );
/// assert_eq!(r.paths(), &WillowRange::full());
/// ```
///
/// [Definition](https://willowprotocol.org/specs/grouping-entries/index.html#D3RangePath).
pub fn paths(&self) -> &PathRange {
unsafe { self.0.paths().cast() }
}
/// Returns a reference to the inner [`TimeRange`](super::TimeRange).
///
/// ```
/// use willow25::prelude::*;
///
/// let r = Range3d::new(
/// SubspaceRange::new_open([5; 32].into()),
/// PathRange::full(),
/// TimeRange::new_closed(0.into(), 17.into()),
/// );
/// assert_eq!(r.times(), &TimeRange::new_closed(0.into(), 17.into()));
/// ```
///
/// [Definition](https://willowprotocol.org/specs/grouping-entries/index.html#D3RangeTime).
pub fn times(&self) -> &TimeRange {
self.0.times()
}
/// Sets the inner [`SubspaceRange`](super::SubspaceRange).
pub fn set_subspaces<SR>(&mut self, new_range: SR)
where
SR: Into<SubspaceRange>,
{
self.0.set_subspaces(new_range);
}
/// Sets the inner [`PathRange`](super::PathRange).
pub fn set_paths<PR>(&mut self, new_range: PR)
where
PR: Into<PathRange>,
{
self.0.set_paths(new_range.into().map(Into::into));
}
/// Sets the inner [`TimeRange`](super::TimeRange).
pub fn set_times<TR>(&mut self, new_range: TR)
where
TR: Into<TimeRange>,
{
self.0.set_times(new_range)
}
/// Returns the [`Range3d`] which [includes](Range3d::includes) `coord` but no other value.
///
/// ```
/// use willow25::prelude::*;
///
/// let r = Range3d::singleton(&([5; 32].into(), Path::new(), Timestamp::from(9)));
///
/// assert!(r.includes(&([5; 32].into(), Path::new(), Timestamp::from(9))));
/// assert!(!r.includes(&([5; 32].into(), Path::new(), Timestamp::from(10))));
/// ```
pub fn singleton<Coord>(coord: &Coord) -> Self
where
Coord: Coordinatelike,
{
wdm::Range3d::<MCL, MCC, MPL, SubspaceId>::singleton(coord).into()
}
/// Returns the `Range3d` which [includes](Range3d::includes) every [coordinate](Coordinatelike).
///
/// ```
/// use willow25::prelude::*;
///
/// let r = Range3d::full();
///
/// assert!(r.includes(&([5; 32].into(), Path::new(), Timestamp::from(9))));
/// assert!(r.includes(&([5; 32].into(), Path::new(), Timestamp::from(10))));
/// ```
pub fn full() -> Self {
wdm::Range3d::<MCL, MCC, MPL, SubspaceId>::full().into()
}
/// Returns whether `self` is the full 3d range, i.e., the 3d range which [includes](Range3d::includes) every [coordinate](Coordinatelike).
///
/// ```
/// use willow25::prelude::*;
///
/// assert!(Range3d::full().is_full());
/// assert!(!Range3d::new(
/// SubspaceRange::full(),
/// PathRange::full(),
/// TimeRange::new_closed(0.into(), 17.into()),
/// ).is_full());
/// ```
pub fn is_full(&self) -> bool {
self.0.is_full()
}
}
impl GreatestElement for Range3d {
fn greatest() -> Self {
wdm::Range3d::<MCL, MCC, MPL, SubspaceId>::greatest().into()
}
}