pub struct ByteFormatter { /* private fields */ }Expand description
A configurable formatter for byte sizes.
§Examples
use prettier_bytes::{ByteFormatter, Standard};
let fmt = ByteFormatter::new().standard(Standard::SI);
assert_eq!(fmt.format(2500).as_str().unwrap(), "2.50 kB");Implementations§
Source§impl ByteFormatter
impl ByteFormatter
Sourcepub const fn new() -> ByteFormatter
pub const fn new() -> ByteFormatter
Creates a new ByteFormatter with sensible defaults:
Binary standard (Base 1024), Bytes, and a space between the number and unit.
Sourcepub const fn standard(self, standard: Standard) -> ByteFormatter
pub const fn standard(self, standard: Standard) -> ByteFormatter
Sets the formatting standard (e.g., SI vs Binary).
Sourcepub const fn unit(self, unit: Unit) -> ByteFormatter
pub const fn unit(self, unit: Unit) -> ByteFormatter
Sets the base unit (e.g., Bytes vs Bits).
Sourcepub const fn space(self, space: bool) -> ByteFormatter
pub const fn space(self, space: bool) -> ByteFormatter
Determines whether a space is placed between the number and the unit.
Defaults to true (e.g., "1.50 MB"). If set to false, the space is omitted
(e.g., "1.50MB").
§Examples
use prettier_bytes::ByteFormatter;
let fmt_spaced = ByteFormatter::new().space(true);
assert_eq!(fmt_spaced.format(1024).as_str().unwrap(), "1.00 KiB");
let fmt_compact = ByteFormatter::new().space(false);
assert_eq!(fmt_compact.format(1024).as_str().unwrap(), "1.00KiB");Sourcepub fn format(self, val: u64) -> FormattedBytes
pub fn format(self, val: u64) -> FormattedBytes
Formats the given value and returns a stack-allocated buffer.
Trait Implementations§
Source§impl Clone for ByteFormatter
impl Clone for ByteFormatter
Source§fn clone(&self) -> ByteFormatter
fn clone(&self) -> ByteFormatter
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ByteFormatter
impl Debug for ByteFormatter
Source§impl Default for ByteFormatter
impl Default for ByteFormatter
Source§fn default() -> ByteFormatter
fn default() -> ByteFormatter
Returns the “default value” for a type. Read more
Source§impl PartialEq for ByteFormatter
impl PartialEq for ByteFormatter
impl Copy for ByteFormatter
impl Eq for ByteFormatter
impl StructuralPartialEq for ByteFormatter
Auto Trait Implementations§
impl Freeze for ByteFormatter
impl RefUnwindSafe for ByteFormatter
impl Send for ByteFormatter
impl Sync for ByteFormatter
impl Unpin for ByteFormatter
impl UnsafeUnpin for ByteFormatter
impl UnwindSafe for ByteFormatter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more