Skip to content

Commit

Permalink
chore: Format imports using rustfmt (#2812)
Browse files Browse the repository at this point in the history
## Description

This enables a strict style in rustfmt to format imports statements. It
needs rust nightly for rustfmt to be able to do this.

To not make the `cargo fmt` from stable rust really noisy it does not
add a rustfmt.toml file, instead it adds a `cargo-make` task to automate
running this locally. This same task is used in CI to run the formatter.

The `Crate`m formatting will be preserved by the default configuration
of rust-analyzer, which makes external contributions easier as well,
most folks shouldn't be running into this formatter.

## Breaking Changes

<!-- Optional, if there are any breaking changes document them,
including how to migrate older code. -->

## Notes & open questions

I guess we need buy-in from everyone on the team for this.

## Change checklist

- [x] Self-review.
- [x] Documentation updates following the [style
guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text),
if relevant.
- [x] Tests if relevant.
- [x] All breaking changes documented.

---------

Co-authored-by: Diva M <[email protected]>
Co-authored-by: Divma <[email protected]>
  • Loading branch information
3 people authored Oct 18, 2024
1 parent 615cbd6 commit 38a0745
Show file tree
Hide file tree
Showing 30 changed files with 175 additions and 156 deletions.
28 changes: 28 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Use cargo-make to run tasks here: https://crates.io/crates/cargo-make

[tasks.format]
workspace = false
command = "cargo"
args = [
"fmt",
"--all",
"--",
"--config",
"unstable_features=true",
"--config",
"imports_granularity=Crate,group_imports=StdExternalCrate,reorder_imports=true",
]

[tasks.format-check]
workspace = false
command = "cargo"
args = [
"fmt",
"--all",
"--check",
"--",
"--config",
"unstable_features=true",
"--config",
"imports_granularity=Crate,group_imports=StdExternalCrate,reorder_imports=true",
]
3 changes: 2 additions & 1 deletion iroh-blobs/examples/connect/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Common code used to created quinn connections in the examples
use std::{path::PathBuf, sync::Arc};

use anyhow::{bail, Context, Result};
use quinn::crypto::rustls::{QuicClientConfig, QuicServerConfig};
use std::{path::PathBuf, sync::Arc};
use tokio::fs;

pub const EXAMPLE_ALPN: &[u8] = b"n0/iroh/examples/bytes/0";
Expand Down
5 changes: 2 additions & 3 deletions iroh-blobs/examples/fetch-fsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
use std::net::SocketAddr;

use anyhow::{Context, Result};
use iroh_io::ConcatenateSliceWriter;
use tracing_subscriber::{prelude::*, EnvFilter};

use iroh_blobs::{
get::fsm::{AtInitial, ConnectedNext, EndBlobNext},
hashseq::HashSeq,
protocol::GetRequest,
Hash,
};
use iroh_io::ConcatenateSliceWriter;
use tracing_subscriber::{prelude::*, EnvFilter};

mod connect;
use connect::{load_certs, make_client_endpoint};
Expand Down
13 changes: 4 additions & 9 deletions iroh-blobs/examples/fetch-stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
//! Since this example does not use [`iroh-net::Endpoint`], it does not do any holepunching, and so will only work locally or between two processes that have public IP addresses.
//!
//! Run the provide-bytes example first. It will give instructions on how to run this example properly.
use std::net::SocketAddr;
use std::{io, net::SocketAddr};

use anyhow::{Context, Result};
use tracing_subscriber::{prelude::*, EnvFilter};

use std::io;

use bao_tree::io::fsm::BaoContentItem;
use bytes::Bytes;
use futures_lite::{Stream, StreamExt};
use genawaiter::sync::Co;
use genawaiter::sync::Gen;
use tokio::io::AsyncWriteExt;

use genawaiter::sync::{Co, Gen};
use iroh_blobs::{
get::fsm::{AtInitial, BlobContentNext, ConnectedNext, EndBlobNext},
hashseq::HashSeq,
protocol::GetRequest,
Hash,
};
use tokio::io::AsyncWriteExt;
use tracing_subscriber::{prelude::*, EnvFilter};

mod connect;
use connect::{load_certs, make_client_endpoint};
Expand Down
3 changes: 1 addition & 2 deletions iroh-blobs/examples/provide-bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
//! cargo run --example provide-bytes collection
//! To provide a collection (multiple blobs)
use anyhow::Result;
use iroh_blobs::{format::collection::Collection, util::local_pool::LocalPool, Hash};
use tracing::warn;
use tracing_subscriber::{prelude::*, EnvFilter};

use iroh_blobs::{format::collection::Collection, util::local_pool::LocalPool, Hash};

mod connect;
use connect::{make_and_write_certs, make_server_endpoint, CERT_PATH};

Expand Down
11 changes: 6 additions & 5 deletions iroh-blobs/src/downloader/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
//!
//! [`Connection`]: iroh_net::endpoint::Connection
use crate::{
get::{db::get_to_db_in_steps, error::GetError},
store::Store,
};
use futures_lite::FutureExt;
use iroh_net::endpoint;

use super::{progress::BroadcastProgressSender, DownloadKind, FailureAction, GetStartFut, Getter};
use crate::{
get::{db::get_to_db_in_steps, error::GetError},
store::Store,
};

impl From<GetError> for FailureAction {
fn from(e: GetError) -> Self {
Expand Down Expand Up @@ -74,8 +74,9 @@ impl super::NeedsConn<endpoint::Connection> for crate::get::db::GetStateNeedsCon

#[cfg(feature = "metrics")]
fn track_metrics(res: &Result<crate::get::Stats, GetError>) {
use crate::metrics::Metrics;
use iroh_metrics::{inc, inc_by};

use crate::metrics::Metrics;
match res {
Ok(stats) => {
let crate::get::Stats {
Expand Down
3 changes: 1 addition & 2 deletions iroh-blobs/src/downloader/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ use std::{
use anyhow::anyhow;
use parking_lot::Mutex;

use super::DownloadKind;
use crate::{
get::{db::DownloadProgress, progress::TransferState},
util::progress::{AsyncChannelProgressSender, IdGenerator, ProgressSendError, ProgressSender},
};

use super::DownloadKind;

/// The channel that can be used to subscribe to progress updates.
pub type ProgressSubscriber = AsyncChannelProgressSender<DownloadProgress>;

Expand Down
5 changes: 2 additions & 3 deletions iroh-blobs/src/downloader/test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#![cfg(test)]
use anyhow::anyhow;
use std::{
sync::atomic::AtomicUsize,
time::{Duration, Instant},
};

use anyhow::anyhow;
use futures_util::future::FutureExt;
use iroh_net::key::SecretKey;

use super::*;
use crate::{
get::{
db::BlobId,
Expand All @@ -19,8 +20,6 @@ use crate::{
},
};

use super::*;

mod dialer;
mod getter;

Expand Down
3 changes: 1 addition & 2 deletions iroh-blobs/src/downloader/test/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use futures_lite::{future::Boxed as BoxFuture, FutureExt};
use parking_lot::RwLock;

use crate::downloader;

use super::*;
use crate::downloader;

#[derive(Default, Clone, derive_more::Debug)]
#[debug("TestingGetter")]
Expand Down
33 changes: 17 additions & 16 deletions iroh-blobs/src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
//! or you can choose to finish early.
//!
//! [iroh-net]: https://docs.rs/iroh-net
use std::error::Error;
use std::fmt::{self, Debug};
use std::time::{Duration, Instant};
use std::{
error::Error,
fmt::{self, Debug},
time::{Duration, Instant},
};

use crate::Hash;
use anyhow::Result;
use bao_tree::io::fsm::BaoContentItem;
use bao_tree::ChunkNum;
use bao_tree::{io::fsm::BaoContentItem, ChunkNum};
use iroh_net::endpoint::{self, RecvStream, SendStream};
use serde::{Deserialize, Serialize};
use tracing::{debug, error};

use crate::protocol::RangeSpecSeq;
use crate::util::io::{TrackingReader, TrackingWriter};
use crate::IROH_BLOCK_SIZE;
use crate::{
protocol::RangeSpecSeq,
util::io::{TrackingReader, TrackingWriter},
Hash, IROH_BLOCK_SIZE,
};

pub mod db;
pub mod error;
Expand Down Expand Up @@ -59,13 +61,6 @@ impl Stats {
pub mod fsm {
use std::{io, result};

use crate::{
protocol::{GetRequest, NonEmptyRequestRangeSpecIter, Request, MAX_MESSAGE_SIZE},
store::BaoBatchWriter,
};

use super::*;

use bao_tree::{
io::fsm::{OutboardMut, ResponseDecoder, ResponseDecoderNext},
BaoTree, ChunkRanges, TreeNode,
Expand All @@ -75,6 +70,12 @@ pub mod fsm {
use iroh_net::endpoint::Connection;
use tokio::io::AsyncWriteExt;

use super::*;
use crate::{
protocol::{GetRequest, NonEmptyRequestRangeSpecIter, Request, MAX_MESSAGE_SIZE},
store::BaoBatchWriter,
};

type WrappedRecvStream = TrackingReader<TokioStreamReader<RecvStream>>;

self_cell::self_cell! {
Expand Down
30 changes: 12 additions & 18 deletions iroh-blobs/src/get/db.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
//! Functions that use the iroh-blobs protocol in conjunction with a bao store.
use std::future::Future;
use std::io;
use std::num::NonZeroU64;
use std::pin::Pin;
use std::{future::Future, io, num::NonZeroU64, pin::Pin};

use anyhow::anyhow;
use bao_tree::{ChunkNum, ChunkRanges};
use futures_lite::StreamExt;
use genawaiter::{
rc::{Co, Gen},
GeneratorState,
};
use iroh_base::hash::Hash;
use iroh_base::rpc::RpcError;
use iroh_base::{hash::Hash, rpc::RpcError};
use iroh_io::AsyncSliceReader;
use iroh_net::endpoint::Connection;
use serde::{Deserialize, Serialize};
use tokio::sync::oneshot;

use crate::hashseq::parse_hash_seq;
use crate::protocol::RangeSpec;
use crate::store::BaoBatchWriter;
use crate::store::BaoBlobSize;
use crate::store::FallibleProgressBatchWriter;
use tracing::trace;

use crate::{
get::{
Expand All @@ -30,15 +24,15 @@ use crate::{
progress::TransferState,
Stats,
},
protocol::{GetRequest, RangeSpecSeq},
store::{MapEntry, MapEntryMut, MapMut, Store as BaoStore},
hashseq::parse_hash_seq,
protocol::{GetRequest, RangeSpec, RangeSpecSeq},
store::{
BaoBatchWriter, BaoBlobSize, FallibleProgressBatchWriter, MapEntry, MapEntryMut, MapMut,
Store as BaoStore,
},
util::progress::{IdGenerator, ProgressSender},
BlobFormat, HashAndFormat,
};
use anyhow::anyhow;
use bao_tree::{ChunkNum, ChunkRanges};
use iroh_io::AsyncSliceReader;
use tracing::trace;

type GetGenerator = Gen<Yield, (), Pin<Box<dyn Future<Output = Result<Stats, GetError>>>>>;
type GetFuture = Pin<Box<dyn Future<Output = Result<Stats, GetError>> + 'static>>;
Expand Down
3 changes: 1 addition & 2 deletions iroh-blobs/src/get/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use std::{collections::HashMap, num::NonZeroU64};
use serde::{Deserialize, Serialize};
use tracing::warn;

use crate::{protocol::RangeSpec, store::BaoBlobSize, Hash};

use super::db::{BlobId, DownloadProgress};
use crate::{protocol::RangeSpec, store::BaoBlobSize, Hash};

/// The identifier for progress events.
pub type ProgressId = u64;
Expand Down
10 changes: 5 additions & 5 deletions iroh-blobs/src/get/request.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
//! Utilities for complex get requests.
use std::sync::Arc;

use crate::{
hashseq::HashSeq,
protocol::{GetRequest, RangeSpecSeq},
Hash, HashAndFormat,
};
use bao_tree::{ChunkNum, ChunkRanges};
use bytes::Bytes;
use iroh_net::endpoint::Connection;
use rand::Rng;

use super::{fsm, Stats};
use crate::{
hashseq::HashSeq,
protocol::{GetRequest, RangeSpecSeq},
Hash, HashAndFormat,
};

/// Get the claimed size of a blob from a peer.
///
Expand Down
6 changes: 4 additions & 2 deletions iroh-blobs/src/hashseq.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! traits related to collections of blobs
use crate::Hash;
use std::{fmt::Debug, io};

use bytes::Bytes;
use iroh_io::{AsyncSliceReader, AsyncSliceReaderExt};
use std::{fmt::Debug, io};

use crate::Hash;

/// A sequence of links, backed by a [`Bytes`] object.
#[derive(Debug, Clone, derive_more::Into)]
Expand Down
4 changes: 2 additions & 2 deletions iroh-blobs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ pub mod provider;
pub mod store;
pub mod util;

pub use crate::util::{Tag, TempTag};
use bao_tree::BlockSize;
pub use iroh_base::hash::{BlobFormat, Hash, HashAndFormat};

use bao_tree::BlockSize;
pub use crate::util::{Tag, TempTag};

/// Block size used by iroh, 2^4*1024 = 16KiB
pub const IROH_BLOCK_SIZE: BlockSize = BlockSize::from_chunk_log(4);
3 changes: 2 additions & 1 deletion iroh-blobs/src/protocol/range_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,11 @@ impl<'a> Iterator for NonEmptyRequestRangeSpecIter<'a> {
mod tests {
use std::ops::Range;

use super::*;
use iroh_test::{assert_eq_hex, hexdump::parse_hexdump};
use proptest::prelude::*;

use super::*;

fn ranges(value_range: Range<u64>) -> impl Strategy<Value = ChunkRanges> {
prop::collection::vec((value_range.clone(), value_range), 0..16).prop_map(|v| {
let mut res = ChunkRanges::empty();
Expand Down
Loading

0 comments on commit 38a0745

Please sign in to comment.