Format a byte count as a human-readable string (B / KB / MB / GB). Used by compute drivers when reporting image pull progress.
(bytes: u64)
| 42 | /// |
| 43 | /// Used by compute drivers when reporting image pull progress. |
| 44 | pub fn format_bytes(bytes: u64) -> String { |
| 45 | const KB: u64 = 1024; |
| 46 | const MB: u64 = 1024 * KB; |
| 47 | const GB: u64 = 1024 * MB; |
| 48 | |
| 49 | if bytes >= GB { |
| 50 | #[allow(clippy::cast_precision_loss)] |
| 51 | let gb = bytes as f64 / GB as f64; |
| 52 | format!("{gb:.1} GB") |
| 53 | } else if bytes >= MB { |
| 54 | format!("{} MB", bytes / MB) |
| 55 | } else if bytes >= KB { |
| 56 | format!("{} KB", bytes / KB) |
| 57 | } else { |
| 58 | format!("{bytes} B") |
| 59 | } |
| 60 | } |
no outgoing calls
no test coverage detected