pub struct Bytes(/* private fields */);Expand description
A byte count: an exact u64 inside, with a Display that picks the unit.
{} prints binary values with IEC labels (125.6 GiB), {:#} decimal
values with SI labels (134.9 GB), and jedec binary
values with JEDEC labels (125.6 GB, what Windows Explorer shows). One
decimal place by default with a trailing .0 dropped; {:.0} to {:.9}
are honoured, as are width, fill, and alignment. Values under 1 KiB print
as integers. Rounding is half away from zero, and a value that rounds up to
1024 of a unit prints as 1 of the next.
§Examples
use sys_lib::Bytes;
let ram = Bytes::new(134_876_545_024);
assert_eq!(ram.to_string(), "125.6 GiB");
assert_eq!(format!("{ram:#}"), "134.9 GB");
assert_eq!(ram.jedec().to_string(), "125.6 GB");
assert_eq!(format!("{ram:.2}"), "125.61 GiB");
assert_eq!(ram.raw(), 134_876_545_024);
assert_eq!(Bytes::new(512).to_string(), "512 B");
assert_eq!(Bytes::from_gib(128).to_string(), "128 GiB");
assert_eq!(Bytes::new(1_048_575).to_string(), "1 MiB");
assert_eq!(Bytes::MAX.to_string(), "16 EiB");Implementations§
Source§impl Bytes
impl Bytes
Sourcepub const fn from_kib(n: u64) -> Bytes
pub const fn from_kib(n: u64) -> Bytes
n KiB (1024 bytes each), saturating at Bytes::MAX.
Sourcepub const fn from_mib(n: u64) -> Bytes
pub const fn from_mib(n: u64) -> Bytes
n MiB, saturating at Bytes::MAX.
Sourcepub const fn from_gib(n: u64) -> Bytes
pub const fn from_gib(n: u64) -> Bytes
n GiB, saturating at Bytes::MAX.
use sys_lib::Bytes;
assert_eq!(Bytes::from_gib(2).raw(), 2 * 1024 * 1024 * 1024);
assert_eq!(Bytes::from_gib(u64::MAX), Bytes::MAX);Sourcepub const fn saturating_add(self, other: Bytes) -> Bytes
pub const fn saturating_add(self, other: Bytes) -> Bytes
self + other, saturating at Bytes::MAX.
Sourcepub const fn saturating_sub(self, other: Bytes) -> Bytes
pub const fn saturating_sub(self, other: Bytes) -> Bytes
self - other, saturating at zero.
Sourcepub const fn checked_add(self, other: Bytes) -> Option<Bytes>
pub const fn checked_add(self, other: Bytes) -> Option<Bytes>
self + other, or None on overflow.
Sourcepub const fn checked_sub(self, other: Bytes) -> Option<Bytes>
pub const fn checked_sub(self, other: Bytes) -> Option<Bytes>
self - other, or None when other is larger.
use sys_lib::Bytes;
assert_eq!(Bytes::new(5).checked_sub(Bytes::new(3)), Some(Bytes::new(2)));
assert_eq!(Bytes::new(3).checked_sub(Bytes::new(5)), None);Sourcepub fn percent_of(self, total: Bytes) -> Percent
pub fn percent_of(self, total: Bytes) -> Percent
The share of total this is: 0% when total is zero, at most 100%.
use sys_lib::Bytes;
assert_eq!(Bytes::new(41).percent_of(Bytes::new(125)).to_string(), "32.8%");