net-bytes 0.2.0

A Rust library for handling file sizes, download speeds, and download acceleration with support for both SI and IEC standards
Documentation

Net Bytes

Crates.io Documentation License

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_decimal for accurate calculations
  • Zero-Cost Abstractions: Efficient implementation with zero runtime overhead

Installation

Add this to your Cargo.toml:

[dependencies]

net-bytes = "0.2.0"

Usage

File Sizes

use net_bytes::{FileSize, NonZeroFileSize, SizeStandard};
use std::num::NonZeroU64;

// Create a file size of 1.5 MB (SI standard)
let size = FileSize::new(1_500_000);
let formatted = size.to_si_string();
println!("Size: {}", formatted); // Prints: 1.50 MB

// Using IEC standard (base-1024)
let formatted_iec = size.to_iec_string();
println!("Size (IEC): {}", formatted_iec); // Prints: 1.43 MiB

// Non-zero file size
let non_zero = NonZeroFileSize::new(NonZeroU64::new(1_500_000).unwrap());
println!("Non-zero size: {}", non_zero.to_si_string());

Download Speeds

use net_bytes::DownloadSpeed;
use std::time::Duration;

// Create from bytes and duration
let speed = DownloadSpeed::new(10_000_000, Duration::from_secs(2));
println!("Speed: {}", speed.to_si_string()); // Prints: 5.00 MB/s

// Or from raw bytes per second
let speed = DownloadSpeed::from_raw(5_000_000);
println!("Speed: {}", speed.to_iec_string()); // Prints: 4.77 MiB/s

Download Acceleration

use net_bytes::DownloadAcceleration;
use std::time::Duration;

// Create from speed change and duration
let accel = DownloadAcceleration::new(
    1_000_000,              // 1 MB/s initial speed
    5_000_000,              // 5 MB/s final speed
    Duration::from_secs(4), // over 4 seconds
);
println!("Acceleration: {}", accel.to_si_string()); // 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(eta) = accel.predict_eta(current_speed.into(), remaining_bytes) {
    println!("ETA: {:.1} seconds", eta);
}

API Reference

FileSize

  • new(bytes: u64) -> Self - Create a new file size
  • as_u64() -> u64 - Get the size in bytes as u64
  • to_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 standard
  • get_iec_parts() -> (String, &'static str) - Get (value, unit) pair in IEC standard
  • to_nonzero() -> Option<NonZeroFileSize> - Convert to NonZeroFileSize if not zero

NonZeroFileSize

  • new(bytes: NonZeroU64) -> Self - Create a new non-zero file size
  • as_u64() -> u64 - Get the size in bytes as u64
  • get_nonzero() -> NonZeroU64 - Get the size as NonZeroU64
  • to_si_string() -> String - Format using SI units
  • to_iec_string() -> String - Format using IEC units
  • get_si_parts() -> (String, &'static str) - Get (value, unit) pair in SI standard
  • get_iec_parts() -> (String, &'static str) - Get (value, unit) pair in IEC standard

DownloadSpeed

  • new(bytes: u64, duration: Duration) -> Self - Create from bytes and duration
  • from_raw(bytes_per_second: u64) -> Self - Create from bytes per second
  • as_decimal() -> Decimal - Get speed as Decimal
  • as_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 standard
  • get_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 duration
  • from_raw(bytes_per_second_sq: i64) -> Self - Create from bytes per second squared
  • as_decimal() -> Decimal - Get acceleration as Decimal
  • as_i64() -> i64 - Get acceleration as i64 (floored)
  • predict_eta(current_speed: Decimal, remaining_bytes: u64) -> Option<Decimal> - Predict ETA considering acceleration
  • 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 standard
  • get_iec_parts() -> (String, &'static str) - Get (value, unit) pair in IEC standard

License

Licensed under either of:

at your option.