Skip to content

Commit 16e5711

Browse files
committed
replace the Zeroable struct with Option
1 parent b6b06ac commit 16e5711

File tree

2 files changed

+3
-60
lines changed

2 files changed

+3
-60
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ maintenance = { status = "actively-developed" }
2020

2121
[dependencies]
2222
twox-hash = "1.1"
23-
serde_derive = "1.0"
2423
serde = { version = "1.0", features = ["derive"] }
2524
rand = { version = "0.7", features = ["small_rng"] }
2625
packed_simd = { version = "0.3", features = ["into_bits"] }

src/top.rs

+3-59
Original file line numberDiff line numberDiff line change
@@ -152,76 +152,20 @@ impl<'a, A: Hash + Eq + Clone + Debug, C: Ord + Debug + 'a> Debug for TopIter<'a
152152
}
153153
}
154154

155-
/// For the result of a `std::iter::sum()` when an additive identity (i.e. zero) can't be constructed (in the case where we're summing an empty iterator).
156-
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
157-
pub enum Zeroable<T> {
158-
/// Zero
159-
Zero,
160-
/// Nonzero
161-
Nonzero(T),
162-
}
163-
#[allow(clippy::missing_errors_doc)]
164-
impl<T> Zeroable<T> {
165-
/// Transform to a `Result<T, E>`.
166-
pub fn ok_or<E>(self, err: E) -> Result<T, E> {
167-
match self {
168-
Zeroable::Nonzero(v) => Ok(v),
169-
Zeroable::Zero => Err(err),
170-
}
171-
}
172-
/// Converts to an `Option<T>`.
173-
pub fn nonzero(self) -> Option<T> {
174-
match self {
175-
Zeroable::Nonzero(v) => Some(v),
176-
Zeroable::Zero => None,
177-
}
178-
}
179-
}
180-
impl<T> From<Option<T>> for Zeroable<T> {
181-
fn from(a: Option<T>) -> Self {
182-
match a {
183-
Some(a) => Zeroable::Nonzero(a),
184-
None => Zeroable::Zero,
185-
}
186-
}
187-
}
188-
impl<T> iter::Sum for Zeroable<T>
189-
where
190-
Self: iter::Sum<T>,
191-
{
192-
fn sum<I>(iter: I) -> Self
193-
where
194-
I: Iterator<Item = Self>,
195-
{
196-
iter.filter_map(|item| {
197-
if let Zeroable::Nonzero(item) = item {
198-
Some(item)
199-
} else {
200-
None
201-
}
202-
})
203-
.sum()
204-
}
205-
}
206-
207155
impl<
208156
A: Hash + Eq + Clone,
209157
C: Ord + New + Clone + for<'a> ops::AddAssign<&'a C> + for<'a> UnionAssign<&'a C> + Intersect,
210-
> iter::Sum<Top<A, C>> for Zeroable<Top<A, C>>
158+
> iter::Sum<Top<A, C>> for Option<Top<A, C>>
211159
{
212160
fn sum<I>(mut iter: I) -> Self
213161
where
214162
I: Iterator<Item = Top<A, C>>,
215163
{
216-
let mut total = if let Some(total) = iter.next() {
217-
total
218-
} else {
219-
return Zeroable::Zero;
220-
};
164+
let mut total = iter.next()?;
221165
for sample in iter {
222166
total += sample;
223167
}
224-
Zeroable::Nonzero(total)
168+
Some(total)
225169
}
226170
}
227171
impl<

0 commit comments

Comments
 (0)