Skip to main content

Bytes

Struct Bytes 

Source
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

Source

pub const ZERO: Bytes

Zero bytes.

Source

pub const MAX: Bytes

The largest count, u64::MAX bytes.

Source

pub const fn new(bytes: u64) -> Bytes

A count of bytes.

Source

pub const fn raw(self) -> u64

The exact number of bytes.

Source

pub const fn from_kib(n: u64) -> Bytes

n KiB (1024 bytes each), saturating at Bytes::MAX.

Source

pub const fn from_mib(n: u64) -> Bytes

n MiB, saturating at Bytes::MAX.

Source

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);
Source

pub const fn saturating_add(self, other: Bytes) -> Bytes

self + other, saturating at Bytes::MAX.

Source

pub const fn saturating_sub(self, other: Bytes) -> Bytes

self - other, saturating at zero.

Source

pub const fn checked_add(self, other: Bytes) -> Option<Bytes>

self + other, or None on overflow.

Source

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);
Source

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%");
Source

pub fn display(self, units: Units) -> impl Display

Formats in the given unit system.

use sys_lib::{Bytes, Units};
assert_eq!(Bytes::new(1_500_000).display(Units::Decimal).to_string(), "1.5 MB");
Source

pub fn binary(self) -> impl Display

Formats with binary values and IEC labels: 125.6 GiB.

Source

pub fn decimal(self) -> impl Display

Formats with decimal values and SI labels: 134.9 GB.

Source

pub fn jedec(self) -> impl Display

Formats with binary values and JEDEC labels: 125.6 GB.

Trait Implementations§

Source§

impl Clone for Bytes

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Bytes

Source§

impl Debug for Bytes

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Bytes

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Bytes

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Bytes

Source§

impl From<Bytes> for u64

Source§

fn from(bytes: Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Bytes

Source§

fn from(bytes: u64) -> Self

Converts to this type from the input type.
Source§

impl Hash for Bytes

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Bytes

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Bytes

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Bytes

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Bytes

Auto Trait Implementations§

§

impl Forget for Bytes

§

impl Freeze for Bytes

§

impl Move for Bytes

§

impl RefUnwindSafe for Bytes

§

impl Send for Bytes

§

impl Sync for Bytes

§

impl Unpin for Bytes

§

impl UnsafeUnpin for Bytes

§

impl UnwindSafe for Bytes

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.