Skip to content

Commit 4543f17

Browse files
committed
Auto merge of #6134 - Eh2406:dead-code, r=alexcrichton
remove dead code, use impl Iterator instead of custom types, cargo fmt This is a mishmash of things. `Graph::sort` was unused so I removed it. There are a bunch of custom types that allow us to return iterators that can now be replaced with `impl Iterator`. jetbrains wanted to reorder some impls to match the definition. fmt also made some changes to the files I opened.
2 parents d0b5a17 + 5c53892 commit 4543f17

File tree

5 files changed

+60
-159
lines changed

5 files changed

+60
-159
lines changed

src/cargo/core/resolver/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use self::types::{RcVecIter, RegistryQueryer, RemainingDeps, ResolverProgress};
6868

6969
pub use self::encode::{EncodableDependency, EncodablePackageId, EncodableResolve};
7070
pub use self::encode::{Metadata, WorkspaceResolve};
71-
pub use self::resolve::{Deps, DepsNotReplaced, Resolve};
71+
pub use self::resolve::Resolve;
7272
pub use self::types::Method;
7373

7474
mod conflict_cache;

src/cargo/core/resolver/resolve.rs

Lines changed: 20 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ use std::iter::FromIterator;
55
use url::Url;
66

77
use core::{Dependency, PackageId, PackageIdSpec, Summary, Target};
8-
use util::Graph;
98
use util::errors::CargoResult;
10-
use util::graph::{Edges, Nodes};
9+
use util::Graph;
1110

1211
use super::encode::Metadata;
1312

@@ -162,21 +161,18 @@ unable to verify that `{0}` is the same as when the lockfile was generated
162161
Ok(())
163162
}
164163

165-
pub fn iter(&self) -> Nodes<PackageId, Vec<Dependency>> {
164+
pub fn iter(&self) -> impl Iterator<Item = &PackageId> {
166165
self.graph.iter()
167166
}
168167

169-
pub fn deps(&self, pkg: &PackageId) -> Deps {
170-
Deps {
171-
edges: self.graph.edges(pkg),
172-
resolve: self,
173-
}
168+
pub fn deps(&self, pkg: &PackageId) -> impl Iterator<Item = (&PackageId, &[Dependency])> {
169+
self.graph
170+
.edges(pkg)
171+
.map(move |(id, deps)| (self.replacement(id).unwrap_or(id), deps.as_slice()))
174172
}
175173

176-
pub fn deps_not_replaced(&self, pkg: &PackageId) -> DepsNotReplaced {
177-
DepsNotReplaced {
178-
edges: self.graph.edges(pkg),
179-
}
174+
pub fn deps_not_replaced(&self, pkg: &PackageId) -> impl Iterator<Item = &PackageId> {
175+
self.graph.edges(pkg).map(|(id, _)| id)
180176
}
181177

182178
pub fn replacement(&self, pkg: &PackageId) -> Option<&PackageId> {
@@ -226,16 +222,22 @@ unable to verify that `{0}` is the same as when the lockfile was generated
226222
};
227223

228224
let crate_name = to_target.crate_name();
229-
let mut names = deps.iter()
230-
.map(|d| d.explicit_name_in_toml().map(|s| s.as_str()).unwrap_or(&crate_name));
225+
let mut names = deps.iter().map(|d| {
226+
d.explicit_name_in_toml()
227+
.map(|s| s.as_str())
228+
.unwrap_or(&crate_name)
229+
});
231230
let name = names.next().unwrap_or(&crate_name);
232231
for n in names {
233232
if n == name {
234-
continue
233+
continue;
235234
}
236-
bail!("multiple dependencies listed for the same crate must \
237-
all have the same name, but the dependency on `{}` \
238-
is listed as having different names", to);
235+
bail!(
236+
"multiple dependencies listed for the same crate must \
237+
all have the same name, but the dependency on `{}` \
238+
is listed as having different names",
239+
to
240+
);
239241
}
240242
Ok(name.to_string())
241243
}
@@ -272,48 +274,3 @@ impl fmt::Debug for Resolve {
272274
write!(fmt, "}}")
273275
}
274276
}
275-
276-
pub struct Deps<'a> {
277-
edges: Option<Edges<'a, PackageId, Vec<Dependency>>>,
278-
resolve: &'a Resolve,
279-
}
280-
281-
impl<'a> Iterator for Deps<'a> {
282-
type Item = (&'a PackageId, &'a [Dependency]);
283-
284-
fn next(&mut self) -> Option<(&'a PackageId, &'a [Dependency])> {
285-
let (id, deps) = self.edges.as_mut()?.next()?;
286-
let id_ret = self.resolve.replacement(id).unwrap_or(id);
287-
Some((id_ret, deps))
288-
}
289-
290-
fn size_hint(&self) -> (usize, Option<usize>) {
291-
// Note: Edges is actually a std::collections::hash_set::Iter, which
292-
// is an ExactSizeIterator.
293-
let len = self.edges.as_ref().map(ExactSizeIterator::len).unwrap_or(0);
294-
(len, Some(len))
295-
}
296-
}
297-
298-
impl<'a> ExactSizeIterator for Deps<'a> {}
299-
300-
pub struct DepsNotReplaced<'a> {
301-
edges: Option<Edges<'a, PackageId, Vec<Dependency>>>,
302-
}
303-
304-
impl<'a> Iterator for DepsNotReplaced<'a> {
305-
type Item = &'a PackageId;
306-
307-
fn next(&mut self) -> Option<&'a PackageId> {
308-
Some(self.edges.as_mut()?.next()?.0)
309-
}
310-
311-
fn size_hint(&self) -> (usize, Option<usize>) {
312-
// Note: Edges is actually a std::collections::hash_set::Iter, which
313-
// is an ExactSizeIterator.
314-
let len = self.edges.as_ref().map(ExactSizeIterator::len).unwrap_or(0);
315-
(len, Some(len))
316-
}
317-
}
318-
319-
impl<'a> ExactSizeIterator for DepsNotReplaced<'a> {}

