Remove rewrite_call_with_binary_search()
This commit is contained in:
parent
d906ea23c7
commit
b02e813db7
55
src/expr.rs
55
src/expr.rs
@ -32,9 +32,9 @@ use patterns::{can_be_overflowed_pat, TuplePatField};
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use string::{rewrite_string, StringFormat};
|
||||
use types::{can_be_overflowed_type, rewrite_path, PathContext};
|
||||
use utils::{binary_search, colon_spaces, contains_skip, extra_offset, first_line_width,
|
||||
inner_attributes, last_line_extendable, last_line_width, left_most_sub_expr, mk_sp,
|
||||
outer_attributes, paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, stmt_expr,
|
||||
use utils::{colon_spaces, contains_skip, extra_offset, first_line_width, inner_attributes,
|
||||
last_line_extendable, last_line_width, left_most_sub_expr, mk_sp, outer_attributes,
|
||||
paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, stmt_expr,
|
||||
trimmed_last_line_width, wrap_str};
|
||||
use vertical::rewrite_with_alignment;
|
||||
use visitor::FmtVisitor;
|
||||
@ -83,13 +83,8 @@ pub fn format_expr(
|
||||
},
|
||||
ast::ExprKind::Call(ref callee, ref args) => {
|
||||
let inner_span = mk_sp(callee.span.hi(), expr.span.hi());
|
||||
rewrite_call_with_binary_search(
|
||||
context,
|
||||
&**callee,
|
||||
&ptr_vec_to_ref_vec(&args),
|
||||
inner_span,
|
||||
shape,
|
||||
)
|
||||
let callee_str = try_opt!(callee.rewrite(context, shape));
|
||||
rewrite_call(context, &callee_str, &args, inner_span, shape)
|
||||
}
|
||||
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape),
|
||||
ast::ExprKind::Binary(ref op, ref lhs, ref rhs) => {
|
||||
@ -2036,46 +2031,6 @@ fn string_requires_rewrite(
|
||||
false
|
||||
}
|
||||
|
||||
pub fn rewrite_call_with_binary_search<R>(
|
||||
context: &RewriteContext,
|
||||
callee: &R,
|
||||
args: &[&ast::Expr],
|
||||
span: Span,
|
||||
shape: Shape,
|
||||
) -> Option<String>
|
||||
where
|
||||
R: Rewrite,
|
||||
{
|
||||
let force_trailing_comma = if context.inside_macro {
|
||||
span_ends_with_comma(context, span)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let closure = |callee_max_width| {
|
||||
// FIXME using byte lens instead of char lens (and probably all over the
|
||||
// place too)
|
||||
let callee_shape = Shape {
|
||||
width: callee_max_width,
|
||||
..shape
|
||||
};
|
||||
let callee_str = callee
|
||||
.rewrite(context, callee_shape)
|
||||
.ok_or(Ordering::Greater)?;
|
||||
|
||||
rewrite_call_inner(
|
||||
context,
|
||||
&callee_str,
|
||||
args,
|
||||
span,
|
||||
shape,
|
||||
context.config.fn_call_width(),
|
||||
force_trailing_comma,
|
||||
)
|
||||
};
|
||||
|
||||
binary_search(1, shape.width, closure)
|
||||
}
|
||||
|
||||
pub fn rewrite_call(
|
||||
context: &RewriteContext,
|
||||
callee: &str,
|
||||
|
44
src/utils.rs
44
src/utils.rs
@ -9,7 +9,6 @@
|
||||
// except according to those terms.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use syntax::{abi, ptr};
|
||||
use syntax::ast::{self, Attribute, MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind,
|
||||
@ -439,33 +438,6 @@ impl Rewrite for String {
|
||||
}
|
||||
}
|
||||
|
||||
// Binary search in integer range. Returns the first Ok value returned by the
|
||||
// callback.
|
||||
// The callback takes an integer and returns either an Ok, or an Err indicating
|
||||
// whether the `guess' was too high (Ordering::Less), or too low.
|
||||
// This function is guaranteed to try to the hi value first.
|
||||
pub fn binary_search<C, T>(mut lo: usize, mut hi: usize, callback: C) -> Option<T>
|
||||
where
|
||||
C: Fn(usize) -> Result<T, Ordering>,
|
||||
{
|
||||
let mut middle = hi;
|
||||
|
||||
while lo <= hi {
|
||||
match callback(middle) {
|
||||
Ok(val) => return Some(val),
|
||||
Err(Ordering::Less) => {
|
||||
hi = middle - 1;
|
||||
}
|
||||
Err(..) => {
|
||||
lo = middle + 1;
|
||||
}
|
||||
}
|
||||
middle = (hi + lo) / 2;
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn colon_spaces(before: bool, after: bool) -> &'static str {
|
||||
match (before, after) {
|
||||
@ -485,22 +457,6 @@ pub fn paren_overhead(context: &RewriteContext) -> usize {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bin_search_test() {
|
||||
let closure = |i| match i {
|
||||
4 => Ok(()),
|
||||
j if j > 4 => Err(Ordering::Less),
|
||||
j if j < 4 => Err(Ordering::Greater),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
assert_eq!(Some(()), binary_search(1, 10, &closure));
|
||||
assert_eq!(None, binary_search(1, 3, &closure));
|
||||
assert_eq!(Some(()), binary_search(0, 44, &closure));
|
||||
assert_eq!(Some(()), binary_search(4, 125, &closure));
|
||||
assert_eq!(None, binary_search(6, 100, &closure));
|
||||
}
|
||||
|
||||
pub fn left_most_sub_expr(e: &ast::Expr) -> &ast::Expr {
|
||||
match e.node {
|
||||
ast::ExprKind::InPlace(ref e, _) |
|
||||
|
Loading…
x
Reference in New Issue
Block a user