"Fossies" - the Fresh Open Source Software Archive

Member "ripgrep-11.0.1/grep-printer/src/counter.rs" (16 Apr 2019, 2122 Bytes) of package /linux/privat/ripgrep-11.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Rust source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 use std::io::{self, Write};
    2 
    3 use termcolor::{ColorSpec, WriteColor};
    4 
    5 /// A writer that counts the number of bytes that have been successfully
    6 /// written.
    7 #[derive(Clone, Debug)]
    8 pub struct CounterWriter<W> {
    9     wtr: W,
   10     count: u64,
   11     total_count: u64,
   12 }
   13 
   14 impl<W: Write> CounterWriter<W> {
   15     pub fn new(wtr: W) -> CounterWriter<W> {
   16         CounterWriter { wtr: wtr, count: 0, total_count: 0 }
   17     }
   18 }
   19 
   20 impl<W> CounterWriter<W> {
   21     /// Returns the total number of bytes written since construction or the
   22     /// last time `reset` was called.
   23     pub fn count(&self) -> u64 {
   24         self.count
   25     }
   26 
   27     /// Returns the total number of bytes written since construction.
   28     pub fn total_count(&self) -> u64 {
   29         self.total_count + self.count
   30     }
   31 
   32     /// Resets the number of bytes written to `0`.
   33     pub fn reset_count(&mut self) {
   34         self.total_count += self.count;
   35         self.count = 0;
   36     }
   37 
   38     /// Clear resets all counting related state for this writer.
   39     ///
   40     /// After this call, the total count of bytes written to the underlying
   41     /// writer is erased and reset.
   42     #[allow(dead_code)]
   43     pub fn clear(&mut self) {
   44         self.count = 0;
   45         self.total_count = 0;
   46     }
   47 
   48     #[allow(dead_code)]
   49     pub fn get_ref(&self) -> &W {
   50         &self.wtr
   51     }
   52 
   53     pub fn get_mut(&mut self) -> &mut W {
   54         &mut self.wtr
   55     }
   56 
   57     pub fn into_inner(self) -> W {
   58         self.wtr
   59     }
   60 }
   61 
   62 impl<W: Write> Write for CounterWriter<W> {
   63     fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
   64         let n = self.wtr.write(buf)?;
   65         self.count += n as u64;
   66         Ok(n)
   67     }
   68 
   69     fn flush(&mut self) -> Result<(), io::Error> {
   70         self.wtr.flush()
   71     }
   72 }
   73 
   74 impl<W: WriteColor> WriteColor for CounterWriter<W> {
   75     fn supports_color(&self) -> bool {
   76         self.wtr.supports_color()
   77     }
   78 
   79     fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
   80         self.wtr.set_color(spec)
   81     }
   82 
   83     fn reset(&mut self) -> io::Result<()> {
   84         self.wtr.reset()
   85     }
   86 
   87     fn is_synchronous(&self) -> bool {
   88         self.wtr.is_synchronous()
   89     }
   90 }