Improve readability in a few sorts

This commit is contained in:
ljedrz 2018-07-25 10:36:57 +02:00
parent 1398572403
commit f653bf4fba
6 changed files with 9 additions and 17 deletions

View File

@ -1229,10 +1229,7 @@ Available lint options:
fn sort_lint_groups(lints: Vec<(&'static str, Vec<lint::LintId>, bool)>) fn sort_lint_groups(lints: Vec<(&'static str, Vec<lint::LintId>, bool)>)
-> Vec<(&'static str, Vec<lint::LintId>)> { -> Vec<(&'static str, Vec<lint::LintId>)> {
let mut lints: Vec<_> = lints.into_iter().map(|(x, y, _)| (x, y)).collect(); let mut lints: Vec<_> = lints.into_iter().map(|(x, y, _)| (x, y)).collect();
lints.sort_by(|&(x, _): &(&'static str, Vec<lint::LintId>), lints.sort_by_key(|ref l| l.0);
&(y, _): &(&'static str, Vec<lint::LintId>)| {
x.cmp(y)
});
lints lints
} }

View File

@ -202,14 +202,13 @@ fn compute_counts_rec(counts: &mut HashMap<String,QueryMetric>, traces: &Vec<Rec
pub fn write_counts(count_file: &mut File, counts: &mut HashMap<String,QueryMetric>) { pub fn write_counts(count_file: &mut File, counts: &mut HashMap<String,QueryMetric>) {
use rustc::util::common::duration_to_secs_str; use rustc::util::common::duration_to_secs_str;
use std::cmp::Ordering; use std::cmp::Reverse;
let mut data = vec![]; let mut data = vec![];
for (ref cons, ref qm) in counts.iter() { for (ref cons, ref qm) in counts.iter() {
data.push((cons.clone(), qm.count.clone(), qm.dur_total.clone(), qm.dur_self.clone())); data.push((cons.clone(), qm.count.clone(), qm.dur_total.clone(), qm.dur_self.clone()));
}; };
data.sort_by(|&(_,_,_,self1),&(_,_,_,self2)| data.sort_by_key(|&k| Reverse(k.3));
if self1 > self2 { Ordering::Less } else { Ordering::Greater } );
for (cons, count, dur_total, dur_self) in data { for (cons, count, dur_total, dur_self) in data {
write!(count_file, "{}, {}, {}, {}\n", write!(count_file, "{}, {}, {}, {}\n",
cons, count, cons, count,

View File

@ -22,7 +22,7 @@ use std::borrow::Cow;
use std::io::prelude::*; use std::io::prelude::*;
use std::io; use std::io;
use std::collections::HashMap; use std::collections::HashMap;
use std::cmp::min; use std::cmp::{min, Reverse};
use termcolor::{StandardStream, ColorChoice, ColorSpec, BufferWriter}; use termcolor::{StandardStream, ColorChoice, ColorSpec, BufferWriter};
use termcolor::{WriteColor, Color, Buffer}; use termcolor::{WriteColor, Color, Buffer};
use unicode_width; use unicode_width;
@ -265,9 +265,7 @@ impl EmitterWriter {
} }
// Find overlapping multiline annotations, put them at different depths // Find overlapping multiline annotations, put them at different depths
multiline_annotations.sort_by(|a, b| { multiline_annotations.sort_by_key(|&(_, ref ml)| (ml.line_start, ml.line_end));
(a.1.line_start, a.1.line_end).cmp(&(b.1.line_start, b.1.line_end))
});
for item in multiline_annotations.clone() { for item in multiline_annotations.clone() {
let ann = item.1; let ann = item.1;
for item in multiline_annotations.iter_mut() { for item in multiline_annotations.iter_mut() {
@ -403,7 +401,7 @@ impl EmitterWriter {
// otherwise the lines would end up needing to go over a message. // otherwise the lines would end up needing to go over a message.
let mut annotations = line.annotations.clone(); let mut annotations = line.annotations.clone();
annotations.sort_by(|a,b| b.start_col.cmp(&a.start_col)); annotations.sort_by_key(|a| Reverse(a.start_col));
// First, figure out where each label will be positioned. // First, figure out where each label will be positioned.
// //

View File

@ -29,9 +29,7 @@ pub fn assert_symbols_are_distinct<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>, mon
(mono_item, mono_item.symbol_name(tcx)) (mono_item, mono_item.symbol_name(tcx))
}).collect(); }).collect();
(&mut symbols[..]).sort_by(|&(_, ref sym1), &(_, ref sym2)|{ (&mut symbols[..]).sort_by_key(|&sym| sym.1);
sym1.cmp(sym2)
});
for pair in (&symbols[..]).windows(2) { for pair in (&symbols[..]).windows(2) {
let sym1 = &pair[0].1; let sym1 = &pair[0].1;

View File

@ -156,7 +156,7 @@ impl<'tcx> MirPatch<'tcx> {
} }
let mut new_statements = self.new_statements; let mut new_statements = self.new_statements;
new_statements.sort_by(|u,v| u.0.cmp(&v.0)); new_statements.sort_by_key(|s| s.0);
let mut delta = 0; let mut delta = 0;
let mut last_bb = START_BLOCK; let mut last_bb = START_BLOCK;

View File

@ -1746,7 +1746,7 @@ pub fn compute_bounds<'gcx: 'tcx, 'tcx>(astconv: &dyn AstConv<'gcx, 'tcx>,
astconv.ast_region_to_region(r, None) astconv.ast_region_to_region(r, None)
}).collect(); }).collect();
trait_bounds.sort_by(|a,b| a.def_id().cmp(&b.def_id())); trait_bounds.sort_by_key(|t| t.def_id());
let implicitly_sized = if let SizedByDefault::Yes = sized_by_default { let implicitly_sized = if let SizedByDefault::Yes = sized_by_default {
!is_unsized(astconv, ast_bounds, span) !is_unsized(astconv, ast_bounds, span)