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
use core::fmt;
#[cfg(feature = "dev")]
use arbitrary::Arbitrary;
use order_theory::GreatestElement;
use willow_data_model::prelude::{self as wdm, EmptyGrouping};
use crate::prelude::*;
pub use core::num::NonZeroU64;
wrapper! {
/// An [`AreaOfInterest`](https://willowprotocol.org/specs/grouping-entries/#Area) is an [`Area`] together with a maximum number of entries it may include, and a maximum sum of total [payload_lengths](https://willowprotocol.org/specs/data-model/index.html#entry_payload_length) for the included entries.
///
/// A [`max_count`](https://willowprotocol.org/specs/grouping-entries/#aoi_count) of [`u64::MAX`] indicates that there is no limit on the number of entries. A [`max_size`](https://willowprotocol.org/specs/grouping-entries/#aoi_size) of [`u64::MAX`] indicates that there is no limit on the total [payload_lengths](https://willowprotocol.org/specs/data-model/index.html#entry_payload_length) of the entries.
///
/// ```
/// use willow25::prelude::*;
///
/// let aoi = AreaOfInterest::new(
/// Area::new(None, Path::new(), TimeRange::new_closed(0.into(), 17.into())),
/// NonZeroU64::new(5).unwrap(),
/// 400,
/// );
///
/// assert_eq!(
/// aoi.area(),
/// &Area::new(None, Path::new(), TimeRange::new_closed(0.into(), 17.into())),
/// );
/// assert_eq!(aoi.max_count(), NonZeroU64::new(5).unwrap());
/// assert_eq!(aoi.max_size(), 400);
/// ```
///
/// [Specification](https://willowprotocol.org/specs/grouping-entries/#aois)
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd)]
#[cfg_attr(feature = "dev", derive(Arbitrary))]
AreaOfInterest; wdm::AreaOfInterest<MCL, MCC, MPL, SubspaceId>
}
impl fmt::Debug for AreaOfInterest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl AreaOfInterest {
/// Creates a new `AreaOfInterest` from its constituent [`Area`], [max_count](https://willowprotocol.org/specs/grouping-entries/#aoi_count), and [max_size](https://willowprotocol.org/specs/grouping-entries/#aoi_size).
///
/// A [`max_count`](https://willowprotocol.org/specs/grouping-entries/#aoi_count) of [`u64::MAX`] indicates that there is no limit on the number of entries. A [`max_size`](https://willowprotocol.org/specs/grouping-entries/#aoi_size) of [`u64::MAX`] indicates that there is no limit on the total [payload_lengths](https://willowprotocol.org/specs/data-model/index.html#entry_payload_length) of the entries.
///
/// ```
/// use willow25::prelude::*;
///
/// let aoi = AreaOfInterest::new(
/// Area::new(None, Path::new(), TimeRange::new_closed(0.into(), 17.into())),
/// NonZeroU64::new(5).unwrap(),
/// 400,
/// );;
///
/// assert_eq!(
/// aoi.area(),
/// &Area::new(None, Path::new(), TimeRange::new_closed(0.into(), 17.into())),
/// );
/// assert_eq!(aoi.max_count(), NonZeroU64::new(5).unwrap());
/// assert_eq!(aoi.max_size(), 400);
/// ```
pub fn new(area: Area, max_count: NonZeroU64, max_size: u64) -> Self {
wdm::AreaOfInterest::<MCL, MCC, MPL, SubspaceId>::new(area.into(), max_count, max_size)
.into()
}
/// Returns a reference to the inner [`Area`].
///
/// ```
/// use willow25::prelude::*;
///
/// let aoi = AreaOfInterest::new(
/// Area::new(None, Path::new(), TimeRange::new_closed(0.into(), 17.into())),
/// NonZeroU64::new(5).unwrap(),
/// 400,
/// );;
/// assert_eq!(
/// aoi.area(),
/// &Area::new(None, Path::new(), TimeRange::new_closed(0.into(), 17.into())),
/// );
/// ```
///
/// [Definition](https://willowprotocol.org/specs/grouping-entries/#aoi_area).
pub fn area(&self) -> &Area {
self.0.area().into()
}
/// Returns the [max_count](https://willowprotocol.org/specs/grouping-entries/#aoi_count) of entries.
///
/// A [`max_count`](https://willowprotocol.org/specs/grouping-entries/#aoi_count) of [`u64::MAX`] indicates that there is no limit on the number of entries in this area of interest.
///
/// ```
/// use willow25::prelude::*;
///
/// let aoi = AreaOfInterest::new(
/// Area::new(None, Path::new(), TimeRange::new_closed(0.into(), 17.into())),
/// NonZeroU64::new(5).unwrap(),
/// 400,
/// );;
/// assert_eq!(aoi.max_count(), NonZeroU64::new(5).unwrap());
/// ```
///
/// [Definition](https://willowprotocol.org/specs/grouping-entries/#AreaPath).
pub fn max_count(&self) -> NonZeroU64 {
self.0.max_count()
}
/// Returns the [max_count](https://willowprotocol.org/specs/grouping-entries/#aoi_count) of entries.
///
/// A [`max_size`](https://willowprotocol.org/specs/grouping-entries/#aoi_size) of [`u64::MAX`] indicates that there is no limit on the total [payload_lengths](https://willowprotocol.org/specs/data-model/index.html#entry_payload_length) of the entries in this area of interest.
///
/// ```
/// use willow25::prelude::*;
///
/// let aoi = AreaOfInterest::new(
/// Area::new(None, Path::new(), TimeRange::new_closed(0.into(), 17.into())),
/// NonZeroU64::new(5).unwrap(),
/// 400,
/// );;
/// assert_eq!(aoi.max_size(), 400);
/// ```
///
/// [Definition](https://willowprotocol.org/specs/grouping-entries/#AreaPath).
pub fn max_size(&self) -> u64 {
self.0.max_size()
}
/// Sets the inner area.
pub fn set_area(&mut self, new_area: Area) {
self.0.set_area(new_area.into())
}
/// Sets the [`max_count`](https://willowprotocol.org/specs/grouping-entries/#aoi_count).
///
/// A [`max_count`](https://willowprotocol.org/specs/grouping-entries/#aoi_count) of [`u64::MAX`] indicates that there is no limit on the number of entries in this area of interest.
pub fn set_max_count(&mut self, new_max_count: NonZeroU64) {
self.0.set_max_count(new_max_count);
}
/// Sets the [`max_size`](https://willowprotocol.org/specs/grouping-entries/#aoi_size).
///
/// A [`max_size`](https://willowprotocol.org/specs/grouping-entries/#aoi_size) of [`u64::MAX`] indicates that there is no limit on the total [payload_lengths](https://willowprotocol.org/specs/data-model/index.html#entry_payload_length) of the entries in this area of interest.
pub fn set_max_size(&mut self, new_max_size: u64) {
self.0.set_max_size(new_max_size);
}
}
impl AreaOfInterest {
/// Returns the [intersection](https://willowprotocol.org/specs/grouping-entries/#aoi_intersection) of `self` and `other`.
///
/// ```
/// use willow25::prelude::*;
///
/// let aoi1 = AreaOfInterest::new(
/// Area::new(None, Path::new(), TimeRange::new_closed(0.into(), 17.into())),
/// NonZeroU64::new(5).unwrap(),
/// 400,
/// );
/// let aoi2 = AreaOfInterest::new(
/// Area::new(None, Path::new(), TimeRange::new_open(14.into())),
/// NonZeroU64::new(12).unwrap(),
/// 32,
/// );
///
/// assert_eq!(
/// aoi1.intersection(&aoi2),
/// Ok(AreaOfInterest::new(
/// Area::new(
/// None,
/// Path::new(),
/// TimeRange::new_closed(14.into(), 17.into())
/// ),
/// NonZeroU64::new(5).unwrap(),
/// 32,
/// )),
/// );
/// ```
pub fn intersection(&self, other: &Self) -> Result<Self, EmptyGrouping> {
self.0.intersection(other.into()).map(Into::into)
}
}
impl GreatestElement for AreaOfInterest {
fn greatest() -> Self {
wdm::AreaOfInterest::<MCL, MCC, MPL, SubspaceId>::greatest().into()
}
fn is_greatest(&self) -> bool {
self.0.is_greatest()
}
}