Net Bytes
A Rust library for handling file sizes, download speeds, and download acceleration with support for both SI (base-1000) and IEC (base-1024) standards.
Features
- File Size Formatting: Format file sizes with automatic unit selection (B, KB/KiB, MB/MiB, etc.)
- Download Speed Calculation: Calculate and format download speeds
- Download Acceleration: Measure and format download acceleration with ETA prediction
- Dual Standard Support: Both SI (base-1000) and IEC (base-1024) standards
- Precise Decimal Handling: Uses
rust_decimalfor accurate calculations - Zero-Cost Abstractions: Efficient implementation with zero runtime overhead
Installation
Add this to your Cargo.toml:
[]
= "0.2.0"
Usage
File Sizes
use ;
use NonZeroU64;
// Create a file size of 1.5 MB (SI standard)
let size = new;
let formatted = size.to_si_string;
println!; // Prints: 1.50 MB
// Using IEC standard (base-1024)
let formatted_iec = size.to_iec_string;
println!; // Prints: 1.43 MiB
// Non-zero file size
let non_zero = new;
println!;
Download Speeds
use DownloadSpeed;
use Duration;
// Create from bytes and duration
let speed = new;
println!; // Prints: 5.00 MB/s
// Or from raw bytes per second
let speed = from_raw;
println!; // Prints: 4.77 MiB/s
Download Acceleration
use DownloadAcceleration;
use Duration;
// Create from speed change and duration
let accel = new;
println!; // Prints: 1.00 MB/s²
// Predict ETA with acceleration
let current_speed = 2_000_000.0; // 2 MB/s
let remaining_bytes = 10_000_000; // 10 MB
if let Some = accel.predict_eta
API Reference
FileSize
new(bytes: u64) -> Self- Create a new file sizeas_u64() -> u64- Get the size in bytes as u64to_si_string() -> String- Format using SI units (KB, MB, GB, etc.)to_iec_string() -> String- Format using IEC units (KiB, MiB, GiB, etc.)get_si_parts() -> (String, &'static str)- Get (value, unit) pair in SI standardget_iec_parts() -> (String, &'static str)- Get (value, unit) pair in IEC standardto_nonzero() -> Option<NonZeroFileSize>- Convert to NonZeroFileSize if not zero
NonZeroFileSize
new(bytes: NonZeroU64) -> Self- Create a new non-zero file sizeas_u64() -> u64- Get the size in bytes as u64get_nonzero() -> NonZeroU64- Get the size as NonZeroU64to_si_string() -> String- Format using SI unitsto_iec_string() -> String- Format using IEC unitsget_si_parts() -> (String, &'static str)- Get (value, unit) pair in SI standardget_iec_parts() -> (String, &'static str)- Get (value, unit) pair in IEC standard
DownloadSpeed
new(bytes: u64, duration: Duration) -> Self- Create from bytes and durationfrom_raw(bytes_per_second: u64) -> Self- Create from bytes per secondas_decimal() -> Decimal- Get speed as Decimalas_u64() -> u64- Get speed as u64 (floored)to_si_string() -> String- Format using SI units (KB/s, MB/s, etc.)to_iec_string() -> String- Format using IEC units (KiB/s, MiB/s, etc.)get_si_parts() -> (String, &'static str)- Get (value, unit) pair in SI standardget_iec_parts() -> (String, &'static str)- Get (value, unit) pair in IEC standard
DownloadAcceleration
new(initial_speed: u64, final_speed: u64, duration: Duration) -> Self- Create from speed change and durationfrom_raw(bytes_per_second_sq: i64) -> Self- Create from bytes per second squaredas_decimal() -> Decimal- Get acceleration as Decimalas_i64() -> i64- Get acceleration as i64 (floored)predict_eta(current_speed: Decimal, remaining_bytes: u64) -> Option<Decimal>- Predict ETA considering accelerationto_si_string() -> String- Format using SI units (KB/s², MB/s², etc.)to_iec_string() -> String- Format using IEC units (KiB/s², MiB/s², etc.)get_si_parts() -> (String, &'static str)- Get (value, unit) pair in SI standardget_iec_parts() -> (String, &'static str)- Get (value, unit) pair in IEC standard
License
Licensed under either of:
at your option.