Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kad: Avoid from/into IteratorIndex. #1623

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions protocols/kad/src/query/peers/closest/disjoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use libp2p_core::PeerId;
use std::{
collections::HashMap,
iter::{Cycle, Map, Peekable},
ops::{Add, Index, IndexMut, Range},
ops::{Index, IndexMut, Range},
};
use wasm_timer::Instant;

Expand Down Expand Up @@ -114,7 +114,7 @@ impl ClosestDisjointPeersIter {
}

for (i, iter) in &mut self.iters.iter_mut().enumerate() {
if i != (*initiated_by).into() {
if IteratorIndex(i) != *initiated_by {
// This iterator never triggered an actual request to the
// given peer - thus ignore the returned boolean.
iter.on_failure(peer);
Expand Down Expand Up @@ -162,7 +162,7 @@ impl ClosestDisjointPeersIter {
}

for (i, iter) in &mut self.iters.iter_mut().enumerate() {
if i != (*initiated_by).into() {
if IteratorIndex(i) != *initiated_by {
// Only report the success to all remaining not-first
// iterators. Do not pass the `closer_peers` in order to
// uphold the S/Kademlia disjoint paths guarantee.
Expand Down Expand Up @@ -323,26 +323,6 @@ impl ClosestDisjointPeersIter {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct IteratorIndex(usize);

impl From<usize> for IteratorIndex {
fn from(i: usize) -> Self {
IteratorIndex(i)
}
}

impl From<IteratorIndex> for usize {
fn from(i: IteratorIndex) -> Self {
i.0
}
}

impl Add<usize> for IteratorIndex {
type Output = Self;

fn add(self, rhs: usize) -> Self::Output {
(self.0 + rhs).into()
}
}

impl Index<IteratorIndex> for Vec<ClosestPeersIter> {
type Output = ClosestPeersIter;

Expand Down