Skip to content

Commit f3c71f8

Browse files
committed
Merge #47: chore: fix clippy and rust problems
ee84bfc ci: update workflows (Cameron Garnham) fcd3d96 chore: fix clippy and rust problems (Cameron Garnham) 7a3787c cargo: remove unused deps (Cameron Garnham) Pull request description: Fix Clippy Warnings for entire Project and Update Testing Workflow. ACKs for top commit: da2ce7: ACK ee84bfc Tree-SHA512: b16fc434552649d6c3479ea9f98d1c949454e278851928b2350a2ee3ea7cb4b303067aa52f1a761d8552ef5dab794f07b8141d5fa2a910a0e81848dd4d488842
2 parents 6b7bcc9 + ee84bfc commit f3c71f8

File tree

135 files changed

+1421
-693
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+1421
-693
lines changed

.github/workflows/coverage.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
steps:
2020
- id: checkout
2121
name: Checkout Repository
22-
uses: actions/checkout@v3
22+
uses: actions/checkout@v4
2323

2424
- id: setup
2525
name: Setup Toolchain
@@ -46,7 +46,7 @@ jobs:
4646

4747
- id: upload
4848
name: Upload Coverage Report
49-
uses: codecov/codecov-action@v3
49+
uses: codecov/codecov-action@v4
5050
with:
5151
token: ${{ secrets.CODECOV_TOKEN }}
5252
files: ${{ steps.coverage.outputs.report }}

.github/workflows/testing.yaml

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
steps:
1616
- id: checkout
1717
name: Checkout Repository
18-
uses: actions/checkout@v3
18+
uses: actions/checkout@v4
1919

2020
- id: setup
2121
name: Setup Toolchain
@@ -39,71 +39,76 @@ jobs:
3939

4040
strategy:
4141
matrix:
42-
toolchain: [stable, nightly]
42+
toolchain: [nightly, stable]
4343

4444
steps:
4545
- id: checkout
4646
name: Checkout Repository
47-
uses: actions/checkout@v3
47+
uses: actions/checkout@v4
4848

4949
- id: setup
5050
name: Setup Toolchain
5151
uses: dtolnay/rust-toolchain@stable
5252
with:
53-
toolchain: nightly
53+
toolchain: ${{ matrix.toolchain }}
5454
components: clippy
5555

5656
- id: cache
5757
name: Enable Workflow Cache
5858
uses: Swatinem/rust-cache@v2
5959

60+
- id: tools
61+
name: Install Tools
62+
uses: taiki-e/install-action@v2
63+
with:
64+
tool: cargo-machete
65+
6066
- id: check
6167
name: Run Build Checks
62-
run: cargo check --workspace --all-targets --all-features
68+
run: cargo check --tests --benches --examples --workspace --all-targets --all-features
6369

6470
- id: lint
6571
name: Run Lint Checks
66-
run: cargo clippy --workspace --all-targets --all-features -- -D clippy::correctness -D clippy::suspicious
72+
run: cargo clippy --tests --benches --examples --workspace --all-targets --all-features -- -D clippy::correctness -D clippy::suspicious -D clippy::complexity -D clippy::perf -D clippy::style -D clippy::pedantic
6773

68-
- id: doc
69-
name: Run Documentation Checks
70-
run: cargo test --doc
74+
- id: docs
75+
name: Lint Documentation
76+
env:
77+
RUSTDOCFLAGS: "-D warnings"
78+
run: cargo doc --no-deps --bins --examples --workspace --all-features
79+
80+
- id: deps
81+
name: Check Unused Dependencies
82+
run: cargo machete --with-metadata
7183

72-
internal:
73-
name: Units and Integrations
84+
unit:
85+
name: Units
7486
runs-on: ubuntu-latest
7587
needs: check
7688

7789
strategy:
7890
matrix:
79-
toolchain: [stable, nightly]
91+
toolchain: [nightly, stable]
8092

8193
steps:
8294
- id: checkout
8395
name: Checkout Repository
84-
uses: actions/checkout@v3
96+
uses: actions/checkout@v4
8597

8698
- id: setup
8799
name: Setup Toolchain
88100
uses: dtolnay/rust-toolchain@stable
89101
with:
90102
toolchain: ${{ matrix.toolchain }}
91-
components: llvm-tools-preview
92103

93104
- id: cache
94105
name: Enable Job Cache
95106
uses: Swatinem/rust-cache@v2
96107

97-
- id: test
98-
name: Run Code Tests
99-
run: cargo test --workspace --all-targets --all-features
100-
101-
- id: tools
102-
name: Install Coverage Tools
103-
uses: taiki-e/install-action@v2
104-
with:
105-
tool: cargo-llvm-cov, cargo-nextest
108+
- id: test-docs
109+
name: Run Documentation Tests
110+
run: cargo test --doc
106111

