Skip to content

Commit b44c54d

Browse files
committed
Auto merge of #14702 - weihanglo:1.82, r=epage
refactor: use `Iterator::is_sorted`
2 parents 90aee40 + a8153f2 commit b44c54d

File tree

4 files changed

+10
-25
lines changed

4 files changed

+10
-25
lines changed

src/cargo/ops/cargo_add/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use crate::util::toml_mut::dependency::MaybeWorkspace;
3737
use crate::util::toml_mut::dependency::PathSource;
3838
use crate::util::toml_mut::dependency::Source;
3939
use crate::util::toml_mut::dependency::WorkspaceSource;
40-
use crate::util::toml_mut::is_sorted;
4140
use crate::util::toml_mut::manifest::DepTable;
4241
use crate::util::toml_mut::manifest::LocalManifest;
4342
use crate::CargoResult;
@@ -111,10 +110,14 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
111110
.map(TomlItem::as_table)
112111
.map_or(true, |table_option| {
113112
table_option.map_or(true, |table| {
114-
is_sorted(table.get_values().iter_mut().map(|(key, _)| {
115-
// get_values key paths always have at least one key.
116-
key.remove(0)
117-
}))
113+
table
114+
.get_values()
115+
.iter_mut()
116+
.map(|(key, _)| {
117+
// get_values key paths always have at least one key.
118+
key.remove(0)
119+
})
120+
.is_sorted()
118121
})
119122
});
120123
for dep in deps {

src/cargo/ops/cargo_new.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::core::{Edition, Shell, Workspace};
22
use crate::util::errors::CargoResult;
33
use crate::util::important_paths::find_root_manifest_for_wd;
4-
use crate::util::toml_mut::is_sorted;
54
use crate::util::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
65
use crate::util::{restricted_names, GlobalContext};
76
use anyhow::{anyhow, Context as _};
@@ -995,7 +994,7 @@ fn update_manifest_with_new_member(
995994
}
996995
}
997996

998-
let was_sorted = is_sorted(members.iter().map(Value::as_str));
997+
let was_sorted = members.iter().map(Value::as_str).is_sorted();
999998
members.push(display_path);
1000999
if was_sorted {
10011000
members.sort_by(|lhs, rhs| lhs.as_str().cmp(&rhs.as_str()));

src/cargo/util/toml_mut/dependency.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::core::SourceId;
1414
use crate::core::Summary;
1515
use crate::core::{Features, GitReference};
1616
use crate::util::toml::lookup_path_base;
17-
use crate::util::toml_mut::is_sorted;
1817
use crate::CargoResult;
1918
use crate::GlobalContext;
2019

@@ -639,7 +638,7 @@ impl Dependency {
639638
.collect::<Option<IndexSet<_>>>()
640639
})
641640
.unwrap_or_default();
642-
let is_already_sorted = is_sorted(features.iter());
641+
let is_already_sorted = features.iter().is_sorted();
643642
features.extend(new_features.iter().map(|s| s.as_str()));
644643
let features = if is_already_sorted {
645644
features.into_iter().sorted().collect::<toml_edit::Value>()

src/cargo/util/toml_mut/mod.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,3 @@
1212
pub mod dependency;
1313
pub mod manifest;
1414
pub mod upgrade;
15-
16-
// Based on Iterator::is_sorted from nightly std; remove in favor of that when stabilized.
17-
pub fn is_sorted(mut it: impl Iterator<Item = impl PartialOrd>) -> bool {
18-
let Some(mut last) = it.next() else {
19-
return true;
20-
};
21-
22-
for curr in it {
23-
if curr < last {
24-
return false;
25-
}
26-
last = curr;
27-
}
28-
29-
true
30-
}

0 commit comments

Comments
 (0)