use std::io;
mod serde;
pub(crate) use self::serde::*;
mod whileok;
pub(crate) use self::whileok::*;
mod readerveciter;
pub(crate) use self::readerveciter::*;
pub struct CounterWriter {
pub count: u64,
}
impl CounterWriter {
pub fn new() -> Self {
CounterWriter { count: 0 }
}
}
impl io::Write for CounterWriter {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.count += bytes.len() as u64;
Ok(bytes.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
pub(crate) fn substitute_err_not_found<T, F>(
res: io::Result<T>,
f: F,
) -> io::Result<T>
where
F: Fn() -> T,
{
match res {
Err(ref e) if e.kind() == io::ErrorKind::NotFound => Ok(f()),
res => res,
}
}
pub(crate) struct EnumerateU64<I> {
iter: I,
count: u64,
}
impl<I> EnumerateU64<I> {
pub(crate) fn new(i: I) -> Self {
Self { iter: i, count: 0 }
}
}
impl<I> Iterator for EnumerateU64<I>
where
I: Iterator,
{
type Item = (u64, <I as Iterator>::Item);
#[inline]
fn next(&mut self) -> Option<(u64, <I as Iterator>::Item)> {
self.iter.next().map(|a| {
let ret = (self.count, a);
self.count += 1;
ret
})
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
#[inline]
fn count(self) -> usize {
self.iter.count()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<(u64, I::Item)> {
self.iter.nth(n).map(|a| {
let i = self.count + n as u64;
self.count = i + 1;
(i, a)
})
}
}