use std::sync::atomic::{AtomicBool, Ordering};
#[derive(Default)]
#[must_use = "You should report missing chunks"]
pub struct MissingChunkReporter(AtomicBool);
impl Clone for MissingChunkReporter {
fn clone(&self) -> Self {
Self(AtomicBool::new(self.0.load(Ordering::Relaxed)))
}
}
impl MissingChunkReporter {
pub fn new(any_missing: bool) -> Self {
Self(AtomicBool::new(any_missing))
}
pub fn report_missing_chunk(&self) {
self.0.store(true, Ordering::Relaxed);
}
pub fn is_empty(&self) -> bool {
!self.any_missing()
}
pub fn any_missing(&self) -> bool {
self.0.load(Ordering::Relaxed)
}
}
impl std::iter::Sum for MissingChunkReporter {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let sum = Self::default();
for r in iter {
if r.any_missing() {
sum.report_missing_chunk();
}
}
sum
}
}