src/cargo/core/source/mod.rs

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::hash_map::{HashMap, IterMut, Values};
1+
use std::collections::hash_map::HashMap;
22
use std::fmt;
33

44
use core::{Dependency, Package, PackageId, Summary};
@@ -51,8 +51,7 @@ pub trait Source {
5151
/// version specified.
5252
fn download(&mut self, package: &PackageId) -> CargoResult<MaybePackage>;
5353

54-
fn finish_download(&mut self, package: &PackageId, contents: Vec<u8>)
55-
-> CargoResult<Package>;
54+
fn finish_download(&mut self, package: &PackageId, contents: Vec<u8>) -> CargoResult<Package>;
5655

5756
/// Generates a unique string which represents the fingerprint of the
5857
/// current state of the source.
@@ -88,13 +87,20 @@ pub trait Source {
8887

8988
pub enum MaybePackage {
9089
Ready(Package),
91-
Download {
92-
url: String,
93-
descriptor: String,
94-
}
90+
Download { url: String, descriptor: String },
9591
}
9692

9793
impl<'a, T: Source + ?Sized + 'a> Source for Box<T> {
94+
/// Forwards to `Source::source_id`
95+
fn source_id(&self) -> &SourceId {
96+
(**self).source_id()
97+
}
98+
99+
/// Forwards to `Source::replaced_source_id`
100+
fn replaced_source_id(&self) -> &SourceId {
101+
(**self).replaced_source_id()
102+
}
103+
98104
/// Forwards to `Source::supports_checksums`
99105
fn supports_checksums(&self) -> bool {
100106
(**self).supports_checksums()
@@ -115,16 +121,6 @@ impl<'a, T: Source + ?Sized + 'a> Source for Box<T> {
115121
(**self).fuzzy_query(dep, f)
116122
}
117123

118-
/// Forwards to `Source::source_id`
119-
fn source_id(&self) -> &SourceId {
120-
(**self).source_id()
121-
}
122-
123-
/// Forwards to `Source::replaced_source_id`
124-
fn replaced_source_id(&self) -> &SourceId {
125-
(**self).replaced_source_id()
126-
}
127-
128124
/// Forwards to `Source::update`
129125
fn update(&mut self) -> CargoResult<()> {
130126
(**self).update()
@@ -159,6 +155,14 @@ impl<'a, T: Source + ?Sized + 'a> Source for Box<T> {
159155
}
160156

161157
impl<'a, T: Source + ?Sized + 'a> Source for &'a mut T {
158+
fn source_id(&self) -> &SourceId {
159+
(**self).source_id()
160+
}
161+
162+
fn replaced_source_id(&self) -> &SourceId {
163+
(**self).replaced_source_id()
164+
}
165+
162166
fn supports_checksums(&self) -> bool {
163167
(**self).supports_checksums()
164168
}
@@ -175,14 +179,6 @@ impl<'a, T: Source + ?Sized + 'a> Source for &'a mut T {
175179
(**self).fuzzy_query(dep, f)
176180
}
177181

178-
fn source_id(&self) -> &SourceId {
179-
(**self).source_id()
180-
}
181-
182-
fn replaced_source_id(&self) -> &SourceId {
183-
(**self).replaced_source_id()
184-
}
185-
186182
fn update(&mut self) -> CargoResult<()> {
187183
(**self).update()
188184
}
@@ -226,14 +222,6 @@ impl<'src> fmt::Debug for SourceMap<'src> {
226222
}
227223
}
228224

229-
/// A `std::collection::hash_map::Values` for `SourceMap`
230-
pub type Sources<'a, 'src> = Values<'a, SourceId, Box<Source + 'src>>;
231-
232-
/// A `std::collection::hash_map::IterMut` for `SourceMap`
233-
pub struct SourcesMut<'a, 'src: 'a> {
234-
inner: IterMut<'a, SourceId, Box<Source + 'src>>,
235-
}
236-
237225
impl<'src> SourceMap<'src> {
238226
/// Create an empty map
239227
pub fn new() -> SourceMap<'src> {
@@ -288,21 +276,14 @@ impl<'src> SourceMap<'src> {
288276
}
289277

290278
/// Like `HashMap::values`
291-
pub fn sources<'a>(&'a self) -> Sources<'a, 'src> {
279+
pub fn sources<'a>(&'a self) -> impl Iterator<Item = &'a Box<Source + 'src>> {
292280
self.map.values()
293281
}
294282

295283
/// Like `HashMap::iter_mut`
296-
pub fn sources_mut<'a>(&'a mut self) -> SourcesMut<'a, 'src> {
297-
SourcesMut {
298-
inner: self.map.iter_mut(),
299-
}
300-
}
301-
}
302-
303-
impl<'a, 'src> Iterator for SourcesMut<'a, 'src> {
304-
type Item = (&'a SourceId, &'a mut (Source + 'src));
305-
fn next(&mut self) -> Option<(&'a SourceId, &'a mut (Source + 'src))> {
306-
self.inner.next().map(|(a, b)| (a, &mut **b))
284+
pub fn sources_mut<'a>(
285+
&'a mut self,
286+
) -> impl Iterator<Item = (&'a SourceId, &'a mut (Source + 'src))> {
287+
self.map.iter_mut().map(|(a, b)| (a, &mut **b))
307288
}
308289
}

src/cargo/util/graph.rs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1+
use std::collections::hash_map::HashMap;
12
use std::fmt;
23
use std::hash::Hash;
3-
use std::collections::hash_map::{HashMap, Iter, Keys};
44

55
pub struct Graph<N, E> {
66
nodes: HashMap<N, HashMap<N, E>>,
77
}
88

9-
enum Mark {
10-
InProgress,
11-
Done,
12-
}
13-
14-
pub type Nodes<'a, N, E> = Keys<'a, N, HashMap<N, E>>;
15-
pub type Edges<'a, N, E> = Iter<'a, N, E>;
16-
179
impl<N: Eq + Hash + Clone, E: Default> Graph<N, E> {
1810
pub fn new() -> Graph<N, E> {
1911
Graph {
@@ -37,37 +29,11 @@ impl<N: Eq + Hash + Clone, E: Default> Graph<N, E> {
3729
self.nodes.get(from)?.get(to)
3830
}
3931

40-
pub fn edges(&self, from: &N) -> Option<Edges<N, E>> {
41-
self.nodes.get(from).map(|set| set.iter())
42-
}
43-
44-
pub fn sort(&self) -> Option<Vec<N>> {
45-
let mut ret = Vec::new();
46-
let mut marks = HashMap::new();
47-
48-
for node in self.nodes.keys() {
49-
self.visit(node, &mut ret, &mut marks);
50-
}
51-
52-
Some(ret)
53-
}
54-
55-
fn visit(&self, node: &N, dst: &mut Vec<N>, marks: &mut HashMap<N, Mark>) {
56-
if marks.contains_key(node) {
57-
return;
58-
}
59-
60-
marks.insert(node.clone(), Mark::InProgress);
61-
62-
for child in self.nodes[node].keys() {
63-
self.visit(child, dst, marks);
64-
}
65-
66-
dst.push(node.clone());
67-
marks.insert(node.clone(), Mark::Done);
32+
pub fn edges(&self, from: &N) -> impl Iterator<Item = (&N, &E)> {
33+
self.nodes.get(from).into_iter().flat_map(|x| x.iter())
6834
}
6935

70-
pub fn iter(&self) -> Nodes<N, E> {
36+
pub fn iter(&self) -> impl Iterator<Item = &N> {
7137
self.nodes.keys()
7238
}
7339

@@ -81,7 +47,7 @@ impl<N: Eq + Hash + Clone, E: Default> Graph<N, E> {
8147
let first_pkg_depending_on = |pkg: &N, res: &[&N]| {
8248
self.nodes
8349
.iter()
84-
.filter(|&(_node, adjacent)| adjacent.contains_key(pkg))
50+
.filter(|&(_, adjacent)| adjacent.contains_key(pkg))
8551
// Note that we can have "cycles" introduced through dev-dependency
8652
// edges, so make sure we don't loop infinitely.
8753
.find(|&(node, _)| !res.contains(&node))

src/cargo/util/paths.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::ffi::{OsStr, OsString};
33
use std::fs::{self, File, OpenOptions};
44
use std::io;
55
use std::io::prelude::*;
6-
use std::path::{Component, Path, PathBuf};
76
use std::iter;
7+
use std::path::{Component, Path, PathBuf};
88

99
use filetime::FileTime;
1010

@@ -127,8 +127,7 @@ pub fn read_bytes(path: &Path) -> CargoResult<Vec<u8>> {
127127
}
128128
f.read_to_end(&mut ret)?;
129129
Ok(ret)
130-
})()
131-
.chain_err(|| format!("failed to read `{}`", path.display()))?;
130+
})().chain_err(|| format!("failed to read `{}`", path.display()))?;
132131
Ok(res)
133132
}
134133

@@ -137,8 +136,7 @@ pub fn write(path: &Path, contents: &[u8]) -> CargoResult<()> {
137136
let mut f = File::create(path)?;
138137
f.write_all(contents)?;
139138
Ok(())
140-
})()
141-
.chain_err(|| format!("failed to write `{}`", path.display()))?;
139+
})().chain_err(|| format!("failed to write `{}`", path.display()))?;
142140
Ok(())
143141
}
144142

@@ -158,8 +156,7 @@ pub fn write_if_changed<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) ->
158156
f.write_all(contents)?;
159157
}
160158
Ok(())
161-
})()
162-
.chain_err(|| format!("failed to write `{}`", path.as_ref().display()))?;
159+
})().chain_err(|| format!("failed to write `{}`", path.as_ref().display()))?;
163160
Ok(())
164161
}
165162

