Skip to content

Commit 3e428a3

Browse files
committed
Auto merge of #13275 - weihanglo:iter-join, r=epage
refactor: replace `iter_join` with `itertools::join`
2 parents 87b4cb2 + cfc6e4b commit 3e428a3

File tree

5 files changed

+14
-44
lines changed

5 files changed

+14
-44
lines changed

src/cargo/core/compiler/future_incompat.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::core::{Dependency, PackageId, Workspace};
3838
use crate::sources::source::QueryKind;
3939
use crate::sources::SourceConfigMap;
4040
use crate::util::cache_lock::CacheLockMode;
41-
use crate::util::{iter_join, CargoResult};
41+
use crate::util::CargoResult;
4242
use anyhow::{bail, format_err, Context};
4343
use serde::{Deserialize, Serialize};
4444
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
@@ -228,7 +228,7 @@ impl OnDiskReports {
228228
/// Returns an ANSI-styled report
229229
pub fn get_report(&self, id: u32, package: Option<&str>) -> CargoResult<String> {
230230
let report = self.reports.iter().find(|r| r.id == id).ok_or_else(|| {
231-
let available = iter_join(self.reports.iter().map(|r| r.id.to_string()), ", ");
231+
let available = itertools::join(self.reports.iter().map(|r| r.id), ", ");
232232
format_err!(
233233
"could not find report with ID {}\n\
234234
Available IDs are: {}",
@@ -250,7 +250,7 @@ impl OnDiskReports {
250250
Available packages are: {}\n
251251
Omit the `--package` flag to display a report for all packages",
252252
package,
253-
iter_join(report.per_package.keys(), ", ")
253+
itertools::join(report.per_package.keys(), ", ")
254254
)
255255
})?
256256
.to_string()
@@ -353,14 +353,8 @@ fn get_updates(ws: &Workspace<'_>, package_ids: &BTreeSet<PackageId>) -> Option<
353353
.collect();
354354
updated_versions.sort();
355355

356-
let updated_versions = iter_join(
357-
updated_versions
358-
.into_iter()
359-
.map(|version| version.to_string()),
360-
", ",
361-
);
362-
363356
if !updated_versions.is_empty() {
357+
let updated_versions = itertools::join(updated_versions, ", ");
364358
writeln!(
365359
updates,
366360
"{} has the following newer versions available: {}",

src/cargo/core/compiler/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ use crate::core::{Feature, PackageId, Target, Verbosity};
9393
use crate::util::errors::{CargoResult, VerboseError};
9494
use crate::util::interning::InternedString;
9595
use crate::util::machine_message::{self, Message};
96-
use crate::util::{add_path_args, internal, iter_join_onto, profile};
96+
use crate::util::{add_path_args, internal, profile};
9797
use cargo_util::{paths, ProcessBuilder, ProcessError};
9898
use cargo_util_schemas::manifest::TomlDebugInfo;
9999
use cargo_util_schemas::manifest::TomlTrimPaths;
@@ -910,9 +910,12 @@ fn add_cap_lints(bcx: &BuildContext<'_, '_>, unit: &Unit, cmd: &mut ProcessBuild
910910
/// [`-Zallow-features`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#allow-features
911911
fn add_allow_features(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder) {
912912
if let Some(allow) = &cx.bcx.config.cli_unstable().allow_features {
913+
use std::fmt::Write;
913914
let mut arg = String::from("-Zallow-features=");
914-
let _ = iter_join_onto(&mut arg, allow, ",");
915-
cmd.arg(&arg);
915+
for f in allow {
916+
let _ = write!(&mut arg, "{f},");
917+
}
918+
cmd.arg(arg.trim_end_matches(','));
916919
}
917920
}
918921

src/cargo/core/features.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ use serde::{Deserialize, Serialize};
147147

148148
use crate::core::resolver::ResolveBehavior;
149149
use crate::util::errors::CargoResult;
150-
use crate::util::{indented_lines, iter_join};
150+
use crate::util::indented_lines;
151151
use crate::Config;
152152

153153
pub const SEE_CHANNELS: &str =
@@ -603,7 +603,7 @@ impl Features {
603603
bail!(
604604
"the feature `{}` is not in the list of allowed features: [{}]",
605605
feature_name,
606-
iter_join(allow, ", "),
606+
itertools::join(allow, ", "),
607607
);
608608
}
609609
}
@@ -1031,7 +1031,7 @@ impl CliUnstable {
10311031
bail!(
10321032
"the feature `{}` is not in the list of allowed features: [{}]",
10331033
k,
1034-
iter_join(allowed, ", ")
1034+
itertools::join(allowed, ", ")
10351035
);
10361036
}
10371037
}

src/cargo/util/mod.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::fmt;
21
use std::path::{Path, PathBuf};
32
use std::time::Duration;
43

@@ -94,32 +93,6 @@ pub fn human_readable_bytes(bytes: u64) -> (f32, &'static str) {
9493
(bytes / 1024_f32.powi(i as i32), UNITS[i])
9594
}
9695

97-
pub fn iter_join_onto<W, I, T>(mut w: W, iter: I, delim: &str) -> fmt::Result
98-
where
99-
W: fmt::Write,
100-
I: IntoIterator<Item = T>,
101-
T: std::fmt::Display,
102-
{
103-
let mut it = iter.into_iter().peekable();
104-
while let Some(n) = it.next() {
105-
write!(w, "{}", n)?;
106-
if it.peek().is_some() {
107-
write!(w, "{}", delim)?;
108-
}
109-
}
110-
Ok(())
111-
}
112-
113-
pub fn iter_join<I, T>(iter: I, delim: &str) -> String
114-
where
115-
I: IntoIterator<Item = T>,
116-
T: std::fmt::Display,
117-
{
118-
let mut s = String::new();
119-
let _ = iter_join_onto(&mut s, iter, delim);
120-
s
121-
}
122-
12396
pub fn indented_lines(text: &str) -> String {
12497
text.lines()
12598
.map(|line| {

tests/testsuite/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ fn clean_dry_run() {
830830
// Verify it didn't delete anything.
831831
let after = p.build_dir().ls_r();
832832
assert_eq!(before, after);
833-
let expected = cargo::util::iter_join(before.iter().map(|p| p.to_str().unwrap()), "\n");
833+
let expected = itertools::join(before.iter().map(|p| p.to_str().unwrap()), "\n");
834834
eprintln!("{expected}");
835835
// Verify the verbose output.
836836
p.cargo("clean --dry-run -v")

0 commit comments

Comments
 (0)