Struct rocket::data::ByteUnit [−][src]
#[repr(transparent)]pub struct ByteUnit(_);
Expand description
A unit of bytes with saturating const
constructors and arithmetic.
Overview
A ByteUnit
represents a unit, a count, a number, of bytes. All operations
on a ByteUnit
– constructors, arithmetic, conversions – saturate.
Overflow, underflow, and divide-by-zero are impossible. See the top-level
documentation for more.
ToByteUnit
provides human-friendly methods on all integer types for
converting into a ByteUnit
: 512.megabytes()
.
Parsing
ByteUnit
implements FromStr
for parsing byte unit strings into a
ByteUnit
. The grammar accepted by the parser is:
byte_unit := uint+ ('.' uint+)? WHITESPACE* suffix
uint := '0'..'9'
suffix := case insensitive SI byte unit suffix ('b' to 'eib')
WHITESPACE := the ' ' character
use ubyte::{ByteUnit, ToByteUnit}; let one_gib: ByteUnit = "1GiB".parse().unwrap(); assert_eq!(one_gib, 1.gibibytes()); let quarter_mb: ByteUnit = "256 kB".parse().unwrap(); assert_eq!(quarter_mb, 256.kilobytes()); let half_mb: ByteUnit = "0.5MB".parse().unwrap(); assert_eq!(half_mb, 500.kilobytes()); let half_mib: ByteUnit = "0.500 mib".parse().unwrap(); assert_eq!(half_mib, 512.kibibytes()); let some_mb: ByteUnit = "20.5MB".parse().unwrap(); assert_eq!(some_mb, 20.megabytes() + 500.kilobytes());
(De)serialization
With the serde
feaure enabled (disabled by default), ByteUnit
implements
Deserialize
from strings, using the same
grammar as the FromStr
implementation, defined above, as well as all
integer types. The Serialize
implementation serializes into a u64
.
Example
use ubyte::{ByteUnit, ToByteUnit}; // Construct with unit-valued associated constants, `const` constructors, or // human-friendly methods from the `ToByteUnit` integer extension trait. const HALF_GB: ByteUnit = ByteUnit::Megabyte(500); const HALF_GIB: ByteUnit = ByteUnit::Mebibyte(512); let half_gb = 500 * ByteUnit::MB; let half_gib = 512 * ByteUnit::MiB; let half_gb = 500.megabytes(); let half_gib = 512.mebibytes(); // All arithmetic operations and conversions saturate. let exbibyte = ByteUnit::Exbibyte(1); let exbibyte_too_large_a = 1024 * ByteUnit::EiB; let exbibyte_too_large_b = ByteUnit::Exbibyte(1024); let exbibyte_too_large_c = 1024.exbibytes(); let div_by_zero = 1024.exbibytes() / 0; let too_small = 1000.megabytes() - 1.gibibytes(); assert_eq!(exbibyte << 4, ByteUnit::max_value()); assert_eq!(exbibyte << 10, ByteUnit::max_value()); assert_eq!(exbibyte_too_large_a, ByteUnit::max_value()); assert_eq!(exbibyte_too_large_b, ByteUnit::max_value()); assert_eq!(exbibyte_too_large_c, ByteUnit::max_value()); assert_eq!(div_by_zero, ByteUnit::max_value()); assert_eq!(too_small, 0);
Implementations
Constructs a ByteUnit
representing n
kB
.
Example
assert_eq!(ByteUnit::Kilobyte(10), 10 * ByteUnit::kB);
Constructs a ByteUnit
representing n
KiB
.
Example
assert_eq!(ByteUnit::Kibibyte(10), 10 * ByteUnit::KiB);
Constructs a ByteUnit
representing n
MB
.
Example
assert_eq!(ByteUnit::Megabyte(10), 10 * ByteUnit::MB);
Constructs a ByteUnit
representing n
MiB
.
Example
assert_eq!(ByteUnit::Mebibyte(10), 10 * ByteUnit::MiB);
Constructs a ByteUnit
representing n
GB
.
Example
assert_eq!(ByteUnit::Gigabyte(10), 10 * ByteUnit::GB);
Constructs a ByteUnit
representing n
GiB
.
Example
assert_eq!(ByteUnit::Gibibyte(10), 10 * ByteUnit::GiB);
Constructs a ByteUnit
representing n
TB
.
Example
assert_eq!(ByteUnit::Terabyte(10), 10 * ByteUnit::TB);
Constructs a ByteUnit
representing n
TiB
.
Example
assert_eq!(ByteUnit::Tebibyte(10), 10 * ByteUnit::TiB);
Constructs a ByteUnit
representing n
PB
.
Example
assert_eq!(ByteUnit::Petabyte(10), 10 * ByteUnit::PB);
Constructs a ByteUnit
representing n
PiB
.
Example
assert_eq!(ByteUnit::Pebibyte(10), 10 * ByteUnit::PiB);
Constructs a ByteUnit
representing n
EB
.
Example
assert_eq!(ByteUnit::Exabyte(10), 10 * ByteUnit::EB);
Constructs a ByteUnit
representing n
EiB
.
Example
assert_eq!(ByteUnit::Exbibyte(10), 10 * ByteUnit::EiB);
The maximum value of bytes representable by ByteUnit
.
Example
assert_eq!(ByteUnit::max_value(), u64::max_value());
Returns the value of bytes represented by self
as a u64
.
Example
let int: u64 = ByteUnit::Gigabyte(4).as_u64(); assert_eq!(int, 4 * ByteUnit::GB); assert_eq!(ByteUnit::Megabyte(42).as_u64(), 42 * 1_000_000); assert_eq!(ByteUnit::Exbibyte(7).as_u64(), 7 * 1 << 60);
Returns the value of bytes represented by self
as a u128
.
Example
let int: u128 = ByteUnit::Gigabyte(4).as_u128(); assert_eq!(int, 4 * ByteUnit::GB); assert_eq!(ByteUnit::Megabyte(42).as_u64(), 42 * 1_000_000); assert_eq!(ByteUnit::Exbibyte(7).as_u64(), 7 * 1 << 60);
Returns the components of the minimal unit representation of self
.
The “minimal unit representation” is the representation that maximizes
the SI-unit while minimizing the whole part of the value. For example,
1024.bytes()
is minimally represented by 1KiB
, while 1023.bytes()
is minimally represented by 1.023kB
.
The four components returned, in tuple-order, are:
whole
- the whole part of the minimal representation.frac
- the fractional part of the minimal representation.suffix
- the suffix of the minimal representation.unit
- the1
-unit of the minimal representation.
Succinctly, this is: (whole, frac, suffix, unit)
. Observe that (whole + frac) * unit
reconstructs the original value.
Example
use ubyte::{ByteUnit, ToByteUnit}; let value = 2.mebibytes() + 512.kibibytes(); assert_eq!(value.to_string(), "2.50MiB"); let (whole, frac, suffix, unit) = value.repr(); assert_eq!(whole, 2); assert_eq!(frac, 0.5); assert_eq!(suffix, "MiB"); assert_eq!(unit, ByteUnit::MiB); let reconstructed = (whole as f64 + frac) * unit.as_u64() as f64; assert_eq!(reconstructed as u64, value);
Trait Implementations
pub fn deserialize<D>(
deserializer: D
) -> Result<ByteUnit, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
[src]
pub fn deserialize<D>(
deserializer: D
) -> Result<ByteUnit, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
[src]Deserialize this value from the given Serde deserializer. Read more
Display self
as best as possible. For perfectly custom display output,
consider using ByteUnit::repr()
.
Example
use ubyte::{ByteUnit, ToByteUnit}; assert_eq!(323.kilobytes().to_string(), "323kB"); assert_eq!(3.megabytes().to_string(), "3MB"); assert_eq!(3.mebibytes().to_string(), "3MiB"); assert_eq!((3.mebibytes() + 140.kilobytes()).to_string(), "3.13MiB"); assert_eq!((3.mebibytes() + 2.mebibytes()).to_string(), "5MiB"); assert_eq!((7.gigabytes() + 58.mebibytes() + 3.kilobytes()).to_string(), "7.06GB"); assert_eq!((7.gibibytes() + 920.mebibytes()).to_string(), "7.90GiB"); assert_eq!(7231.kilobytes().to_string(), "6.90MiB"); assert_eq!(format!("{:.0}", 7.gibibytes() + 920.mebibytes()), "8GiB"); assert_eq!(format!("{:.1}", 7.gibibytes() + 920.mebibytes()), "7.9GiB"); assert_eq!(format!("{:.2}", 7.gibibytes() + 920.mebibytes()), "7.90GiB"); assert_eq!(format!("{:.3}", 7.gibibytes() + 920.mebibytes()), "7.898GiB"); assert_eq!(format!("{:.4}", 7.gibibytes() + 920.mebibytes()), "7.8984GiB"); assert_eq!(format!("{:.4}", 7231.kilobytes()), "6.8960MiB"); assert_eq!(format!("{:.0}", 7231.kilobytes()), "7MiB"); assert_eq!(format!("{:.2}", 999.kilobytes() + 990.bytes()), "976.55KiB"); assert_eq!(format!("{:.0}", 999.kilobytes() + 990.bytes()), "1MB"); assert_eq!(format!("{:04.2}", 999.kilobytes() + 990.bytes()), "0976.55KiB"); assert_eq!(format!("{:02.0}", 999.kilobytes() + 990.bytes()), "01MB"); assert_eq!(format!("{:04.0}", 999.kilobytes() + 990.bytes()), "0001MB");
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 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
pub fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
[src]
pub fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
[src]Serialize this value into the given Serde serializer. Read more
Auto Trait Implementations
impl RefUnwindSafe for ByteUnit
impl UnwindSafe for ByteUnit
Blanket Implementations
Mutably borrows from an owned value. Read more
Compare self to key
and return true
if they are equal.
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Converts self
into a collection.
type Output = T
type Output = T
Should always be Self
pub fn vzip(self) -> V