pub struct SpecificSize<M = Any> { /* fields omitted */ }
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");
let size2: Size = "1 kB".parse().unwrap();
assert_eq!(size2.to_string(), "1 kB");
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);
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 cause of bugs.
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));
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");
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.
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);
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);
Performs copy-assignment from source
. Read more
Formats the value using the given formatter. Read more
The associated error which can be returned from parsing.
Parses a string s
to return a value of this type. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
Formats the value using the given formatter. Read more