prodash/unit/
bytes.rs

1use std::fmt;
2
3use crate::{progress::Step, unit::DisplayValue};
4
5/// A marker for formatting numbers as bytes in renderers.
6#[derive(Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Debug)]
7pub struct Bytes;
8
9impl Bytes {
10    fn format_bytes(w: &mut dyn fmt::Write, value: Step) -> fmt::Result {
11        let string = bytesize::ByteSize(value as u64).display().si().to_string();
12        for token in string.split(' ') {
13            w.write_str(token)?;
14        }
15        Ok(())
16    }
17}
18
19impl DisplayValue for Bytes {
20    fn display_current_value(&self, w: &mut dyn fmt::Write, value: Step, _upper: Option<Step>) -> fmt::Result {
21        Self::format_bytes(w, value)
22    }
23    fn display_upper_bound(&self, w: &mut dyn fmt::Write, upper_bound: Step, _value: Step) -> fmt::Result {
24        Self::format_bytes(w, upper_bound)
25    }
26
27    fn dyn_hash(&self, state: &mut dyn std::hash::Hasher) {
28        state.write(&[])
29    }
30
31    fn display_unit(&self, _w: &mut dyn fmt::Write, _value: Step) -> fmt::Result {
32        Ok(())
33    }
34}