From a73f43a3697844d875455c8e1def0679b1fe5e90 Mon Sep 17 00:00:00 2001 From: Asmir Avdicevic Date: Mon, 25 Nov 2024 11:51:02 +0100 Subject: [PATCH] code cleanup --- .github/workflows/ci.yml | 6 +++--- Cargo.lock | 7 +++++++ iroh/Cargo.toml | 7 ++++++- iroh/examples/transfer.rs | 32 ++++++++------------------------ 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 53888fa4a7..16daebb147 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -246,13 +246,13 @@ jobs: # TODO: We have a bunch of platform-dependent code so should # probably run this job on the full platform matrix - name: clippy check (all features) - run: cargo clippy --workspace --all-features --all-targets --bins --tests --benches + run: cargo clippy --workspace --all-features --all-targets --bins --tests --benches --examples - name: clippy check (no features) - run: cargo clippy --workspace --no-default-features --lib --bins --tests + run: cargo clippy --workspace --no-default-features --lib --bins --tests --examples - name: clippy check (default features) - run: cargo clippy --workspace --all-targets + run: cargo clippy --workspace --all-targets --examples msrv: if: "github.event_name != 'pull_request' || ! contains(github.event.pull_request.labels.*.name, 'flaky-test')" diff --git a/Cargo.lock b/Cargo.lock index 409947558a..6a7e13a716 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2499,6 +2499,7 @@ dependencies = [ "nested_enum_utils", "num_cpus", "parking_lot", + "parse-size", "postcard", "proptest", "quic-rpc", @@ -3655,6 +3656,12 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "parse-size" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "487f2ccd1e17ce8c1bfab3a65c89525af41cfad4c8659021a1e9a2aacd73b89b" + [[package]] name = "paste" version = "1.0.15" diff --git a/iroh/Cargo.toml b/iroh/Cargo.toml index 91b2350171..14a790323b 100644 --- a/iroh/Cargo.toml +++ b/iroh/Cargo.toml @@ -62,6 +62,7 @@ ref-cast = "1.0.23" # Examples clap = { version = "4", features = ["derive"], optional = true } indicatif = { version = "0.17", features = ["tokio"], optional = true } +parse-size = { version = "1.1.0", optional = true } # Documentation tests url = { version = "2.5.0", features = ["serde"] } @@ -74,7 +75,7 @@ test = [] discovery-pkarr-dht = ["iroh-net/discovery-pkarr-dht"] test-utils = ["iroh-net/test-utils"] -examples = ["dep:clap", "dep:indicatif"] +examples = ["dep:clap", "dep:indicatif", "dep:parse-size"] [dev-dependencies] anyhow = { version = "1" } @@ -101,3 +102,7 @@ rustdoc-args = ["--cfg", "iroh_docsrs"] [[example]] name = "rpc" required-features = ["examples"] + +[[example]] +name = "transfer" +required-features = ["examples"] diff --git a/iroh/examples/transfer.rs b/iroh/examples/transfer.rs index 1e0c35d89c..957b584612 100644 --- a/iroh/examples/transfer.rs +++ b/iroh/examples/transfer.rs @@ -7,6 +7,7 @@ use anyhow::{Context, Result}; use bytes::Bytes; use clap::{Parser, Subcommand}; use futures_lite::StreamExt; +use indicatif::HumanBytes; use iroh_net::{ key::SecretKey, ticket::NodeTicket, Endpoint, NodeAddr, RelayMap, RelayMode, RelayUrl, }; @@ -213,10 +214,10 @@ async fn fetch(ticket: &str, relay_url: Option) -> anyhow::Result<()> { len, dur, duration, chnk ); println!( - "Transferred {} B in {} seconds, {} B/s", - len, + "Transferred {} in {:.4}, {}/s", + HumanBytes(len as u64), duration.as_secs_f64(), - len as f64 / duration.as_secs_f64() + HumanBytes((len as f64 / duration.as_secs_f64()) as u64) ); Ok(()) @@ -274,7 +275,7 @@ async fn send_data_on_stream( stream: &mut iroh_net::endpoint::SendStream, stream_size: u64, ) -> Result<()> { - const DATA: &[u8] = &[0xAB; 7 * 1024 * 1024]; + const DATA: &[u8] = &[0xAB; 1024 * 1024]; let bytes_data = Bytes::from_static(DATA); let full_chunks = stream_size / (DATA.len() as u64); @@ -303,24 +304,7 @@ async fn send_data_on_stream( Ok(()) } -fn parse_byte_size(s: &str) -> Result { - let s = s.trim(); - - let multiplier = match s.chars().last() { - Some('T') => 1024 * 1024 * 1024 * 1024, - Some('G') => 1024 * 1024 * 1024, - Some('M') => 1024 * 1024, - Some('k') => 1024, - _ => 1, - }; - - let s = if multiplier != 1 { - &s[..s.len() - 1] - } else { - s - }; - - let base: u64 = u64::from_str(s)?; - - Ok(base * multiplier) +fn parse_byte_size(s: &str) -> Result { + let cfg = parse_size::Config::new().with_binary(); + cfg.parse_size(s).map_err(Into::into) }