From 04c4c291b30f0104b15b7dc389e7242d526d0d35 Mon Sep 17 00:00:00 2001 From: Andrew Voynov Date: Fri, 6 Oct 2023 19:21:41 +0300 Subject: [PATCH 01/16] Fixed inverted SI conditions - Conditions that check whether the SI or IEC units must be used were inverted, i.e., when `si_prefix == true` it would use `"iB"` instead of `"B"`. - KB & kiB were used instead of kB & KiB. - Switches (true/false) in tests are also fixed. --- src/lib.rs | 58 ++++++++++++++++++++++++++-------------------------- src/parse.rs | 2 +- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index edf5f97f..453a4ba8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,8 +25,8 @@ //! ``` //! use bytesize::ByteSize; //! -//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true)); -//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false)); +//! assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(false)); +//! assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(true)); //! ``` mod parse; @@ -185,14 +185,14 @@ impl ByteSize { } pub fn to_string(bytes: u64, si_prefix: bool) -> String { - let unit = if si_prefix { KIB } else { KB }; - let unit_base = if si_prefix { LN_KIB } else { LN_KB }; + let unit = if si_prefix { KB } else { KIB }; + let unit_base = if si_prefix { LN_KB } else { LN_KIB }; let unit_prefix = if si_prefix { UNITS_SI.as_bytes() } else { UNITS.as_bytes() }; - let unit_suffix = if si_prefix { "iB" } else { "B" }; + let unit_suffix = if si_prefix { "B" } else { "iB" }; if bytes < unit { format!("{} B", bytes) @@ -427,12 +427,12 @@ mod tests { #[test] fn test_display() { assert_display("215 B", ByteSize::b(215)); - assert_display("1.0 KB", ByteSize::kb(1)); - assert_display("301.0 KB", ByteSize::kb(301)); - assert_display("419.0 MB", ByteSize::mb(419)); - assert_display("518.0 GB", ByteSize::gb(518)); - assert_display("815.0 TB", ByteSize::tb(815)); - assert_display("609.0 PB", ByteSize::pb(609)); + assert_display("1.0 KiB", ByteSize::kib(1)); + assert_display("301.0 KiB", ByteSize::kib(301)); + assert_display("419.0 MiB", ByteSize::mib(419)); + assert_display("518.0 GiB", ByteSize::gib(518)); + assert_display("815.0 TiB", ByteSize::tib(815)); + assert_display("609.0 PiB", ByteSize::pib(609)); } #[test] @@ -453,33 +453,33 @@ mod tests { #[test] fn test_to_string_as() { - assert_to_string("215 B", ByteSize::b(215), true); assert_to_string("215 B", ByteSize::b(215), false); + assert_to_string("215 B", ByteSize::b(215), true); - assert_to_string("1.0 kiB", ByteSize::kib(1), true); - assert_to_string("1.0 KB", ByteSize::kib(1), false); + assert_to_string("1.0 KiB", ByteSize::kib(1), false); + assert_to_string("1.0 kB", ByteSize::kib(1), true); - assert_to_string("293.9 kiB", ByteSize::kb(301), true); - assert_to_string("301.0 KB", ByteSize::kb(301), false); + assert_to_string("293.9 KiB", ByteSize::kb(301), false); + assert_to_string("301.0 kB", ByteSize::kb(301), true); - assert_to_string("1.0 MiB", ByteSize::mib(1), true); - assert_to_string("1048.6 KB", ByteSize::mib(1), false); + assert_to_string("1.0 MiB", ByteSize::mib(1), false); + assert_to_string("1048.6 kB", ByteSize::mib(1), true); // a bug case: https://github.com/flang-project/bytesize/issues/8 - assert_to_string("1.9 GiB", ByteSize::mib(1907), true); - assert_to_string("2.0 GB", ByteSize::mib(1908), false); + assert_to_string("1.9 GiB", ByteSize::mib(1907), false); + assert_to_string("2.0 GB", ByteSize::mib(1908), true); - assert_to_string("399.6 MiB", ByteSize::mb(419), true); - assert_to_string("419.0 MB", ByteSize::mb(419), false); + assert_to_string("399.6 MiB", ByteSize::mb(419), false); + assert_to_string("419.0 MB", ByteSize::mb(419), true); - assert_to_string("482.4 GiB", ByteSize::gb(518), true); - assert_to_string("518.0 GB", ByteSize::gb(518), false); + assert_to_string("482.4 GiB", ByteSize::gb(518), false); + assert_to_string("518.0 GB", ByteSize::gb(518), true); - assert_to_string("741.2 TiB", ByteSize::tb(815), true); - assert_to_string("815.0 TB", ByteSize::tb(815), false); + assert_to_string("741.2 TiB", ByteSize::tb(815), false); + assert_to_string("815.0 TB", ByteSize::tb(815), true); - assert_to_string("540.9 PiB", ByteSize::pb(609), true); - assert_to_string("609.0 PB", ByteSize::pb(609), false); + assert_to_string("540.9 PiB", ByteSize::pb(609), false); + assert_to_string("609.0 PB", ByteSize::pb(609), true); } #[test] @@ -489,7 +489,7 @@ mod tests { #[test] fn test_to_string() { - assert_to_string("609.0 PB", ByteSize::pb(609), false); + assert_to_string("609.0 PB", ByteSize::pb(609), true); } #[cfg(feature = "serde")] diff --git a/src/parse.rs b/src/parse.rs index a5cca5b3..a0d3cf55 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -231,7 +231,7 @@ mod tests { assert_eq!(parse(&format!("{}", parse("128GB"))), 128 * Unit::GigaByte); assert_eq!( - parse(&crate::to_string(parse("128.000 GiB"), true)), + parse(&crate::to_string(parse("128.000 GiB"), false)), 128 * Unit::GibiByte ); } From 1ccab13d4af82c38fc70d600578199c6bdc5ddea Mon Sep 17 00:00:00 2001 From: Andrew Voynov Date: Fri, 6 Oct 2023 19:44:55 +0300 Subject: [PATCH 02/16] Swaped (fixed) values of `LN_KIB` & `LN_KB` --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 453a4ba8..4b9e7368 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,8 +69,8 @@ pub const PIB: u64 = 1_125_899_906_842_624; static UNITS: &str = "KMGTPE"; static UNITS_SI: &str = "kMGTPE"; -static LN_KB: f64 = 6.931471806; // ln 1024 -static LN_KIB: f64 = 6.907755279; // ln 1000 +static LN_KIB: f64 = 6.931471806; // ln 1024 +static LN_KB: f64 = 6.907755279; // ln 1000 pub fn kb>(size: V) -> u64 { size.into() * KB From 4d7c8d9ca90dc2ca0265146bf169f3aa96f5746d Mon Sep 17 00:00:00 2001 From: Andrew Voynov Date: Fri, 6 Oct 2023 20:09:12 +0300 Subject: [PATCH 03/16] Updated tests in README.md Additional small fixes: - Removed comment about fixed bug in `./src/lib.rs`. - Rust code now has consistent 4 spaces indents in README.md. --- README.md | 145 ++++++++++++++++++++++++++--------------------------- src/lib.rs | 1 - 2 files changed, 72 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index b25bc9dd..3f1fd73a 100644 --- a/README.md +++ b/README.md @@ -29,84 +29,83 @@ extern crate bytesize; ``` ## Example -### Human readable representations (SI unit and Binary unit) +### Human readable representations (SI units and Binary units) ```rust #[allow(dead_code)] fn assert_display(expected: &str, b: ByteSize) { - assert_eq!(expected, format!("{}", b)); + assert_eq!(expected, format!("{}", b)); } #[test] - fn test_display() { - assert_display("215 B", ByteSize(215)); +fn test_display() { assert_display("215 B", ByteSize::b(215)); - assert_display("1.0 KB", ByteSize::kb(1)); - assert_display("301.0 KB", ByteSize::kb(301)); - assert_display("419.0 MB", ByteSize::mb(419)); - assert_display("518.0 GB", ByteSize::gb(518)); - assert_display("815.0 TB", ByteSize::tb(815)); - assert_display("609.0 PB", ByteSize::pb(609)); - } - - fn assert_to_string(expected: &str, b: ByteSize, si: bool) { - assert_eq!(expected.to_string(), b.to_string_as(si)); - } + assert_display("1.0 KiB", ByteSize::kib(1)); + assert_display("301.0 KiB", ByteSize::kib(301)); + assert_display("419.0 MiB", ByteSize::mib(419)); + assert_display("518.0 GiB", ByteSize::gib(518)); + assert_display("815.0 TiB", ByteSize::tib(815)); + assert_display("609.0 PiB", ByteSize::pib(609)); +} - #[test] - fn test_to_string() { - assert_to_string("215 B", ByteSize(215), true); - assert_to_string("215 B", ByteSize(215), false); +fn assert_to_string(expected: &str, b: ByteSize, si: bool) { + assert_eq!(expected.to_string(), b.to_string_as(si)); +} - assert_to_string("215 B", ByteSize::b(215), true); +#[test] +fn test_to_string_as() { assert_to_string("215 B", ByteSize::b(215), false); + assert_to_string("215 B", ByteSize::b(215), true); + + assert_to_string("1.0 KiB", ByteSize::kib(1), false); + assert_to_string("1.0 kB", ByteSize::kib(1), true); + + assert_to_string("293.9 KiB", ByteSize::kb(301), false); + assert_to_string("301.0 kB", ByteSize::kb(301), true); - assert_to_string("1.0 kiB", ByteSize::kib(1), true); - assert_to_string("1.0 KB", ByteSize::kib(1), false); - - assert_to_string("293.9 kiB", ByteSize::kb(301), true); - assert_to_string("301.0 KB", ByteSize::kb(301), false); - - assert_to_string("1.0 MiB", ByteSize::mib(1), true); - assert_to_string("1048.6 KB", ByteSize::mib(1), false); - - assert_to_string("399.6 MiB", ByteSize::mb(419), true); - assert_to_string("419.0 MB", ByteSize::mb(419), false); - - assert_to_string("482.4 GiB", ByteSize::gb(518), true); - assert_to_string("518.0 GB", ByteSize::gb(518), false); - - assert_to_string("741.2 TiB", ByteSize::tb(815), true); - assert_to_string("815.0 TB", ByteSize::tb(815), false); - - assert_to_string("540.9 PiB", ByteSize::pb(609), true); - assert_to_string("609.0 PB", ByteSize::pb(609), false); - } - - #[test] - fn test_parsing_from_str() { - // shortcut for writing test cases - fn parse(s: &str) -> u64 { - s.parse::().unwrap().0 - } - - assert_eq!("0".parse::().unwrap().0, 0); - assert_eq!(parse("0"), 0); - assert_eq!(parse("500"), 500); - assert_eq!(parse("1K"), Unit::KiloByte * 1); - assert_eq!(parse("1Ki"), Unit::KibiByte * 1); - assert_eq!(parse("1.5Ki"), (1.5 * Unit::KibiByte) as u64); - assert_eq!(parse("1KiB"), 1 * Unit::KibiByte); - assert_eq!(parse("1.5KiB"), (1.5 * Unit::KibiByte) as u64); - assert_eq!(parse("3 MB"), Unit::MegaByte * 3); - assert_eq!(parse("4 MiB"), Unit::MebiByte * 4); - assert_eq!(parse("6 GB"), 6 * Unit::GigaByte); - assert_eq!(parse("4 GiB"), 4 * Unit::GibiByte); - assert_eq!(parse("88TB"), 88 * Unit::TeraByte); - assert_eq!(parse("521TiB"), 521 * Unit::TebiByte); - assert_eq!(parse("8 PB"), 8 * Unit::PetaByte); - assert_eq!(parse("8P"), 8 * Unit::PetaByte); - assert_eq!(parse("12 PiB"), 12 * Unit::PebiByte); - } + assert_to_string("1.0 MiB", ByteSize::mib(1), false); + assert_to_string("1048.6 kB", ByteSize::mib(1), true); + + assert_to_string("1.9 GiB", ByteSize::mib(1907), false); + assert_to_string("2.0 GB", ByteSize::mib(1908), true); + + assert_to_string("399.6 MiB", ByteSize::mb(419), false); + assert_to_string("419.0 MB", ByteSize::mb(419), true); + + assert_to_string("482.4 GiB", ByteSize::gb(518), false); + assert_to_string("518.0 GB", ByteSize::gb(518), true); + + assert_to_string("741.2 TiB", ByteSize::tb(815), false); + assert_to_string("815.0 TB", ByteSize::tb(815), true); + + assert_to_string("540.9 PiB", ByteSize::pb(609), false); + assert_to_string("609.0 PB", ByteSize::pb(609), true); +} + +#[test] +fn test_parsing_from_str() { + // shortcut for writing test cases + fn parse(s: &str) -> u64 { + s.parse::().unwrap().0 + } + + assert_eq!("0".parse::().unwrap().0, 0); + assert_eq!(parse("0"), 0); + assert_eq!(parse("500"), 500); + assert_eq!(parse("1K"), Unit::KiloByte * 1); + assert_eq!(parse("1Ki"), Unit::KibiByte * 1); + assert_eq!(parse("1.5Ki"), (1.5 * Unit::KibiByte) as u64); + assert_eq!(parse("1KiB"), 1 * Unit::KibiByte); + assert_eq!(parse("1.5KiB"), (1.5 * Unit::KibiByte) as u64); + assert_eq!(parse("3 MB"), Unit::MegaByte * 3); + assert_eq!(parse("4 MiB"), Unit::MebiByte * 4); + assert_eq!(parse("6 GB"), 6 * Unit::GigaByte); + assert_eq!(parse("4 GiB"), 4 * Unit::GibiByte); + assert_eq!(parse("88TB"), 88 * Unit::TeraByte); + assert_eq!(parse("521TiB"), 521 * Unit::TebiByte); + assert_eq!(parse("8 PB"), 8 * Unit::PetaByte); + assert_eq!(parse("8P"), 8 * Unit::PetaByte); + assert_eq!(parse("12 PiB"), 12 * Unit::PebiByte); +} ``` ### Arithmetic operations @@ -116,13 +115,13 @@ extern crate bytesize; use bytesize::ByteSize; fn byte_arithmetic_operator() { - let x = ByteSize::mb(1); - let y = ByteSize::kb(100); + let x = ByteSize::mb(1); + let y = ByteSize::kb(100); - let plus = x + y; - print!("{}", plus); + let plus = x + y; + println!("{}", plus); - let minus = ByteSize::tb(100) + ByteSize::gb(4); - print!("{}", minus); + let minus = ByteSize::tb(100) + ByteSize::gb(4); + println!("{}", minus); } ``` diff --git a/src/lib.rs b/src/lib.rs index 4b9e7368..88b210e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -465,7 +465,6 @@ mod tests { assert_to_string("1.0 MiB", ByteSize::mib(1), false); assert_to_string("1048.6 kB", ByteSize::mib(1), true); - // a bug case: https://github.com/flang-project/bytesize/issues/8 assert_to_string("1.9 GiB", ByteSize::mib(1907), false); assert_to_string("2.0 GB", ByteSize::mib(1908), true); From cf581a04907149ca42f2300c1a3120cc4bfe1372 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 10 Feb 2025 00:13:54 +0000 Subject: [PATCH 04/16] chore: merge master readme --- README.md | 122 ++++++++++++++---------------------------------------- 1 file changed, 30 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index d43895ed..2f457de4 100644 --- a/README.md +++ b/README.md @@ -1,115 +1,53 @@ ## ByteSize -[![CI](https://github.com/hyunsik/bytesize/actions/workflows/ci.yml/badge.svg)](https://github.com/hyunsik/bytesize/actions/workflows/ci.yml) + + +[![CI](https://github.com/bytesize-rs/bytesize/actions/workflows/ci.yml/badge.svg)](https://github.com/bytesize-rs/bytesize/actions/workflows/ci.yml) [![Crates.io Version](https://img.shields.io/crates/v/bytesize.svg)](https://crates.io/crates/bytesize) -`ByteSize` is a utility for human-readable byte count representations. + + + + +`ByteSize` is a semantic wrapper for byte count representations. Features: -- Pre-defined constants for various size units (e.g., B, KB, KiB, MB, MiB, GB, GiB, ... PiB). +- Pre-defined constants for various size units (e.g., B, Kb, Kib, Mb, Mib, Gb, Gib, ... PB). - `ByteSize` type which presents size units convertible to different size units. - Arithmetic operations for `ByteSize`. -- FromStr impl for `ByteSize`, allowing to parse from string size representations like 1.5KiB and 521TiB. +- `FromStr` impl for `ByteSize`, allowing for parsing string size representations like "1.5KiB" and "521TiB". - Serde support for binary and human-readable deserializers like JSON. -[API Documentation](https://docs.rs/bytesize) +### Examples -## Example - -### Human readable representations (SI unit and Binary unit) +Construction using SI or IEC helpers. ```rust -fn assert_display(expected: &str, b: ByteSize) { - assert_eq!(expected, format!("{}", b)); -} - -#[test] -fn test_display() { - assert_display("215 B", ByteSize::b(215)); - assert_display("1.0 KiB", ByteSize::kib(1)); - assert_display("301.0 KiB", ByteSize::kib(301)); - assert_display("419.0 MiB", ByteSize::mib(419)); - assert_display("518.0 GiB", ByteSize::gib(518)); - assert_display("815.0 TiB", ByteSize::tib(815)); - assert_display("609.0 PiB", ByteSize::pib(609)); -} - -#[test] -fn test_display_alignment() { - assert_eq!("|357 B |", format!("|{:10}|", ByteSize(357))); - assert_eq!("| 357 B|", format!("|{:>10}|", ByteSize(357))); - assert_eq!("|357 B |", format!("|{:<10}|", ByteSize(357))); - assert_eq!("| 357 B |", format!("|{:^10}|", ByteSize(357))); - - assert_eq!("|-----357 B|", format!("|{:->10}|", ByteSize(357))); - assert_eq!("|357 B-----|", format!("|{:-<10}|", ByteSize(357))); - assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteSize(357))); -} - -fn assert_to_string(expected: &str, b: ByteSize, si: bool) { - assert_eq!(expected.to_string(), b.to_string_as(si)); -} - -#[test] -fn test_to_string_as() { - assert_to_string("215 B", ByteSize::b(215), true); - assert_to_string("215 B", ByteSize::b(215), false); - assert_to_string("215 B", ByteSize::b(215), true); - - assert_to_string("1.0 KiB", ByteSize::kib(1), true); - assert_to_string("1.0 KB", ByteSize::kib(1), false); - - assert_to_string("293.9 KiB", ByteSize::kb(301), true); - assert_to_string("301.0 KB", ByteSize::kb(301), false); - - assert_to_string("1.0 MiB", ByteSize::mib(1), false); - assert_to_string("1048.6 kB", ByteSize::mib(1), true); - - assert_to_string("1.9 GiB", ByteSize::mib(1907), true); - assert_to_string("2.0 GB", ByteSize::mib(1908), false); - - assert_to_string("399.6 MiB", ByteSize::mb(419), true); - assert_to_string("419.0 MB", ByteSize::mb(419), false); - - assert_to_string("399.6 MiB", ByteSize::mb(419), false); - assert_to_string("419.0 MB", ByteSize::mb(419), true); - - assert_to_string("482.4 GiB", ByteSize::gb(518), false); - assert_to_string("518.0 GB", ByteSize::gb(518), true); - - assert_to_string("741.2 TiB", ByteSize::tb(815), false); - assert_to_string("815.0 TB", ByteSize::tb(815), true); - - assert_to_string("540.9 PiB", ByteSize::pb(609), false); - assert_to_string("609.0 PB", ByteSize::pb(609), true); -} - -#[test] -fn test_parsing_from_str() { - // shortcut for writing test cases - fn parse(s: &str) -> u64 { - s.parse::().unwrap().0 - } - - assert_to_string("540.9 PiB", ByteSize::pb(609), false); - assert_to_string("609.0 PB", ByteSize::pb(609), true); -} +use bytesize::ByteSize; + +assert!(ByteSize::kib(4) > ByteSize::kb(4)); ``` -### Arithmetic operations +Display as human-readable string. ```rust use bytesize::ByteSize; -fn byte_arithmetic_operator() { - let x = ByteSize::mb(1); - let y = ByteSize::kb(100); +assert_eq!("482.4 GiB", ByteSize::gb(518).to_string_as(true)); +assert_eq!("518.0 GB", ByteSize::gb(518).to_string_as(false)); +``` + +Arithmetic operations are supported. + +```rust +use bytesize::ByteSize; - let plus = x + y; - println!("{}", plus); +let plus = ByteSize::mb(1) + ByteSize::kb(100); +println!("{plus}"); - let minus = ByteSize::tb(100) + ByteSize::gb(4); - println!("{}", minus); -} +let minus = ByteSize::tb(1) - ByteSize::gb(4); +assert_eq!(ByteSize::gb(996), minus); ``` + + From 70a9fdd5bd5f3293e880fd4ff91e46800dd80d2b Mon Sep 17 00:00:00 2001 From: Croxx Date: Mon, 18 Nov 2024 19:08:42 +0800 Subject: [PATCH 05/16] test: add unit test for json serde (#54) --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index ac004157..887eacec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -511,4 +511,14 @@ mod tests { let s: S = toml::from_str(r#"x = "9223372036854775807""#).unwrap(); assert_eq!(s.x, "9223372036854775807".parse::().unwrap()); } + + #[test] + #[cfg(feature = "serde")] + fn test_serde_json() { + let json = serde_json::to_string(&ByteSize::mib(1)).unwrap(); + assert_eq!(json, "\"1.0 MiB\""); + + let deserialized: ByteSize = serde_json::from_str(&json).unwrap(); + assert_eq!(deserialized.0, 1048576); + } } From b9728cec13b82fece9969c315fbda118fbe648d6 Mon Sep 17 00:00:00 2001 From: Croxx Date: Mon, 18 Nov 2024 23:03:55 +0800 Subject: [PATCH 06/16] feat: impl Sub and SubAssign for bytesize (#56) --- CHANGELOG.md | 4 ++++ src/lib.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cc2adba..8f17df9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,3 +3,7 @@ ## Unreleased - Use SI format by default with `Display`. - Use "KiB" for SI unit. +- Implement `Sub` for `ByteSize`. +- Implement `Sub>` for `ByteSize`. +- Implement `SubAssign` for `ByteSize`. +- Implement `SubAssign>` for `ByteSize`. \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 887eacec..d467da4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::convert::TryFrom; use std::fmt::{self, Debug, Display, Formatter}; -use std::ops::{Add, AddAssign, Mul, MulAssign}; +use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}; /// byte size for 1 byte pub const B: u64 = 1; @@ -284,6 +284,43 @@ where } } +impl Sub for ByteSize { + type Output = ByteSize; + + #[inline(always)] + fn sub(self, rhs: ByteSize) -> ByteSize { + ByteSize(self.0 - rhs.0) + } +} + +impl SubAssign for ByteSize { + #[inline(always)] + fn sub_assign(&mut self, rhs: ByteSize) { + self.0 -= rhs.0 + } +} + +impl Sub for ByteSize +where + T: Into, +{ + type Output = ByteSize; + #[inline(always)] + fn sub(self, rhs: T) -> ByteSize { + ByteSize(self.0 - (rhs.into())) + } +} + +impl SubAssign for ByteSize +where + T: Into, +{ + #[inline(always)] + fn sub_assign(&mut self, rhs: T) { + self.0 -= rhs.into(); + } +} + impl Mul for ByteSize where T: Into, @@ -380,6 +417,8 @@ mod tests { assert_eq!((x + y).as_u64(), 1_100_000u64); + assert_eq!((x - y).as_u64(), 900_000u64); + assert_eq!((x + (100 * 1000) as u64).as_u64(), 1_100_000); assert_eq!((x * 2u64).as_u64(), 2_000_000); @@ -403,6 +442,14 @@ mod tests { assert_eq!((x + B as u8).as_u64(), 1_000_001); + assert_eq!((x - MB as u64).as_u64(), 0); + + assert_eq!((x - MB as u32).as_u64(), 0); + + assert_eq!((x - KB as u32).as_u64(), 999_000); + + assert_eq!((x - B as u32).as_u64(), 999_999); + x += MB as u64; x += MB as u32; x += 10u16; From 0664ffb494468e1c62f2017110cf8e4305af9753 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 20 Nov 2024 22:19:00 +0000 Subject: [PATCH 07/16] ci: fix coverage --- .github/workflows/coverage.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index fce2a78f..7d455ebb 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -33,7 +33,7 @@ jobs: - name: Upload coverage to Codecov uses: codecov/codecov-action@v4.5.0 with: - files: codecov.json fail_ci_if_error: true - env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + files: codecov.json + slug: bytesize-rs/bytesize + token: ${{ secrets.CODECOV_TOKEN }} From c3fbaeab9b1c72c957bc9b55eccf64d9499305c5 Mon Sep 17 00:00:00 2001 From: Croxx Date: Thu, 21 Nov 2024 11:47:05 +0800 Subject: [PATCH 08/16] chore: remove stale travis ci manifest (#58) Signed-off-by: MrCroxx --- .travis.yml | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ddd15862..00000000 --- a/.travis.yml +++ /dev/null @@ -1,7 +0,0 @@ -language: rust -before_script: - - rustup component add rustfmt-preview -script: - - cargo fmt --all -- --check - - cargo build --verbose --all - - cargo test --verbose --all From d65ba15f99a4e724bfdb43a358a42b51fe716ee1 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Feb 2025 22:12:45 +0000 Subject: [PATCH 09/16] chore: address clippy lints (#60) --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d467da4c..2eee6c1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -348,9 +348,9 @@ impl<'de> Deserialize<'de> for ByteSize { where D: Deserializer<'de>, { - struct ByteSizeVistor; + struct ByteSizeVisitor; - impl<'de> de::Visitor<'de> for ByteSizeVistor { + impl de::Visitor<'_> for ByteSizeVisitor { type Value = ByteSize; fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -385,9 +385,9 @@ impl<'de> Deserialize<'de> for ByteSize { } if deserializer.is_human_readable() { - deserializer.deserialize_any(ByteSizeVistor) + deserializer.deserialize_any(ByteSizeVisitor) } else { - deserializer.deserialize_u64(ByteSizeVistor) + deserializer.deserialize_u64(ByteSizeVisitor) } } } From e30fa83d059a2182576a55cd08a543081439c7ed Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Feb 2025 22:37:05 +0000 Subject: [PATCH 10/16] chore: check in lockfile (#61) --- .gitignore | 1 - CHANGELOG.md | 3 +- Cargo.lock | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 +-- 4 files changed, 199 insertions(+), 7 deletions(-) create mode 100644 Cargo.lock diff --git a/.gitignore b/.gitignore index f1731b49..83fe80ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -Cargo.lock target .idea .vscode diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f17df9a..b4f1a3e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,10 @@ # Changelog ## Unreleased + - Use SI format by default with `Display`. - Use "KiB" for SI unit. - Implement `Sub` for `ByteSize`. - Implement `Sub>` for `ByteSize`. - Implement `SubAssign` for `ByteSize`. -- Implement `SubAssign>` for `ByteSize`. \ No newline at end of file +- Implement `SubAssign>` for `ByteSize`. diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 00000000..5c3c438f --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,192 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "arbitrary" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dde20b3d026af13f561bdd0f15edf01fc734f0dafcedbaf42bba506a9517f223" +dependencies = [ + "derive_arbitrary", +] + +[[package]] +name = "bytesize" +version = "1.3.0" +dependencies = [ + "arbitrary", + "serde", + "serde_json", + "toml", +] + +[[package]] +name = "derive_arbitrary" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30542c1ad912e0e3d22a1935c290e12e8a29d704a420177a31faad4a601a0800" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + +[[package]] +name = "indexmap" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "proc-macro2" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd" + +[[package]] +name = "serde" +version = "1.0.217" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.217" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.138" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "serde_spanned" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" +dependencies = [ + "serde", +] + +[[package]] +name = "syn" +version = "2.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "toml" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd87a5cdd6ffab733b2f74bc4fd7ee5fff6634124999ac278c35fc78c6120148" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02a8b472d1a3d7c18e2d61a489aee3453fd9031c33e4f55bd533f4a7adca1bee" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + +[[package]] +name = "unicode-ident" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" + +[[package]] +name = "winnow" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86e376c75f4f43f44db463cf729e0d3acbf954d13e22c51e26e4c264b4ab545f" +dependencies = [ + "memchr", +] diff --git a/Cargo.toml b/Cargo.toml index d6c45fbd..1b8be9f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,11 @@ license = "Apache-2.0" edition = "2021" rust-version = "1.65" +[features] +default = [] +arbitrary = ["dep:arbitrary"] +serde = ["dep:serde"] + [dependencies] arbitrary = { version = "1", features = ["derive"], optional = true } serde = { version = "1", optional = true } @@ -18,8 +23,3 @@ serde = { version = "1", optional = true } serde = { version = "1", features = ["derive"] } serde_json = "1" toml = "0.8" - -[features] -arbitrary = ["dep:arbitrary"] -default = [] -serde = ["dep:serde"] From b2e3df8b8ab96cb4147d053d9614be3e4284de18 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Feb 2025 22:43:29 +0000 Subject: [PATCH 11/16] chore: move serde impls to module (#62) --- Cargo.toml | 2 +- src/lib.rs | 105 +-------------------------------------------------- src/serde.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 105 deletions(-) create mode 100644 src/serde.rs diff --git a/Cargo.toml b/Cargo.toml index 1b8be9f2..fc54e5ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ version = "1.3.0" authors = ["Hyunsik Choi ", "MrCroxx "] keywords = ["byte", "byte-size", "utility", "human-readable", "format"] categories = ["development-tools", "filesystem"] -repository = "https://github.com/hyunsik/bytesize" +repository = "https://github.com/bytesize-rs/bytesize" license = "Apache-2.0" edition = "2021" rust-version = "1.65" diff --git a/src/lib.rs b/src/lib.rs index 2eee6c1a..b098abeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,15 +28,8 @@ //! ``` mod parse; - -#[cfg(feature = "arbitrary")] -extern crate arbitrary; -#[cfg(feature = "serde")] -extern crate serde; -#[cfg(feature = "serde")] -use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "serde")] -use std::convert::TryFrom; +mod serde; use std::fmt::{self, Debug, Display, Formatter}; use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}; @@ -342,70 +335,6 @@ where } } -#[cfg(feature = "serde")] -impl<'de> Deserialize<'de> for ByteSize { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - struct ByteSizeVisitor; - - impl de::Visitor<'_> for ByteSizeVisitor { - type Value = ByteSize; - - fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str("an integer or string") - } - - fn visit_i64(self, value: i64) -> Result { - if let Ok(val) = u64::try_from(value) { - Ok(ByteSize(val)) - } else { - Err(E::invalid_value( - de::Unexpected::Signed(value), - &"integer overflow", - )) - } - } - - fn visit_u64(self, value: u64) -> Result { - Ok(ByteSize(value)) - } - - fn visit_str(self, value: &str) -> Result { - if let Ok(val) = value.parse() { - Ok(val) - } else { - Err(E::invalid_value( - de::Unexpected::Str(value), - &"parsable string", - )) - } - } - } - - if deserializer.is_human_readable() { - deserializer.deserialize_any(ByteSizeVisitor) - } else { - deserializer.deserialize_u64(ByteSizeVisitor) - } - } -} - -#[cfg(feature = "serde")] -impl Serialize for ByteSize { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - if serializer.is_human_readable() { - ::serialize(self.to_string().as_str(), serializer) - } else { - self.0.serialize(serializer) - } - } -} - #[cfg(test)] mod tests { use super::*; @@ -536,36 +465,4 @@ mod tests { fn test_to_string() { assert_to_string("609.0 PB", ByteSize::pb(609), true); } - - #[cfg(feature = "serde")] - #[test] - fn test_serde() { - #[derive(Serialize, Deserialize)] - struct S { - x: ByteSize, - } - - let s: S = serde_json::from_str(r#"{ "x": "5 B" }"#).unwrap(); - assert_eq!(s.x, ByteSize(5)); - - let s: S = serde_json::from_str(r#"{ "x": 1048576 }"#).unwrap(); - assert_eq!(s.x, "1 MiB".parse::().unwrap()); - - let s: S = toml::from_str(r#"x = "2.5 MiB""#).unwrap(); - assert_eq!(s.x, "2.5 MiB".parse::().unwrap()); - - // i64 MAX - let s: S = toml::from_str(r#"x = "9223372036854775807""#).unwrap(); - assert_eq!(s.x, "9223372036854775807".parse::().unwrap()); - } - - #[test] - #[cfg(feature = "serde")] - fn test_serde_json() { - let json = serde_json::to_string(&ByteSize::mib(1)).unwrap(); - assert_eq!(json, "\"1.0 MiB\""); - - let deserialized: ByteSize = serde_json::from_str(&json).unwrap(); - assert_eq!(deserialized.0, 1048576); - } } diff --git a/src/serde.rs b/src/serde.rs new file mode 100644 index 00000000..5cda0d38 --- /dev/null +++ b/src/serde.rs @@ -0,0 +1,103 @@ +use std::fmt; + +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; + +use crate::ByteSize; + +impl<'de> Deserialize<'de> for ByteSize { + fn deserialize(de: D) -> Result + where + D: Deserializer<'de>, + { + struct ByteSizeVisitor; + + impl de::Visitor<'_> for ByteSizeVisitor { + type Value = ByteSize; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter.write_str("an integer or string") + } + + fn visit_i64(self, value: i64) -> Result { + if let Ok(val) = u64::try_from(value) { + Ok(ByteSize(val)) + } else { + Err(E::invalid_value( + de::Unexpected::Signed(value), + &"integer overflow", + )) + } + } + + fn visit_u64(self, value: u64) -> Result { + Ok(ByteSize(value)) + } + + fn visit_str(self, value: &str) -> Result { + if let Ok(val) = value.parse() { + Ok(val) + } else { + Err(E::invalid_value( + de::Unexpected::Str(value), + &"parsable string", + )) + } + } + } + + if de.is_human_readable() { + de.deserialize_any(ByteSizeVisitor) + } else { + de.deserialize_u64(ByteSizeVisitor) + } + } +} + +impl Serialize for ByteSize { + fn serialize(&self, ser: S) -> Result + where + S: Serializer, + { + if ser.is_human_readable() { + ::serialize(self.to_string().as_str(), ser) + } else { + self.0.serialize(ser) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_serde() { + #[derive(Serialize, Deserialize)] + struct S { + x: ByteSize, + } + + let s = serde_json::from_str::(r#"{ "x": "5 B" }"#).unwrap(); + assert_eq!(s.x, ByteSize(5)); + + let s = serde_json::from_str::(r#"{ "x": 1048576 }"#).unwrap(); + assert_eq!(s.x, "1 MiB".parse::().unwrap()); + + let s = toml::from_str::(r#"x = "2.5 MiB""#).unwrap(); + assert_eq!(s.x, "2.5 MiB".parse::().unwrap()); + + // i64 MAX + let s = toml::from_str::(r#"x = "9223372036854775807""#).unwrap(); + assert_eq!(s.x, "9223372036854775807".parse::().unwrap()); + } + + #[test] + + fn test_serde_json() { + let json = serde_json::to_string(&ByteSize::mib(1)).unwrap(); + assert_eq!(json, "\"1.0 MiB\""); + + let deserialized = serde_json::from_str::(&json).unwrap(); + assert_eq!(deserialized.0, 1048576); + } +} From 38536c37ac0ac09d06d0078615fd40befb985379 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Feb 2025 22:56:27 +0000 Subject: [PATCH 12/16] chore: don't show failed CI for codecov issues (#64) --- codecov.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 codecov.yml diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 00000000..87f4f27e --- /dev/null +++ b/codecov.yml @@ -0,0 +1,16 @@ +comment: false + +coverage: + status: + project: + default: + threshold: 100% # make CI green + patch: + default: + threshold: 100% # make CI green + +# ignore code coverage on following paths +ignore: + - "**/tests" + - "**/benches" + - "**/examples" From 54a1156c3de355b54a1f09a726fe56320ba09053 Mon Sep 17 00:00:00 2001 From: Stefan Holderbach Date: Mon, 10 Feb 2025 00:20:22 +0100 Subject: [PATCH 13/16] Fix overly permissive parsing after whitespace (#59) * Fix parsing logic after first whitespace The way the parse was implemented accepted additional numeric characters or `.` after the first valid `f64` literal but ignored them. This permitted more strings that are invalid following a strict grammar for byte sizes and silently ignores the further symbols without error. ``` 1.0 ...KB 1.0 42.0 B ``` This change makes those illegal. `1 000 B` was also subject to the bad `skip_while` ignoring the following `000`. In this version of the fix whitespace is not accepted as a digit separator. So it will raise an error if the user doesn't explicitly strip the whitespace instead of reporting a wrong value. * Add tests for invalid chars after whitespace * Add test documenting that whitespace is not a valid digit separator More following the old choices of using `f64::from_str` --------- Co-authored-by: Rob Ede --- src/parse.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/parse.rs b/src/parse.rs index 10d783f3..93afdb23 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -10,9 +10,7 @@ impl std::str::FromStr for ByteSize { let number = take_while(value, |c| c.is_ascii_digit() || c == '.'); match number.parse::() { Ok(v) => { - let suffix = skip_while(value, |c| { - c.is_whitespace() || c.is_ascii_digit() || c == '.' - }); + let suffix = skip_while(&value[number.len()..], char::is_whitespace); match suffix.parse::() { Ok(u) => Ok(Self((v * u) as u64)), Err(error) => Err(format!( @@ -219,6 +217,11 @@ mod tests { assert!(parse("").is_err()); assert!(parse("a124GB").is_err()); + assert!(parse("1.3 42.0 B").is_err()); + assert!(parse("1.3 ... B").is_err()); + // The original implementation did not account for the possibility that users may + // use whitespace to visually separate digits, thus treat it as an error + assert!(parse("1 000 B").is_err()); } #[test] From 96851deb1719558cf36ffbdd4bf3d13b840684e7 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Feb 2025 23:34:03 +0000 Subject: [PATCH 14/16] docs: update changelog (#66) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4f1a3e9..b1378a30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,3 +8,4 @@ - Implement `Sub>` for `ByteSize`. - Implement `SubAssign` for `ByteSize`. - Implement `SubAssign>` for `ByteSize`. +- Reject parsing non-unit characters after whitespace. From 05a48ff8667121460d83bbf831c2dd7342719f72 Mon Sep 17 00:00:00 2001 From: Croxx Date: Mon, 18 Nov 2024 19:08:42 +0800 Subject: [PATCH 15/16] test: add unit test for json serde (#54) --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index b098abeb..89c488ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -465,4 +465,14 @@ mod tests { fn test_to_string() { assert_to_string("609.0 PB", ByteSize::pb(609), true); } + + #[test] + #[cfg(feature = "serde")] + fn test_serde_json() { + let json = serde_json::to_string(&ByteSize::mib(1)).unwrap(); + assert_eq!(json, "\"1.0 MiB\""); + + let deserialized: ByteSize = serde_json::from_str(&json).unwrap(); + assert_eq!(deserialized.0, 1048576); + } } From a1eb7b5f2ba871d80c2617d18d926387c48a4884 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 9 Feb 2025 22:43:29 +0000 Subject: [PATCH 16/16] chore: move serde impls to module (#62) --- src/lib.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 89c488ea..b098abeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -465,14 +465,4 @@ mod tests { fn test_to_string() { assert_to_string("609.0 PB", ByteSize::pb(609), true); } - - #[test] - #[cfg(feature = "serde")] - fn test_serde_json() { - let json = serde_json::to_string(&ByteSize::mib(1)).unwrap(); - assert_eq!(json, "\"1.0 MiB\""); - - let deserialized: ByteSize = serde_json::from_str(&json).unwrap(); - assert_eq!(deserialized.0, 1048576); - } }