pdf_create/
util.rs

1//! Generic utilities
2
3use std::io::{self, Write};
4
5/// Source: <https://stackoverflow.com/questions/42187591/>
6pub struct ByteCounter<W> {
7    inner: W,
8    count: usize,
9}
10
11impl<W> ByteCounter<W>
12where
13    W: Write,
14{
15    /// Create a new byte counter
16    pub fn new(inner: W) -> Self {
17        ByteCounter { inner, count: 0 }
18    }
19
20    /// Return the inner writer
21    pub fn into_inner(self) -> W {
22        self.inner
23    }
24
25    /// Get the number of bytes written
26    pub fn bytes_written(&self) -> usize {
27        self.count
28    }
29}
30
31impl<W> Write for ByteCounter<W>
32where
33    W: Write,
34{
35    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
36        let res = self.inner.write(buf);
37        if let Ok(size) = res {
38            self.count += size
39        }
40        res
41    }
42
43    fn flush(&mut self) -> io::Result<()> {
44        self.inner.flush()
45    }
46}
47
48pub(crate) struct NextID {
49    obj_id: u64,
50}
51
52impl NextID {
53    pub(crate) fn new(start: u64) -> Self {
54        Self { obj_id: start }
55    }
56
57    pub(crate) fn next(&mut self) -> u64 {
58        let next = self.obj_id;
59        self.obj_id += 1;
60        next
61    }
62}