Skip to content

Commit

Permalink
refactor: use Iterator::is_sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Oct 18, 2024
1 parent 6f92aaa commit 7c79d81
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 25 deletions.
13 changes: 8 additions & 5 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use crate::util::toml_mut::dependency::MaybeWorkspace;
use crate::util::toml_mut::dependency::PathSource;
use crate::util::toml_mut::dependency::Source;
use crate::util::toml_mut::dependency::WorkspaceSource;
use crate::util::toml_mut::is_sorted;
use crate::util::toml_mut::manifest::DepTable;
use crate::util::toml_mut::manifest::LocalManifest;
use crate::CargoResult;
Expand Down Expand Up @@ -111,10 +110,14 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
.map(TomlItem::as_table)
.map_or(true, |table_option| {
table_option.map_or(true, |table| {
is_sorted(table.get_values().iter_mut().map(|(key, _)| {
// get_values key paths always have at least one key.
key.remove(0)
}))
table
.get_values()
.iter_mut()
.map(|(key, _)| {
// get_values key paths always have at least one key.
key.remove(0)
})
.is_sorted()
})
});
for dep in deps {
Expand Down
3 changes: 1 addition & 2 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::core::{Edition, Shell, Workspace};
use crate::util::errors::CargoResult;
use crate::util::important_paths::find_root_manifest_for_wd;
use crate::util::toml_mut::is_sorted;
use crate::util::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
use crate::util::{restricted_names, GlobalContext};
use anyhow::{anyhow, Context as _};
Expand Down Expand Up @@ -995,7 +994,7 @@ fn update_manifest_with_new_member(
}
}

let was_sorted = is_sorted(members.iter().map(Value::as_str));
let was_sorted = members.iter().map(Value::as_str).is_sorted();
members.push(display_path);
if was_sorted {
members.sort_by(|lhs, rhs| lhs.as_str().cmp(&rhs.as_str()));
Expand Down
3 changes: 1 addition & 2 deletions src/cargo/util/toml_mut/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::core::SourceId;
use crate::core::Summary;
use crate::core::{Features, GitReference};
use crate::util::toml::lookup_path_base;
use crate::util::toml_mut::is_sorted;
use crate::CargoResult;
use crate::GlobalContext;

Expand Down Expand Up @@ -639,7 +638,7 @@ impl Dependency {
.collect::<Option<IndexSet<_>>>()
})
.unwrap_or_default();
let is_already_sorted = is_sorted(features.iter());
let is_already_sorted = features.iter().is_sorted();
features.extend(new_features.iter().map(|s| s.as_str()));
let features = if is_already_sorted {
features.into_iter().sorted().collect::<toml_edit::Value>()
Expand Down
16 changes: 0 additions & 16 deletions src/cargo/util/toml_mut/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,3 @@
pub mod dependency;
pub mod manifest;
pub mod upgrade;

// Based on Iterator::is_sorted from nightly std; remove in favor of that when stabilized.
pub fn is_sorted(mut it: impl Iterator<Item = impl PartialOrd>) -> bool {
let Some(mut last) = it.next() else {
return true;
};

for curr in it {
if curr < last {
return false;
}
last = curr;
}

true
}

0 comments on commit 7c79d81

Please sign in to comment.