@@ -173,8 +170,7 @@ pub fn append(path: &Path, contents: &[u8]) -> CargoResult<()> {
173170

174171
f.write_all(contents)?;
175172
Ok(())
176-
})()
177-
.chain_err(|| format!("failed to write `{}`", path.display()))?;
173+
})().chain_err(|| format!("failed to write `{}`", path.display()))?;
178174
Ok(())
179175
}
180176

@@ -198,8 +194,8 @@ pub fn path2bytes(path: &Path) -> CargoResult<&[u8]> {
198194

199195
#[cfg(unix)]
200196
pub fn bytes2path(bytes: &[u8]) -> CargoResult<PathBuf> {
201-
use std::os::unix::prelude::*;
202197
use std::ffi::OsStr;
198+
use std::os::unix::prelude::*;
203199
Ok(PathBuf::from(OsStr::from_bytes(bytes)))
204200
}
205201
#[cfg(windows)]
@@ -258,7 +254,8 @@ fn _remove_dir_all(p: &Path) -> CargoResult<()> {
258254
if p.symlink_metadata()?.file_type().is_symlink() {
259255
return remove_file(p);
260256
}
261-
let entries = p.read_dir()
257+
let entries = p
258+
.read_dir()
262259
.chain_err(|| format!("failed to read directory `{}`", p.display()))?;
263260
for entry in entries {
264261
let entry = entry?;

0 commit comments

Comments
 (0)