pub struct SpecificSize<M = Any> { /* private fields */ }Expand description
SpecificSize represents a size in bytes with a multiple.
SpecificSize can be created using the new function, or parsed from a
string using the FromStr trait.
use human_size::{SpecificSize, Size, Byte, Any};
let size1 = SpecificSize::new(1000, Byte).unwrap();
assert_eq!(size1.to_string(), "1000 B");
// `Size` is a type alias for `SpecificSize<Any>`.
let size2: Size = "1 kB".parse().unwrap();
assert_eq!(size2.to_string(), "1 kB");
// Even though the multiples are different we can still compare them.
assert_eq!(size1, size2);Creating a SpecificSize with a specific Multiple, e.g. Kilobyte,
only uses 8 bytes. Using the generic mulitple, i.e. Any, it can
represent all multiples but requires an extra 8 bytes for a total of 16
bytes.
use std::mem;
use human_size::{SpecificSize, Size, Byte, Any};
assert_eq!(mem::size_of::<SpecificSize<Byte>>(), 8);
assert_eq!(mem::size_of::<Size>(), 16);§Notes
When comparing sizes with one another it is to possible compare different
multiples, see the first example above. However due to a lack of precision
in floating point numbers equality ignores a difference less then
0.00000001, after applying the multiple. See the PartialEq
implementation (via [src] to the right) for details.
The same is true for converting to and from multiples, here again the lack of precision of floating points can be a cause of bugs.
Implementations§
Source§impl<M: Multiple> SpecificSize<M>
impl<M: Multiple> SpecificSize<M>
Sourcepub fn new<V>(
value: V,
multiple: M,
) -> Result<SpecificSize<M>, InvalidValueError>
pub fn new<V>( value: V, multiple: M, ) -> Result<SpecificSize<M>, InvalidValueError>
Create a new SpecificSize with the given value and multiple. If the
value is not normal this will return an error, however zero is
allowed. If the value is normal the result can be safely unwraped.
use std::f64;
use human_size::{SpecificSize, Kilobyte, InvalidValueError};
let size = SpecificSize::new(100, Kilobyte).unwrap();
assert_eq!(size.to_string(), "100 kB");
let res = SpecificSize::new(f64::NAN, Kilobyte);
assert_eq!(res, Err(InvalidValueError)); // NAN is not a valid number.Sourcepub fn into<M2>(self) -> SpecificSize<M2>where
M2: Multiple,
pub fn into<M2>(self) -> SpecificSize<M2>where
M2: Multiple,
Conversion between sizes with different multiples.
This allows a size with one multiple to be converted into a size with another multiple.
use human_size::{SpecificSize, Byte, Kilobyte};
let size = SpecificSize::new(1, Kilobyte).unwrap();
let size2: SpecificSize<Byte> = size.into();
assert_eq!(size, size2);
assert_eq!(size.to_string(), "1 kB");
assert_eq!(size2.to_string(), "1000 B");§Notes
Normally this would be done by implementing the From or Into trait.
However currently this is not possible due to the blanket implementation
in the standard library. Maybe once specialisation is available this can
be resolved.
Sourcepub fn value(self) -> f64
pub fn value(self) -> f64
Returns the size in current the multiple.
use human_size::{SpecificSize, Kilobyte};
let size = SpecificSize::new(1, Kilobyte).unwrap();
assert_eq!(size.value(), 1.0);Sourcepub fn multiple(self) -> M
pub fn multiple(self) -> M
Returns the multiple.
use human_size::{SpecificSize, Any, Kilobyte};
let size1 = SpecificSize::new(1, Kilobyte).unwrap();
let size2 = SpecificSize::new(1, Any::Kilobyte).unwrap();
assert_eq!(size1.multiple(), Kilobyte);
assert_eq!(size2.multiple(), Any::Kilobyte);Sourcepub fn to_bytes(self) -> u64
pub fn to_bytes(self) -> u64
Returns the size as bytes.
§Notes
Be careful of truncation for large file size.
§Examples
use human_size::{SpecificSize, Any, Kilobyte};
let size1 = SpecificSize::new(1, Kilobyte).unwrap();
let size2 = SpecificSize::new(8, Any::Kilobyte).unwrap();
assert_eq!(size1.to_bytes(), 1000);
assert_eq!(size2.to_bytes(), 8000);Trait Implementations§
Source§impl<M: Clone> Clone for SpecificSize<M>
impl<M: Clone> Clone for SpecificSize<M>
Source§fn clone(&self) -> SpecificSize<M>
fn clone(&self) -> SpecificSize<M>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more