diff --git a/Cargo.Bazel.json.lock b/Cargo.Bazel.json.lock index 19308a0fea8d..61ac8fa0ccc5 100644 --- a/Cargo.Bazel.json.lock +++ b/Cargo.Bazel.json.lock @@ -1,5 +1,5 @@ { - "checksum": "d219ecf3003ae760b2a967cbf6532e80a278a2640cd74d57ab32af145fae636f", + "checksum": "5c79068fe3c1bb4d83af7406cb7b26fab7ec5aaaa978d05bdd40e2ba89e5d243", "crates": { "actix-codec 0.5.1": { "name": "actix-codec", @@ -4711,67 +4711,6 @@ ], "license_file": "LICENSE-APACHE" }, - "atty 0.2.14": { - "name": "atty", - "version": "0.2.14", - "package_url": "https://github.com/softprops/atty", - "repository": { - "Http": { - "url": "https://static.crates.io/crates/atty/0.2.14/download", - "sha256": "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" - } - }, - "targets": [ - { - "Library": { - "crate_name": "atty", - "crate_root": "src/lib.rs", - "srcs": { - "allow_empty": true, - "include": [ - "**/*.rs" - ] - } - } - } - ], - "library_target_name": "atty", - "common_attrs": { - "compile_data_glob": [ - "**" - ], - "deps": { - "common": [], - "selects": { - "cfg(target_os = \"hermit\")": [ - { - "id": "hermit-abi 0.1.19", - "target": "hermit_abi" - } - ], - "cfg(unix)": [ - { - "id": "libc 0.2.186", - "target": "libc" - } - ], - "cfg(windows)": [ - { - "id": "winapi 0.3.9", - "target": "winapi" - } - ] - } - }, - "edition": "2015", - "version": "0.2.14" - }, - "license": "MIT", - "license_ids": [ - "MIT" - ], - "license_file": "LICENSE" - }, "auto_impl 1.1.0": { "name": "auto_impl", "version": "1.1.0", @@ -20538,10 +20477,6 @@ "id": "predicates 3.1.2", "target": "predicates" }, - { - "id": "pretty-bytes 0.2.2", - "target": "pretty_bytes" - }, { "id": "pretty_assertions 1.4.0", "target": "pretty_assertions" @@ -29923,54 +29858,6 @@ ], "license_file": "LICENSE-APACHE" }, - "hermit-abi 0.1.19": { - "name": "hermit-abi", - "version": "0.1.19", - "package_url": "https://github.com/hermitcore/libhermit-rs", - "repository": { - "Http": { - "url": "https://static.crates.io/crates/hermit-abi/0.1.19/download", - "sha256": "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" - } - }, - "targets": [ - { - "Library": { - "crate_name": "hermit_abi", - "crate_root": "src/lib.rs", - "srcs": { - "allow_empty": true, - "include": [ - "**/*.rs" - ] - } - } - } - ], - "library_target_name": "hermit_abi", - "common_attrs": { - "compile_data_glob": [ - "**" - ], - "deps": { - "common": [ - { - "id": "libc 0.2.186", - "target": "libc" - } - ], - "selects": {} - }, - "edition": "2018", - "version": "0.1.19" - }, - "license": "MIT/Apache-2.0", - "license_ids": [ - "Apache-2.0", - "MIT" - ], - "license_file": "LICENSE-APACHE" - }, "hermit-abi 0.3.9": { "name": "hermit-abi", "version": "0.3.9", @@ -55384,57 +55271,6 @@ ], "license_file": "LICENSE" }, - "pretty-bytes 0.2.2": { - "name": "pretty-bytes", - "version": "0.2.2", - "package_url": "https://github.com/banyan/rust-pretty-bytes", - "repository": { - "Http": { - "url": "https://static.crates.io/crates/pretty-bytes/0.2.2/download", - "sha256": "009d6edd2c1dbf2e1c0cd48a2f7766e03498d49ada7109a01c6911815c685316" - } - }, - "targets": [ - { - "Library": { - "crate_name": "pretty_bytes", - "crate_root": "src/lib.rs", - "srcs": { - "allow_empty": true, - "include": [ - "**/*.rs" - ] - } - } - } - ], - "library_target_name": "pretty_bytes", - "common_attrs": { - "compile_data_glob": [ - "**" - ], - "deps": { - "common": [ - { - "id": "atty 0.2.14", - "target": "atty" - }, - { - "id": "getopts 0.2.24", - "target": "getopts" - } - ], - "selects": {} - }, - "edition": "2015", - "version": "0.2.2" - }, - "license": "MIT", - "license_ids": [ - "MIT" - ], - "license_file": null - }, "pretty_assertions 1.4.0": { "name": "pretty_assertions", "version": "1.4.0", @@ -93053,7 +92889,6 @@ "pkg-config 0.3.32", "pprof 0.15.0", "predicates 3.1.2", - "pretty-bytes 0.2.2", "pretty_assertions 1.4.0", "priority-queue 1.3.2", "proc-macro2 1.0.106", diff --git a/Cargo.Bazel.toml.lock b/Cargo.Bazel.toml.lock index d725e5bd7046..fe00da0299f6 100644 --- a/Cargo.Bazel.toml.lock +++ b/Cargo.Bazel.toml.lock @@ -832,17 +832,6 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "auto_impl" version = "1.1.0" @@ -3486,7 +3475,6 @@ dependencies = [ "pkg-config", "pprof", "predicates", - "pretty-bytes", "pretty_assertions", "priority-queue", "proc-macro2", @@ -5077,15 +5065,6 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - [[package]] name = "hermit-abi" version = "0.3.9" @@ -9338,16 +9317,6 @@ dependencies = [ "unicode-segmentation", ] -[[package]] -name = "pretty-bytes" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "009d6edd2c1dbf2e1c0cd48a2f7766e03498d49ada7109a01c6911815c685316" -dependencies = [ - "atty", - "getopts", -] - [[package]] name = "pretty_assertions" version = "1.4.0" diff --git a/Cargo.lock b/Cargo.lock index 28f29c444e74..f8feca8c2b3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -871,17 +871,6 @@ dependencies = [ "x509-cert", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "auto_impl" version = "1.3.0" @@ -5518,15 +5507,6 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] - [[package]] name = "hermit-abi" version = "0.5.2" @@ -13925,7 +13905,6 @@ dependencies = [ "maplit", "num-traits", "on_wire", - "pretty-bytes", "pretty_assertions", "proptest", "prost", @@ -16598,7 +16577,7 @@ version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" dependencies = [ - "hermit-abi 0.5.2", + "hermit-abi", "libc", "windows-sys 0.61.2", ] @@ -18585,7 +18564,7 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" dependencies = [ - "hermit-abi 0.5.2", + "hermit-abi", "libc", ] @@ -19746,7 +19725,7 @@ checksum = "5d0e4f59085d47d8241c88ead0f274e8a0cb551f3625263c05eb8dd897c34218" dependencies = [ "cfg-if", "concurrent-queue", - "hermit-abi 0.5.2", + "hermit-abi", "pin-project-lite", "rustix 1.1.4", "windows-sys 0.61.2", @@ -19918,16 +19897,6 @@ dependencies = [ "unicode-width 0.2.0", ] -[[package]] -name = "pretty-bytes" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "009d6edd2c1dbf2e1c0cd48a2f7766e03498d49ada7109a01c6911815c685316" -dependencies = [ - "atty", - "getopts", -] - [[package]] name = "pretty_assertions" version = "1.4.1" diff --git a/bazel/rust.MODULE.bazel b/bazel/rust.MODULE.bazel index 9b5c20fe8186..27b01eca8ceb 100644 --- a/bazel/rust.MODULE.bazel +++ b/bazel/rust.MODULE.bazel @@ -1213,10 +1213,6 @@ crate.spec( package = "predicates", version = "^3.1.2", ) -crate.spec( - package = "pretty-bytes", - version = "^0.2.2", -) crate.spec( package = "pretty_assertions", version = "^1.4.0", diff --git a/rs/sns/integration_tests/BUILD.bazel b/rs/sns/integration_tests/BUILD.bazel index a9e44189b7e0..d6f2dc79595f 100644 --- a/rs/sns/integration_tests/BUILD.bazel +++ b/rs/sns/integration_tests/BUILD.bazel @@ -37,7 +37,6 @@ BASE_DEPENDENCIES = [ "//rs/universal_canister/lib", "@crate_index//:candid", "@crate_index//:maplit", - "@crate_index//:pretty-bytes", "@crate_index//:proptest", "@crate_index//:prost", "@crate_index//:rand", @@ -123,7 +122,6 @@ rust_canister( "//rs/sns/governance", "//rs/types/base_types", "@crate_index//:ic-stable-structures", - "@crate_index//:pretty-bytes", "@crate_index//:prost", "@crate_index//:rand", ], diff --git a/rs/sns/integration_tests/Cargo.toml b/rs/sns/integration_tests/Cargo.toml index 7363377dcb27..7997b6d59b58 100644 --- a/rs/sns/integration_tests/Cargo.toml +++ b/rs/sns/integration_tests/Cargo.toml @@ -49,7 +49,6 @@ ic-nns-test-utils-golden-nns-state = { path = "../../nns/test_utils/golden_nns_s ic-universal-canister = { path = "../../universal_canister/lib" } icrc-ledger-types = { path = "../../../packages/icrc-ledger-types" } maplit = { workspace = true } -pretty-bytes = "0.2.2" proptest = { workspace = true } prost = { workspace = true } rand = { workspace = true } diff --git a/rs/sns/integration_tests/test_canisters/sns_governance_mem_test_canister.rs b/rs/sns/integration_tests/test_canisters/sns_governance_mem_test_canister.rs index 7f28cf4a83f9..547bd08a05dc 100644 --- a/rs/sns/integration_tests/test_canisters/sns_governance_mem_test_canister.rs +++ b/rs/sns/integration_tests/test_canisters/sns_governance_mem_test_canister.rs @@ -34,7 +34,6 @@ use ic_stable_structures::{ memory_manager::{MemoryId, MemoryManager, VirtualMemory}, }; use icrc_ledger_types::icrc1::account::Subaccount; -use pretty_bytes::converter; use rand::{RngCore, SeedableRng, rngs::StdRng}; use std::{cell::RefCell, collections::BTreeMap}; @@ -131,8 +130,32 @@ fn proposal_action_iterator() -> impl Iterator { /// Provide human readable String given a count of bytes. /// Ex: 4240000 -> "4.24 MB" +/// +/// Uses SI units (base 1000) and rounds to at most 2 decimal places, dropping +/// any trailing zeros (e.g. 4_000_000 -> "4 MB", not "4.00 MB"). fn pretty_bytes(bytes: usize) -> String { - converter::convert(bytes as f64) + const UNITS: [&str; 9] = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; + + // This avoids later doing 0_f64.ln(), which (in math) has NO result. + if bytes == 0 { + return "0 B".to_string(); + } + let bytes = bytes as f64; + + let delimiter = 1000_f64; + // bytes.ln() / delimiter.ln() is log_1000(bytes), i.e. log10(bytes) / 3. + let exponent = std::cmp::min( + (bytes.ln() / delimiter.ln()).floor() as i32, + (UNITS.len() - 1) as i32, + ); + + // Round to 2 decimals, then trim any trailing zeros and a trailing decimal point. + let value = format!("{:.2}", bytes / delimiter.powi(exponent)); + let value = value.trim_end_matches('0').trim_end_matches('.'); + + let unit = UNITS[exponent as usize]; + + format!("{} {}", value, unit) } /// Create a vector of NeuronIds. This is an expensive enough operation to do