use std::sync::Arc;
use super::File;
use crate::{error::Error, limits::Charge, once::ResultCell};
pub(super) struct FileCell {
value: ResultCell<File, Error>,
next: Option<Pending>,
}
impl FileCell {
pub(super) fn new() -> Self {
Self {
value: ResultCell::new(),
next: None,
}
}
}
impl std::ops::Deref for FileCell {
type Target = ResultCell<File, Error>;
fn deref(&self) -> &Self::Target {
&self.value
}
}
pub(super) struct Pending {
entries: Box<[FileCell]>,
_charge: Charge,
}
fn take(entries: &mut Box<[FileCell]>, charge: &mut Charge) -> Pending {
let zero = Charge::zero(charge.meter());
Pending {
entries: std::mem::take(entries),
_charge: std::mem::replace(charge, zero),
}
}
pub(super) fn drain(entries: &mut Box<[FileCell]>, charge: &Charge) {
let mut current = Pending {
entries: std::mem::take(entries),
_charge: Charge::zero(charge.meter()),
};
loop {
let mut next = current
.entries
.first_mut()
.and_then(|cell| cell.next.take());
for cell in &mut current.entries {
let file = match cell.value.take() {
Some(Ok(file)) => file,
Some(Err(kept)) => {
let (mut error, _charge) = kept.into_parts();
while let Error::Entry { source, .. } = error {
let Some(inner) = Arc::into_inner(source) else {
break;
};
error = inner;
}
continue;
}
None => continue,
};
let child =
match file {
File::Bundle(file) => Arc::into_inner(file)
.map(|mut file| take(&mut file.entries, &mut file.charge)),
File::Web(file) => Arc::into_inner(file)
.map(|mut file| take(&mut file.entries, &mut file.charge)),
File::Serialized(_) | File::Raw(_) => None,
};
if let Some(mut child) = child
&& let Some(first) = child.entries.first_mut()
{
first.next = next;
next = Some(child);
}
}
drop(current);
let Some(pending) = next else { break };
current = pending;
}
}