Skip to content

Commit 90131d3

Browse files
committed
Concretely type the iterable diffs
1 parent 3ad3884 commit 90131d3

File tree

2 files changed

+44
-59
lines changed

2 files changed

+44
-59
lines changed

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! [Docs.rs](https://docs.rs) (formerly cratesfyi) is an open source project to host
22
//! documentation of crates for the Rust Programming Language.
33
#![allow(clippy::cognitive_complexity)]
4-
#![feature(type_alias_impl_trait)]
54

65
pub use self::build_queue::BuildQueue;
76

src/utils/consistency/diff.rs

+44-58
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
use super::data::{Crate, CrateName, Data, Release, Version};
2-
use std::{collections::BTreeMap, fmt::Debug};
2+
use std::{
3+
cmp::Ordering,
4+
collections::{btree_map::IntoIter, BTreeMap},
5+
fmt::Debug,
6+
iter::Peekable,
7+
};
38

49
#[derive(Debug)]
510
pub(crate) struct DataDiff {
6-
pub(crate) crates: CratesDiff,
11+
pub(crate) crates: DiffMap<CrateName, Crate>,
712
}
813

9-
pub(crate) type CratesDiff = impl Iterator<Item = Diff<CrateName, Crate>> + Debug;
10-
1114
#[derive(Debug)]
1215
pub(crate) struct CrateDiff {
13-
pub(crate) releases: ReleasesDiff,
16+
pub(crate) releases: DiffMap<Version, Release>,
1417
}
1518

16-
pub(crate) type ReleasesDiff = impl Iterator<Item = Diff<Version, Release>> + Debug;
17-
1819
#[derive(Debug)]
1920
pub(crate) struct ReleaseDiff {}
2021

@@ -30,69 +31,60 @@ pub(crate) trait Diffable {
3031
fn diff(self, other: Self) -> Self::Diff;
3132
}
3233

33-
fn diff_map<Key: Ord + Debug, Value: Diffable + Debug>(
34-
left: BTreeMap<Key, Value>,
35-
right: BTreeMap<Key, Value>,
36-
) -> impl Iterator<Item = Diff<Key, Value>> + Debug {
37-
use std::{cmp::Ordering, collections::btree_map::IntoIter, iter::Peekable};
34+
#[derive(Debug)]
35+
pub(crate) struct DiffMap<Key, Value> {
36+
left: Peekable<std::collections::btree_map::IntoIter<Key, Value>>,
37+
right: Peekable<IntoIter<Key, Value>>,
38+
}
3839

39-
#[derive(Debug)]
40-
struct DiffMap<Key, Value> {
41-
left: Peekable<std::collections::btree_map::IntoIter<Key, Value>>,
42-
right: Peekable<IntoIter<Key, Value>>,
40+
impl<Key, Value> DiffMap<Key, Value> {
41+
fn new(left: BTreeMap<Key, Value>, right: BTreeMap<Key, Value>) -> Self {
42+
Self {
43+
left: left.into_iter().peekable(),
44+
right: right.into_iter().peekable(),
45+
}
4346
}
47+
}
48+
49+
impl<Key: Ord, Value: Diffable> Iterator for DiffMap<Key, Value> {
50+
type Item = Diff<Key, Value>;
4451

45-
impl<Key: Ord, Value: Diffable> Iterator for DiffMap<Key, Value> {
46-
type Item = Diff<Key, Value>;
47-
48-
fn next(&mut self) -> Option<Self::Item> {
49-
match (self.left.peek(), self.right.peek()) {
50-
(Some((left, _)), Some((right, _))) => match left.cmp(right) {
51-
Ordering::Less => {
52-
let (key, value) = self.left.next().unwrap();
53-
Some(Diff::Left(key, value))
54-
}
55-
Ordering::Equal => {
56-
let (key, left) = self.left.next().unwrap();
57-
let (_, right) = self.right.next().unwrap();
58-
Some(Diff::Both(key, left.diff(right)))
59-
}
60-
Ordering::Greater => {
61-
let (key, value) = self.right.next().unwrap();
62-
Some(Diff::Right(key, value))
63-
}
64-
},
65-
(Some((_, _)), None) => {
52+
fn next(&mut self) -> Option<Self::Item> {
53+
match (self.left.peek(), self.right.peek()) {
54+
(Some((left, _)), Some((right, _))) => match left.cmp(right) {
55+
Ordering::Less => {
6656
let (key, value) = self.left.next().unwrap();
6757
Some(Diff::Left(key, value))
6858
}
69-
(None, Some((_, _))) => {
59+
Ordering::Equal => {
60+
let (key, left) = self.left.next().unwrap();
61+
let (_, right) = self.right.next().unwrap();
62+
Some(Diff::Both(key, left.diff(right)))
63+
}
64+
Ordering::Greater => {
7065
let (key, value) = self.right.next().unwrap();
7166
Some(Diff::Right(key, value))
7267
}
73-
(None, None) => None,
68+
},
69+
(Some((_, _)), None) => {
70+
let (key, value) = self.left.next().unwrap();
71+
Some(Diff::Left(key, value))
72+
}
73+
(None, Some((_, _))) => {
74+
let (key, value) = self.right.next().unwrap();
75+
Some(Diff::Right(key, value))
7476
}
77+
(None, None) => None,
7578
}
7679
}
77-
78-
DiffMap {
79-
left: left.into_iter().peekable(),
80-
right: right.into_iter().peekable(),
81-
}
8280
}
8381

8482
impl Diffable for Data {
8583
type Diff = DataDiff;
8684

8785
fn diff(self, other: Self) -> Self::Diff {
88-
fn diff_crates(
89-
left: BTreeMap<CrateName, Crate>,
90-
right: BTreeMap<CrateName, Crate>,
91-
) -> CratesDiff {
92-
diff_map(left, right)
93-
}
9486
DataDiff {
95-
crates: diff_crates(self.crates, other.crates),
87+
crates: DiffMap::new(self.crates, other.crates),
9688
}
9789
}
9890
}
@@ -101,14 +93,8 @@ impl Diffable for Crate {
10193
type Diff = CrateDiff;
10294

10395
fn diff(self, other: Self) -> Self::Diff {
104-
fn diff_releases(
105-
left: BTreeMap<Version, Release>,
106-
right: BTreeMap<Version, Release>,
107-
) -> ReleasesDiff {
108-
diff_map(left, right)
109-
}
11096
CrateDiff {
111-
releases: diff_releases(self.releases, other.releases),
97+
releases: DiffMap::new(self.releases, other.releases),
11298
}
11399
}
114100
}

0 commit comments

Comments
 (0)