107-
- id: coverage
108-
name: Generate Coverage Report
109-
run: cargo llvm-cov nextest --workspace --all-targets --all-features
112+
- id: test
113+
name: Run Unit Tests
114+
run: cargo test --tests --benches --examples --workspace --all-targets --all-features

examples/get_metadata/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ version.workspace = true
1616
[dependencies]
1717
dht = { path = "../../packages/dht" }
1818
handshake = { path = "../../packages/handshake" }
19-
metainfo = { path = "../../packages/metainfo" }
2019
peer = { path = "../../packages/peer" }
2120
select = { path = "../../packages/select" }
2221

2322
clap = "3"
2423
futures = "0.1"
2524
tokio-core = "0.1"
26-
tokio-io = "0.1"
2725
tokio-codec = "0.1"
2826
pendulum = "0.3"
2927
hex = "0.4"

examples/get_metadata/src/main.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ use peer::protocols::{NullProtocol, PeerExtensionProtocol, PeerWireProtocol};
2121
use peer::{IPeerManagerMessage, OPeerManagerMessage, PeerInfo, PeerManagerBuilder, PeerProtocolCodec};
2222
use pendulum::future::TimerBuilder;
2323
use pendulum::HashedWheelBuilder;
24+
use select::discovery::error::DiscoveryError;
2425
use select::discovery::{IDiscoveryMessage, ODiscoveryMessage, UtMetadataModule};
25-
use select::{ControlMessage, IExtendedMessage, IUberMessage, OExtendedMessage, OUberMessage, UberModuleBuilder};
26+
use select::{ControlMessage, DiscoveryTrait, IExtendedMessage, IUberMessage, OExtendedMessage, OUberMessage, UberModuleBuilder};
2627
use tokio_core::reactor::Core;
2728

2829
// Legacy Handshaker, when bip_dht is migrated, it will accept S directly
@@ -69,6 +70,7 @@ where
6970
fn metadata(&mut self, _data: ()) {}
7071
}
7172

