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
- Dual Standard Support: Both SI (base-1000) and IEC (base-1024) standards
- Precise Decimal Handling: Uses
rust_decimalfor accurate calculations
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
File Sizes
use ;
use NonZeroU64;
// Create a file size of 1.5 MB (SI standard)
let size = new;
println!; // Prints: 1.50 MB
// Using IEC standard (base-1024)
let size_iec = new;
println!; // Prints: 1.43 MiB
Download Speeds
use ;
use Duration;
use NonZeroU64;
// Create from bytes and duration
let speed = new;
println!; // Prints: 5.00 MB/s
Download Acceleration
use ;
use Duration;
use NonZeroU64;
// Create from initial speed, final speed, and duration
let acceleration = new;
println!; // Prints: 1.00 MB/s²
API Reference
FileSize
new(bytes: NonZeroU64, standard: SizeStandard) -> Self- Create a new file sizeget_si_parts() -> (String, &'static str)- Get formatted value and unit in SI standardget_iec_parts() -> (String, &'static str)- Get formatted value and unit in IEC standardas_u64() -> u64- Get the size in bytes as u64get_nonzero() -> NonZeroU64- Get the size as NonZeroU64
DownloadSpeed
new(bytes: NonZeroU64, duration: Duration, standard: SizeStandard) -> Self- Create from bytes and durationfrom_raw(bytes_per_second: NonZeroU64, standard: SizeStandard) -> Self- Create from bytes per secondas_decimal() -> Decimal- Get speed as Decimalas_u64() -> u64- Get speed as u64 (floored)get_si_parts() -> (String, &'static str)- Get formatted value and unit in SI standardget_iec_parts() -> (String, &'static str)- Get formatted value and unit in IEC standard
DownloadAcceleration
new(initial_speed: NonZeroU64, final_speed: NonZeroU64, duration: Duration, standard: SizeStandard) -> Self- Create from speed change and durationfrom_raw(bytes_per_second_sq: i64, standard: SizeStandard) -> Self- Create from bytes per second squaredas_i64() -> i64- Get acceleration as i64 (floored)get_si_parts() -> (String, &'static str)- Get formatted value and unit in SI standardget_iec_parts() -> (String, &'static str)- Get formatted value and unit in IEC standard
License
Licensed under either of:
at your option.