net-bytes 0.1.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
  • Dual Standard Support: Both SI (base-1000) and IEC (base-1024) standards
  • Precise Decimal Handling: Uses rust_decimal for accurate calculations

Installation

Add this to your Cargo.toml:

[dependencies]
net-bytes = "0.1.0"

Usage

File Sizes

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

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

// Using IEC standard (base-1024)
let size_iec = FileSize::new(NonZeroU64::new(1_500_000).unwrap(), SizeStandard::IEC);
println!("Size (IEC): {}", size_iec); // Prints: 1.43 MiB

Download Speeds

use net_bytes::{DownloadSpeed, SizeStandard};
use std::time::Duration;
use std::num::NonZeroU64;

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

Download Acceleration

use net_bytes::{DownloadAcceleration, SizeStandard};
use std::time::Duration;
use std::num::NonZeroU64;

// Create from initial speed, final speed, and duration
let acceleration = DownloadAcceleration::new(
    NonZeroU64::new(1_000_000).unwrap(),  // 1 MB/s initial speed
    NonZeroU64::new(5_000_000).unwrap(),  // 5 MB/s final speed
    Duration::from_secs(4),               // over 4 seconds
    SizeStandard::SI
);
println!("Acceleration: {}/s²", acceleration); // Prints: 1.00 MB/s²

API Reference

FileSize

  • new(bytes: NonZeroU64, standard: SizeStandard) -> Self - Create a new file size
  • get_si_parts() -> (String, &'static str) - Get formatted value and unit in SI standard
  • get_iec_parts() -> (String, &'static str) - Get formatted value and unit in IEC standard
  • as_u64() -> u64 - Get the size in bytes as u64
  • get_nonzero() -> NonZeroU64 - Get the size as NonZeroU64

DownloadSpeed

  • new(bytes: NonZeroU64, duration: Duration, standard: SizeStandard) -> Self - Create from bytes and duration
  • from_raw(bytes_per_second: NonZeroU64, standard: SizeStandard) -> Self - Create from bytes per second
  • as_decimal() -> Decimal - Get speed as Decimal
  • as_u64() -> u64 - Get speed as u64 (floored)
  • get_si_parts() -> (String, &'static str) - Get formatted value and unit in SI standard
  • get_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 duration
  • from_raw(bytes_per_second_sq: i64, standard: SizeStandard) -> Self - Create from bytes per second squared
  • as_i64() -> i64 - Get acceleration as i64 (floored)
  • get_si_parts() -> (String, &'static str) - Get formatted value and unit in SI standard
  • get_iec_parts() -> (String, &'static str) - Get formatted value and unit in IEC standard

License

Licensed under either of:

at your option.