Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Arqu committed Nov 25, 2024
1 parent 42c8cea commit a73f43a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion iroh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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" }
Expand All @@ -101,3 +102,7 @@ rustdoc-args = ["--cfg", "iroh_docsrs"]
[[example]]
name = "rpc"
required-features = ["examples"]

[[example]]
name = "transfer"
required-features = ["examples"]
32 changes: 8 additions & 24 deletions iroh/examples/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -213,10 +214,10 @@ async fn fetch(ticket: &str, relay_url: Option<String>) -> 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(())
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -303,24 +304,7 @@ async fn send_data_on_stream(
Ok(())
}

fn parse_byte_size(s: &str) -> Result<u64, std::num::ParseIntError> {
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<u64> {
let cfg = parse_size::Config::new().with_binary();
cfg.parse_size(s).map_err(Into::into)
}

0 comments on commit a73f43a

Please sign in to comment.