Skip to content

Commit 4e7d8e1

Browse files
Sheaperobjtede
andauthored
feat: add custom unit conversions (#111)
Co-authored-by: Rob Ede <robjtede@icloud.com>
1 parent 5a9d431 commit 4e7d8e1

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add `ByteSize::as_*()` methods to return equivalent sizes in KB, GiB, etc.
6+
57
## 2.1.0
68

79
- Support parsing and formatting exabytes (EB) & exbibytes (EiB).

src/lib.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,78 @@ impl ByteSize {
249249
self.0
250250
}
251251

252+
/// Returns byte count as kilobytes.
253+
#[inline(always)]
254+
pub fn as_kb(&self) -> f64 {
255+
self.0 as f64 / KB as f64
256+
}
257+
258+
/// Returns byte count as kibibytes.
259+
#[inline(always)]
260+
pub fn as_kib(&self) -> f64 {
261+
self.0 as f64 / KIB as f64
262+
}
263+
264+
/// Returns byte count as megabytes.
265+
#[inline(always)]
266+
pub fn as_mb(&self) -> f64 {
267+
self.0 as f64 / MB as f64
268+
}
269+
270+
/// Returns byte count as mebibytes.
271+
#[inline(always)]
272+
pub fn as_mib(&self) -> f64 {
273+
self.0 as f64 / MIB as f64
274+
}
275+
276+
/// Returns byte count as gigabytes.
277+
#[inline(always)]
278+
pub fn as_gb(&self) -> f64 {
279+
self.0 as f64 / GB as f64
280+
}
281+
282+
/// Returns byte count as gibibytes.
283+
#[inline(always)]
284+
pub fn as_gib(&self) -> f64 {
285+
self.0 as f64 / GIB as f64
286+
}
287+
288+
/// Returns byte count as terabytes.
289+
#[inline(always)]
290+
pub fn as_tb(&self) -> f64 {
291+
self.0 as f64 / TB as f64
292+
}
293+
294+
/// Returns byte count as tebibytes.
295+
#[inline(always)]
296+
pub fn as_tib(&self) -> f64 {
297+
self.0 as f64 / TIB as f64
298+
}
299+
300+
/// Returns byte count as petabytes.
301+
#[inline(always)]
302+
pub fn as_pb(&self) -> f64 {
303+
self.0 as f64 / PB as f64
304+
}
305+
306+
/// Returns byte count as pebibytes.
307+
#[inline(always)]
308+
pub fn as_pib(&self) -> f64 {
309+
self.0 as f64 / PIB as f64
310+
}
311+
312+
/// Returns byte count as exabytes.
313+
#[inline(always)]
314+
pub fn as_eb(&self) -> f64 {
315+
self.0 as f64 / EB as f64
316+
}
317+
318+
/// Returns byte count as exbibytes.
319+
#[inline(always)]
320+
pub fn as_eib(&self) -> f64 {
321+
self.0 as f64 / EIB as f64
322+
}
323+
252324
/// Returns a formatting display wrapper.
253325
pub fn display(&self) -> Display {
254326
Display {
@@ -488,6 +560,16 @@ mod tests {
488560
assert!(ByteSize::pib(1) < ByteSize::eb(1));
489561
}
490562

563+
#[test]
564+
fn as_unit_conversions() {
565+
assert_eq!(41992187.5, ByteSize::gb(43).as_kib());
566+
assert_eq!(0.028311552, ByteSize::mib(27).as_gb());
567+
assert_eq!(0.0380859375, ByteSize::tib(39).as_pib());
568+
assert_eq!(961.482752, ByteSize::kib(938948).as_mb());
569+
assert_eq!(4.195428726649908, ByteSize::pb(4837).as_eib());
570+
assert_eq!(2.613772153284117, ByteSize::b(2873872874893).as_tib());
571+
}
572+
491573
#[track_caller]
492574
fn assert_display(expected: &str, b: ByteSize) {
493575
assert_eq!(expected, format!("{b}"));

0 commit comments

Comments
 (0)