73+
#[allow(clippy::too_many_lines)]
7274
fn main() {
7375
let matches = clap_app!(myapp =>
7476
(version: "1.0")
@@ -100,7 +102,7 @@ fn main() {
100102
// Set a low handshake timeout so we don't wait on peers that aren't listening on tcp
101103
HandshakerConfig::default().with_connect_timeout(Duration::from_millis(500)),
102104
)
103-
.build(TcpTransport, core.handle())
105+
.build(TcpTransport, &core.handle())
104106
.unwrap()
105107
.into_parts();
106108
// Create a peer manager that will hold our peers and heartbeat/send messages to them
@@ -109,7 +111,7 @@ fn main() {
109111
// Hook up a future that feeds incoming (handshaken) peers over to the peer manager
110112
core.handle().spawn(
111113
handshaker_recv
112-
.map_err(|_| ())
114+
.map_err(|()| ())
113115
.map(|complete_msg| {
114116
// Our handshaker finished handshaking some peer, get
115117
// the peer info as well as the peer itself (socket)
@@ -141,11 +143,22 @@ fn main() {
141143
);
142144

143145
// Create our UtMetadata selection module
144-
let (uber_send, uber_recv) = UberModuleBuilder::new()
145-
.with_extended_builder(Some(ExtendedMessageBuilder::new()))
146-
.with_discovery_module(UtMetadataModule::new())
147-
.build()
148-
.split();
146+
let (uber_send, uber_recv) = {
147+
let mut this = UberModuleBuilder::new().with_extended_builder(Some(ExtendedMessageBuilder::new()));
148+
let module = UtMetadataModule::new();
149+
this.discovery.push(Box::new(module)
150+
as Box<
151+
dyn DiscoveryTrait<
152+
SinkItem = IDiscoveryMessage,
153+
SinkError = Box<DiscoveryError>,
154+
Item = ODiscoveryMessage,
155+
Error = Box<DiscoveryError>,
156+
>,
157+
>);
158+
this
159+
}
160+
.build()
161+
.split();
149162

150163
// Tell the uber module we want to download metainfo for the given hash
151164
let uber_send = core
@@ -159,7 +172,7 @@ fn main() {
159172
let timer = TimerBuilder::default().build(HashedWheelBuilder::default().build());
160173
let timer_recv = timer.sleep_stream(Duration::from_millis(100)).unwrap().map(Either::B);
161174

162-
let merged_recv = peer_manager_recv.map(Either::A).map_err(|_| ()).select(timer_recv);
175+
let merged_recv = peer_manager_recv.map(Either::A).map_err(|()| ()).select(timer_recv);
163176

164177
// Hook up a future that receives messages from the peer manager
165178
core.handle().spawn(future::loop_fn(
@@ -183,23 +196,23 @@ fn main() {
183196
info, message,
184197
))),
185198
Either::A(OPeerManagerMessage::PeerAdded(info)) => {
186-
println!("Connected To Peer: {:?}", info);
199+
println!("Connected To Peer: {info:?}");
187200
Some(IUberMessage::Control(ControlMessage::PeerConnected(info)))
188201
}
189202
Either::A(OPeerManagerMessage::PeerRemoved(info)) => {
190-
println!("We Removed Peer {:?} From The Peer Manager", info);
203+
println!("We Removed Peer {info:?} From The Peer Manager");
191204
Some(IUberMessage::Control(ControlMessage::PeerDisconnected(info)))
192205
}
193206
Either::A(OPeerManagerMessage::PeerDisconnect(info)) => {
194-
println!("Peer {:?} Disconnected From Us", info);
207+
println!("Peer {info:?} Disconnected From Us");
195208
Some(IUberMessage::Control(ControlMessage::PeerDisconnected(info)))
196209
}
197210
Either::A(OPeerManagerMessage::PeerError(info, error)) => {
198-
println!("Peer {:?} Disconnected With Error: {:?}", info, error);
211+
println!("Peer {info:?} Disconnected With Error: {error:?}");
199212
Some(IUberMessage::Control(ControlMessage::PeerDisconnected(info)))
200213
}
201-
Either::B(_) => Some(IUberMessage::Control(ControlMessage::Tick(Duration::from_millis(100)))),
202-
_ => None,
214+
Either::B(()) => Some(IUberMessage::Control(ControlMessage::Tick(Duration::from_millis(100)))),
215+
Either::A(_) => None,
203216
};
204217

205218
match opt_message {

examples/simple_torrent/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@ peer = { path = "../../packages/peer" }
2323
clap = "3"
2424
futures = "0.1"
2525
tokio-core = "0.1"
26-
tokio-io = "0.1"
27-
tokio-timer = "0.1"
2826
tokio-codec = "0.1"

examples/simple_torrent/src/main.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ impl<S> Handshaker for LegacyHandshaker<S> where S: Sink<SinkItem=InitiateMessag
6767
const MAX_PENDING_BLOCKS: usize = 50;
6868

6969
// Some enum to store our selection state updates
70+
#[allow(dead_code)]
7071
#[derive(Debug)]
7172
enum SelectState {
7273
Choke(PeerInfo),
@@ -84,9 +85,11 @@ enum SelectState {
8485
TorrentAdded,
8586
}
8687

88+
#[allow(clippy::too_many_lines)]
8789
fn main() {
8890
// Command line argument parsing
8991
let matches = clap_app!(myapp =>
92+
9093
(version: "1.0")
9194
(author: "Andrew <[email protected]>")
9295
(about: "Simple torrent downloading")
@@ -130,7 +133,7 @@ fn main() {
130133
// block when we reach our max peers). Setting these to low
131134
// values so we don't have more than 2 unused tcp connections.
132135
.with_config(HandshakerConfig::default().with_wait_buffer_size(0).with_done_buffer_size(0))
133-
.build::<TcpTransport>(TcpTransport, core.handle()) // Will handshake over TCP (could swap this for UTP in the future)
136+
.build::<TcpTransport>(TcpTransport, &core.handle()) // Will handshake over TCP (could swap this for UTP in the future)
134137
.unwrap()
135138
.into_parts();
136139
// Create a peer manager that will hold our peers and heartbeat/send messages to them
@@ -146,7 +149,7 @@ fn main() {
146149
let map_peer_manager_send = peer_manager_send.clone().sink_map_err(|_| ());
147150
core.handle().spawn(
148151
handshaker_recv
149-
.map_err(|_| ())
152+
.map_err(|()| ())
150153
.map(|complete_msg| {
151154
// Our handshaker finished handshaking some peer, get
152155
// the peer info as well as the peer itself (socket)
@@ -173,7 +176,7 @@ fn main() {
173176

174177
// Map out the errors for these sinks so they match
175178
let map_select_send = select_send.clone().sink_map_err(|_| ());
176-
let map_disk_manager_send = disk_manager_send.clone().sink_map_err(|_| ());
179+
let map_disk_manager_send = disk_manager_send.clone().sink_map_err(|()| ());
177180

178181
// Hook up a future that receives messages from the peer manager, and forwards request to the disk manager or selection manager (using loop fn
179182
// here because we need to be able to access state, like request_map and a different future combinator wouldn't let us keep it around to access)
@@ -241,15 +244,15 @@ fn main() {
241244
OPeerManagerMessage::PeerAdded(info) => Some(Either::A(SelectState::NewPeer(info))),
242245
OPeerManagerMessage::SentMessage(_, _) => None,
243246
OPeerManagerMessage::PeerRemoved(info) => {
244-
println!("We Removed Peer {:?} From The Peer Manager", info);
247+
println!("We Removed Peer {info:?} From The Peer Manager");
245248
Some(Either::A(SelectState::RemovedPeer(info)))
246249
}
247250
OPeerManagerMessage::PeerDisconnect(info) => {
248-
println!("Peer {:?} Disconnected From Us", info);
251+
println!("Peer {info:?} Disconnected From Us");
249252
Some(Either::A(SelectState::RemovedPeer(info)))
250253
}
251254
OPeerManagerMessage::PeerError(info, error) => {
252-
println!("Peer {:?} Disconnected With Error: {:?}", info, error);
255+
println!("Peer {info:?} Disconnected With Error: {error:?}");
253256
Some(Either::A(SelectState::RemovedPeer(info)))
254257
}
255258
};
@@ -305,6 +308,7 @@ fn main() {
305308
let peer_info = peer_list.remove(1);
306309

307310
// Pack up our block into a peer wire protocol message and send it off to the peer
311+
#[allow(clippy::cast_possible_truncation)]
308312
let piece =
309313
PieceMessage::new(metadata.piece_index() as u32, metadata.block_offset() as u32, block.freeze());
310314
let pwp_message = PeerWireProtocolMessage::Piece(piece);
@@ -360,13 +364,13 @@ fn main() {
360364
match opt_item.unwrap() {
361365
// Disk manager identified a good piece already downloaded
362366
SelectState::GoodPiece(index) => {
363-
piece_requests.retain(|req| req.piece_index() != index as u32);
367+
piece_requests.retain(|req| u64::from(req.piece_index()) != index);
364368
Loop::Continue((select_recv, piece_requests, cur_pieces + 1))
365369
}
366370
// Disk manager is finished identifying good pieces, torrent has been added
367371
SelectState::TorrentAdded => Loop::Break((select_recv, piece_requests, cur_pieces)),
368372
// Shouldn't be receiving any other messages...
369-
message => panic!("Unexpected Message Received In Selection Receiver: {:?}", message),
373+
message => panic!("Unexpected Message Received In Selection Receiver: {message:?}"),
370374
}
371375
})
372376
.map_err(|_| ())
@@ -467,14 +471,14 @@ fn main() {
467471
vec![IPeerManagerMessage::SendMessage(
468472
peer,
469473
0,
470-
PeerWireProtocolMessage::Have(HaveMessage::new(piece as u32)),
474+
PeerWireProtocolMessage::Have(HaveMessage::new(piece.try_into().unwrap())),
471475
)]
472476
} else {
473477
vec![]
474478
}
475479
}
476480
// Decided not to handle these two cases here
477-
SelectState::RemovedPeer(info) => panic!("Peer {:?} Got Disconnected", info),
481+
SelectState::RemovedPeer(info) => panic!("Peer {info:?} Got Disconnected"),
478482
SelectState::BadPiece(_) => panic!("Peer Gave Us Bad Piece"),
479483
_ => vec![],
480484
};
@@ -507,11 +511,11 @@ fn main() {
507511
Box::new(
508512
map_peer_manager_send
509513
.send_all(stream::iter_result(send_messages.into_iter().map(Ok::<_, ()>)))
510-
.map_err(|_| ())
514+
.map_err(|()| ())
511515
.and_then(|(map_peer_manager_send, _)| {
512516
map_peer_manager_send.send_all(stream::iter_result(next_piece_requests))
513517
})
514-
.map_err(|_| ())
518+
.map_err(|()| ())
515519
.map(move |(map_peer_manager_send, _)| {
516520
Loop::Continue((
517521
select_recv,
@@ -567,6 +571,7 @@ fn generate_requests(info: &Info, block_size: usize) -> Vec<RequestMessage> {
567571
for block_index in 0..whole_blocks {
568572
let block_offset = block_index * block_size as u64;
569573

574+
#[allow(clippy::cast_possible_truncation)]
570575
requests.push(RequestMessage::new(piece_index as u32, block_offset as u32, block_size));
571576
}
572577

@@ -576,9 +581,9 @@ fn generate_requests(info: &Info, block_size: usize) -> Vec<RequestMessage> {
576581
let block_offset = whole_blocks * block_size as u64;
577582

578583
requests.push(RequestMessage::new(
579-
piece_index as u32,
580-
block_offset as u32,
581-
partial_block_length as usize,
584+
piece_index.try_into().unwrap(),
585+
block_offset.try_into().unwrap(),
586+
partial_block_length.try_into().unwrap(),
582587
));
583588
}
584589

0 commit comments

Comments
 (0)