2015-04-21 21:01:19 +12:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-12-06 01:11:26 +01:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::iter::ExactSizeIterator;
|
|
|
|
|
2017-01-19 10:47:07 +13:00
|
|
|
use syntax::abi;
|
2015-11-22 16:07:38 +01:00
|
|
|
use syntax::ast::{self, Mutability, FunctionRetTy};
|
2015-11-14 13:52:43 -08:00
|
|
|
use syntax::codemap::{self, Span, BytePos};
|
2017-01-19 10:47:07 +13:00
|
|
|
use syntax::print::pprust;
|
|
|
|
use syntax::symbol::keywords;
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2017-01-31 08:28:48 +13:00
|
|
|
use {Shape, Spanned};
|
2016-05-25 20:41:26 +02:00
|
|
|
use codemap::SpanUtils;
|
2015-10-17 15:56:53 +02:00
|
|
|
use lists::{format_item_list, itemize_list, format_fn_args};
|
2015-08-14 14:09:19 +02:00
|
|
|
use rewrite::{Rewrite, RewriteContext};
|
2017-03-28 23:16:52 +09:00
|
|
|
use utils::{extra_offset, format_mutability, colon_spaces, wrap_str};
|
2017-05-23 11:20:29 +09:00
|
|
|
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple_type};
|
2016-01-11 12:26:57 -07:00
|
|
|
use config::TypeDensity;
|
2015-08-14 14:09:19 +02:00
|
|
|
|
2017-01-19 10:47:07 +13:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub enum PathContext {
|
|
|
|
Expr,
|
|
|
|
Type,
|
|
|
|
Import,
|
|
|
|
}
|
|
|
|
|
2015-08-14 14:09:19 +02:00
|
|
|
// Does not wrap on simple segments.
|
|
|
|
pub fn rewrite_path(context: &RewriteContext,
|
2017-01-19 10:47:07 +13:00
|
|
|
path_context: PathContext,
|
2015-08-14 14:09:19 +02:00
|
|
|
qself: Option<&ast::QSelf>,
|
|
|
|
path: &ast::Path,
|
2017-01-31 08:28:48 +13:00
|
|
|
shape: Shape)
|
2015-08-14 14:09:19 +02:00
|
|
|
-> Option<String> {
|
2016-01-07 12:13:45 +05:30
|
|
|
let skip_count = qself.map_or(0, |x| x.position);
|
2015-08-14 14:09:19 +02:00
|
|
|
|
2017-01-19 10:47:07 +13:00
|
|
|
let mut result = if path.is_global() && qself.is_none() &&
|
|
|
|
path_context != PathContext::Import {
|
2015-08-14 14:09:19 +02:00
|
|
|
"::".to_owned()
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut span_lo = path.span.lo;
|
|
|
|
|
2016-08-23 23:14:45 +09:00
|
|
|
if let Some(qself) = qself {
|
2015-08-14 14:09:19 +02:00
|
|
|
result.push('<');
|
2017-05-16 15:47:09 +07:00
|
|
|
if context.config.spaces_within_angle_brackets() {
|
2016-10-13 18:48:22 +03:00
|
|
|
result.push_str(" ")
|
|
|
|
}
|
|
|
|
|
2017-01-31 08:28:48 +13:00
|
|
|
let fmt_ty = try_opt!(qself.ty.rewrite(context, shape));
|
2015-09-27 10:09:08 -04:00
|
|
|
result.push_str(&fmt_ty);
|
2015-08-14 14:09:19 +02:00
|
|
|
|
2015-09-06 23:18:27 +02:00
|
|
|
if skip_count > 0 {
|
|
|
|
result.push_str(" as ");
|
2017-01-19 10:47:07 +13:00
|
|
|
if path.is_global() && path_context != PathContext::Import {
|
2016-05-02 17:11:22 -07:00
|
|
|
result.push_str("::");
|
|
|
|
}
|
2015-08-14 14:09:19 +02:00
|
|
|
|
2017-02-21 14:43:43 +13:00
|
|
|
let extra_offset = extra_offset(&result, shape);
|
2015-09-06 23:18:27 +02:00
|
|
|
// 3 = ">::".len()
|
2017-02-21 14:43:43 +13:00
|
|
|
let shape = try_opt!(try_opt!(shape.shrink_left(extra_offset)).sub_width(3));
|
2015-09-06 23:18:27 +02:00
|
|
|
|
2017-01-19 10:47:07 +13:00
|
|
|
result = try_opt!(rewrite_path_segments(PathContext::Type,
|
2015-11-14 13:52:43 -08:00
|
|
|
result,
|
2015-09-06 23:18:27 +02:00
|
|
|
path.segments.iter().take(skip_count),
|
|
|
|
span_lo,
|
|
|
|
path.span.hi,
|
|
|
|
context,
|
2017-02-21 14:43:43 +13:00
|
|
|
shape));
|
2015-09-06 23:18:27 +02:00
|
|
|
}
|
2015-08-14 14:09:19 +02:00
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
if context.config.spaces_within_angle_brackets() {
|
2016-10-13 18:48:22 +03:00
|
|
|
result.push_str(" ")
|
|
|
|
}
|
|
|
|
|
2015-08-14 14:09:19 +02:00
|
|
|
result.push_str(">::");
|
|
|
|
span_lo = qself.ty.span.hi + BytePos(1);
|
|
|
|
}
|
|
|
|
|
2017-01-19 10:47:07 +13:00
|
|
|
rewrite_path_segments(path_context,
|
2015-11-14 13:52:43 -08:00
|
|
|
result,
|
2015-08-14 14:09:19 +02:00
|
|
|
path.segments.iter().skip(skip_count),
|
|
|
|
span_lo,
|
|
|
|
path.span.hi,
|
|
|
|
context,
|
2017-02-21 14:43:43 +13:00
|
|
|
shape)
|
2015-08-14 14:09:19 +02:00
|
|
|
}
|
|
|
|
|
2017-01-19 10:47:07 +13:00
|
|
|
fn rewrite_path_segments<'a, I>(path_context: PathContext,
|
2015-11-14 13:52:43 -08:00
|
|
|
mut buffer: String,
|
2015-08-14 14:09:19 +02:00
|
|
|
iter: I,
|
|
|
|
mut span_lo: BytePos,
|
|
|
|
span_hi: BytePos,
|
|
|
|
context: &RewriteContext,
|
2017-01-31 08:28:48 +13:00
|
|
|
shape: Shape)
|
2015-08-14 14:09:19 +02:00
|
|
|
-> Option<String>
|
|
|
|
where I: Iterator<Item = &'a ast::PathSegment>
|
|
|
|
{
|
|
|
|
let mut first = true;
|
2017-02-21 14:43:43 +13:00
|
|
|
let shape = shape.visual_indent(0);
|
2015-08-14 14:09:19 +02:00
|
|
|
|
|
|
|
for segment in iter {
|
2017-01-19 10:47:07 +13:00
|
|
|
// Indicates a global path, shouldn't be rendered.
|
|
|
|
if segment.identifier.name == keywords::CrateRoot.name() {
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-04 18:09:05 +02:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
buffer.push_str("::");
|
|
|
|
}
|
|
|
|
|
2017-02-21 14:43:43 +13:00
|
|
|
let extra_offset = extra_offset(&buffer, shape);
|
|
|
|
let new_shape = try_opt!(shape.shrink_left(extra_offset));
|
2017-01-19 10:47:07 +13:00
|
|
|
let segment_string = try_opt!(rewrite_segment(path_context,
|
2015-11-14 13:52:43 -08:00
|
|
|
segment,
|
2015-08-14 14:09:19 +02:00
|
|
|
&mut span_lo,
|
|
|
|
span_hi,
|
|
|
|
context,
|
2017-02-21 14:43:43 +13:00
|
|
|
new_shape));
|
2015-08-14 14:09:19 +02:00
|
|
|
|
|
|
|
buffer.push_str(&segment_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(buffer)
|
|
|
|
}
|
|
|
|
|
2015-09-03 08:57:22 +12:00
|
|
|
#[derive(Debug)]
|
2015-08-14 14:09:19 +02:00
|
|
|
enum SegmentParam<'a> {
|
|
|
|
LifeTime(&'a ast::Lifetime),
|
|
|
|
Type(&'a ast::Ty),
|
|
|
|
Binding(&'a ast::TypeBinding),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SegmentParam<'a> {
|
|
|
|
fn get_span(&self) -> Span {
|
|
|
|
match *self {
|
2016-08-23 23:14:45 +09:00
|
|
|
SegmentParam::LifeTime(lt) => lt.span,
|
|
|
|
SegmentParam::Type(ty) => ty.span,
|
|
|
|
SegmentParam::Binding(binding) => binding.span,
|
2015-08-14 14:09:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 08:57:22 +12:00
|
|
|
impl<'a> Rewrite for SegmentParam<'a> {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-10-17 14:19:55 +02:00
|
|
|
match *self {
|
2017-01-31 08:28:48 +13:00
|
|
|
SegmentParam::LifeTime(lt) => lt.rewrite(context, shape),
|
|
|
|
SegmentParam::Type(ty) => ty.rewrite(context, shape),
|
2016-08-23 23:14:45 +09:00
|
|
|
SegmentParam::Binding(binding) => {
|
2017-05-16 15:47:09 +07:00
|
|
|
let mut result = match context.config.type_punctuation_density() {
|
2017-02-22 15:11:16 -05:00
|
|
|
TypeDensity::Wide => format!("{} = ", binding.ident),
|
|
|
|
TypeDensity::Compressed => format!("{}=", binding.ident),
|
|
|
|
};
|
2017-01-31 08:28:48 +13:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(result.len()));
|
2017-05-16 23:24:38 +09:00
|
|
|
let rewrite = try_opt!(binding.ty.rewrite(context,
|
|
|
|
Shape::legacy(budget,
|
|
|
|
shape.indent +
|
|
|
|
result.len())));
|
2015-10-17 14:19:55 +02:00
|
|
|
result.push_str(&rewrite);
|
|
|
|
Some(result)
|
2015-09-09 23:17:31 +02:00
|
|
|
}
|
2015-10-17 14:19:55 +02:00
|
|
|
}
|
2015-08-14 14:09:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Formats a path segment. There are some hacks involved to correctly determine
|
|
|
|
// the segment's associated span since it's not part of the AST.
|
|
|
|
//
|
|
|
|
// The span_lo is assumed to be greater than the end of any previous segment's
|
|
|
|
// parameters and lesser or equal than the start of current segment.
|
|
|
|
//
|
|
|
|
// span_hi is assumed equal to the end of the entire path.
|
|
|
|
//
|
|
|
|
// When the segment contains a positive number of parameters, we update span_lo
|
|
|
|
// so that invariants described above will hold for the next segment.
|
2017-01-19 10:47:07 +13:00
|
|
|
fn rewrite_segment(path_context: PathContext,
|
2015-11-14 13:52:43 -08:00
|
|
|
segment: &ast::PathSegment,
|
2015-08-14 14:09:19 +02:00
|
|
|
span_lo: &mut BytePos,
|
|
|
|
span_hi: BytePos,
|
|
|
|
context: &RewriteContext,
|
2017-01-31 08:28:48 +13:00
|
|
|
shape: Shape)
|
2015-08-14 14:09:19 +02:00
|
|
|
-> Option<String> {
|
|
|
|
let ident_len = segment.identifier.to_string().len();
|
2017-02-21 14:43:43 +13:00
|
|
|
let shape = try_opt!(shape.shrink_left(ident_len));
|
2015-08-14 14:09:19 +02:00
|
|
|
|
2017-01-19 10:47:07 +13:00
|
|
|
let params = if let Some(ref params) = segment.parameters {
|
|
|
|
match **params {
|
|
|
|
ast::PathParameters::AngleBracketed(ref data) if !data.lifetimes.is_empty() ||
|
|
|
|
!data.types.is_empty() ||
|
|
|
|
!data.bindings.is_empty() => {
|
|
|
|
let param_list = data.lifetimes
|
|
|
|
.iter()
|
|
|
|
.map(SegmentParam::LifeTime)
|
2017-03-28 11:25:59 +13:00
|
|
|
.chain(data.types.iter().map(|x| SegmentParam::Type(&*x)))
|
|
|
|
.chain(data.bindings.iter().map(|x| SegmentParam::Binding(&*x)))
|
2017-01-19 10:47:07 +13:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2017-03-28 11:14:47 +13:00
|
|
|
let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1);
|
|
|
|
let list_lo = context
|
|
|
|
.codemap
|
|
|
|
.span_after(codemap::mk_sp(*span_lo, span_hi), "<");
|
2017-01-19 10:47:07 +13:00
|
|
|
let separator = if path_context == PathContext::Expr {
|
|
|
|
"::"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
// 1 for <
|
|
|
|
let extra_offset = 1 + separator.len();
|
|
|
|
// 1 for >
|
2017-02-21 14:43:43 +13:00
|
|
|
// TODO bad visual indent
|
2017-02-22 16:20:50 +13:00
|
|
|
let list_shape = try_opt!(try_opt!(shape.shrink_left(extra_offset)).sub_width(1))
|
|
|
|
.visual_indent(0);
|
2017-01-19 10:47:07 +13:00
|
|
|
|
2017-01-31 08:28:48 +13:00
|
|
|
let items = itemize_list(context.codemap,
|
|
|
|
param_list.into_iter(),
|
|
|
|
">",
|
|
|
|
|param| param.get_span().lo,
|
|
|
|
|param| param.get_span().hi,
|
2017-02-21 14:43:43 +13:00
|
|
|
|seg| seg.rewrite(context, list_shape),
|
2017-01-31 08:28:48 +13:00
|
|
|
list_lo,
|
|
|
|
span_hi);
|
2017-02-21 14:43:43 +13:00
|
|
|
let list_str = try_opt!(format_item_list(items, list_shape, context.config));
|
2017-01-19 10:47:07 +13:00
|
|
|
|
|
|
|
// Update position of last bracket.
|
|
|
|
*span_lo = next_span_lo;
|
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
if context.config.spaces_within_angle_brackets() && list_str.len() > 0 {
|
2017-01-19 10:47:07 +13:00
|
|
|
format!("{}< {} >", separator, list_str)
|
|
|
|
} else {
|
|
|
|
format!("{}<{}>", separator, list_str)
|
|
|
|
}
|
2016-10-13 18:48:22 +03:00
|
|
|
}
|
2017-01-19 10:47:07 +13:00
|
|
|
ast::PathParameters::Parenthesized(ref data) => {
|
|
|
|
let output = match data.output {
|
|
|
|
Some(ref ty) => FunctionRetTy::Ty(ty.clone()),
|
|
|
|
None => FunctionRetTy::Default(codemap::DUMMY_SP),
|
|
|
|
};
|
|
|
|
try_opt!(format_function_type(data.inputs.iter().map(|x| &**x),
|
|
|
|
&output,
|
|
|
|
false,
|
|
|
|
data.span,
|
|
|
|
context,
|
2017-02-21 14:43:43 +13:00
|
|
|
shape))
|
2017-01-19 10:47:07 +13:00
|
|
|
}
|
|
|
|
_ => String::new(),
|
2015-08-14 14:09:19 +02:00
|
|
|
}
|
2017-01-19 10:47:07 +13:00
|
|
|
} else {
|
|
|
|
String::new()
|
2015-08-14 14:09:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Some(format!("{}{}", segment.identifier, params))
|
|
|
|
}
|
|
|
|
|
2015-11-22 16:07:38 +01:00
|
|
|
fn format_function_type<'a, I>(inputs: I,
|
2015-11-23 13:23:41 +13:00
|
|
|
output: &FunctionRetTy,
|
2016-04-11 21:20:03 +12:00
|
|
|
variadic: bool,
|
2015-11-22 16:07:38 +01:00
|
|
|
span: Span,
|
|
|
|
context: &RewriteContext,
|
2017-01-31 08:28:48 +13:00
|
|
|
shape: Shape)
|
2015-11-22 16:07:38 +01:00
|
|
|
-> Option<String>
|
2015-12-06 01:11:26 +01:00
|
|
|
where I: ExactSizeIterator,
|
|
|
|
<I as Iterator>::Item: Deref,
|
|
|
|
<I::Item as Deref>::Target: Rewrite + Spanned + 'a
|
2015-11-22 16:07:38 +01:00
|
|
|
{
|
2016-04-11 21:20:03 +12:00
|
|
|
// Code for handling variadics is somewhat duplicated for items, but they
|
|
|
|
// are different enough to need some serious refactoring to share code.
|
|
|
|
enum ArgumentKind<T>
|
|
|
|
where T: Deref,
|
|
|
|
<T as Deref>::Target: Rewrite + Spanned
|
|
|
|
{
|
|
|
|
Regular(Box<T>),
|
|
|
|
Variadic(BytePos),
|
|
|
|
}
|
|
|
|
|
|
|
|
let variadic_arg = if variadic {
|
|
|
|
let variadic_start = context.codemap.span_before(span, "...");
|
|
|
|
Some(ArgumentKind::Variadic(variadic_start))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2015-11-22 16:07:38 +01:00
|
|
|
// 2 for ()
|
2017-01-31 08:28:48 +13:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(2));
|
2015-11-22 16:07:38 +01:00
|
|
|
// 1 for (
|
2017-01-31 08:28:48 +13:00
|
|
|
let offset = shape.indent + 1;
|
2016-03-07 13:41:32 -05:00
|
|
|
let list_lo = context.codemap.span_after(span, "(");
|
2017-03-28 11:14:47 +13:00
|
|
|
let items = itemize_list(context.codemap,
|
|
|
|
// FIXME Would be nice to avoid this allocation,
|
|
|
|
// but I couldn't get the types to work out.
|
|
|
|
inputs
|
|
|
|
.map(|i| ArgumentKind::Regular(Box::new(i)))
|
|
|
|
.chain(variadic_arg),
|
|
|
|
")",
|
|
|
|
|arg| match *arg {
|
|
|
|
ArgumentKind::Regular(ref ty) => ty.span().lo,
|
|
|
|
ArgumentKind::Variadic(start) => start,
|
|
|
|
},
|
|
|
|
|arg| match *arg {
|
|
|
|
ArgumentKind::Regular(ref ty) => ty.span().hi,
|
|
|
|
ArgumentKind::Variadic(start) => start + BytePos(3),
|
|
|
|
},
|
|
|
|
|arg| match *arg {
|
|
|
|
ArgumentKind::Regular(ref ty) => {
|
|
|
|
ty.rewrite(context, Shape::legacy(budget, offset))
|
|
|
|
}
|
|
|
|
ArgumentKind::Variadic(_) => Some("...".to_owned()),
|
|
|
|
},
|
|
|
|
list_lo,
|
|
|
|
span.hi);
|
2017-01-31 08:28:48 +13:00
|
|
|
|
|
|
|
let list_str = try_opt!(format_fn_args(items, Shape::legacy(budget, offset), context.config));
|
2015-11-22 16:07:38 +01:00
|
|
|
|
2015-11-23 13:23:41 +13:00
|
|
|
let output = match *output {
|
2016-03-01 17:27:19 -05:00
|
|
|
FunctionRetTy::Ty(ref ty) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(4));
|
|
|
|
let type_str = try_opt!(ty.rewrite(context, Shape::legacy(budget, offset + 4)));
|
2015-11-22 16:07:38 +01:00
|
|
|
format!(" -> {}", type_str)
|
|
|
|
}
|
2016-03-01 17:27:19 -05:00
|
|
|
FunctionRetTy::Default(..) => String::new(),
|
2015-11-22 16:07:38 +01:00
|
|
|
};
|
|
|
|
|
2017-01-31 08:28:48 +13:00
|
|
|
let infix = if !output.is_empty() && output.len() + list_str.len() > shape.width {
|
2015-11-22 16:07:38 +01:00
|
|
|
format!("\n{}", (offset - 1).to_string(context.config))
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
Some(if context.config.spaces_within_parens() {
|
2017-02-22 16:20:50 +13:00
|
|
|
format!("( {} ){}{}", list_str, infix, output)
|
|
|
|
} else {
|
|
|
|
format!("({}){}{}", list_str, infix, output)
|
|
|
|
})
|
2015-11-22 16:07:38 +01:00
|
|
|
}
|
|
|
|
|
2017-01-09 06:58:06 +04:00
|
|
|
fn type_bound_colon(context: &RewriteContext) -> &'static str {
|
2017-05-16 15:47:09 +07:00
|
|
|
colon_spaces(context.config.space_before_bound(),
|
|
|
|
context.config.space_after_bound_colon())
|
2017-01-09 06:58:06 +04:00
|
|
|
}
|
|
|
|
|
2015-08-14 14:09:19 +02:00
|
|
|
impl Rewrite for ast::WherePredicate {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-09-17 20:21:06 +02:00
|
|
|
// TODO: dead spans?
|
2015-10-17 14:19:55 +02:00
|
|
|
let result = match *self {
|
2017-03-22 09:05:50 +13:00
|
|
|
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
|
|
|
|
ref bound_lifetimes,
|
|
|
|
ref bounded_ty,
|
|
|
|
ref bounds,
|
|
|
|
..
|
|
|
|
}) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
let type_str = try_opt!(bounded_ty.rewrite(context, shape));
|
2015-09-27 17:56:38 +02:00
|
|
|
|
2017-01-09 06:58:06 +04:00
|
|
|
let colon = type_bound_colon(context);
|
|
|
|
|
2015-09-09 23:09:39 +02:00
|
|
|
if !bound_lifetimes.is_empty() {
|
2017-05-23 22:13:29 +09:00
|
|
|
let lifetime_str: String = try_opt!(bound_lifetimes
|
|
|
|
.iter()
|
|
|
|
.map(|lt| lt.rewrite(context, shape))
|
2017-05-25 16:23:07 +12:00
|
|
|
.collect::<Option<Vec<_>>>())
|
|
|
|
.join(", ");
|
2016-06-06 22:30:40 +01:00
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
let joiner = match context.config.type_punctuation_density() {
|
2017-04-24 19:59:21 +02:00
|
|
|
TypeDensity::Compressed => "+",
|
|
|
|
TypeDensity::Wide => " + ",
|
|
|
|
};
|
2017-01-09 06:58:06 +04:00
|
|
|
// 6 = "for<> ".len()
|
|
|
|
let used_width = lifetime_str.len() + type_str.len() + colon.len() + 6;
|
2017-01-31 08:28:48 +13:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(used_width));
|
2017-06-04 15:25:07 +09:00
|
|
|
let bounds_str: String = try_opt!(
|
|
|
|
bounds
|
|
|
|
.iter()
|
|
|
|
.map(|ty_bound| {
|
|
|
|
ty_bound.rewrite(context,
|
|
|
|
Shape::legacy(budget, shape.indent + used_width))
|
|
|
|
})
|
|
|
|
.collect::<Option<Vec<_>>>()
|
2017-06-05 15:32:21 +09:00
|
|
|
).join(joiner);
|
2015-09-09 23:09:39 +02:00
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
if context.config.spaces_within_angle_brackets() && lifetime_str.len() > 0 {
|
2017-03-22 09:05:50 +13:00
|
|
|
format!("for< {} > {}{}{}",
|
|
|
|
lifetime_str,
|
|
|
|
type_str,
|
|
|
|
colon,
|
|
|
|
bounds_str)
|
2016-10-13 18:48:22 +03:00
|
|
|
} else {
|
2017-01-09 06:58:06 +04:00
|
|
|
format!("for<{}> {}{}{}", lifetime_str, type_str, colon, bounds_str)
|
2016-10-13 18:48:22 +03:00
|
|
|
}
|
2015-09-09 23:09:39 +02:00
|
|
|
} else {
|
2017-05-16 15:47:09 +07:00
|
|
|
let joiner = match context.config.type_punctuation_density() {
|
2017-04-24 19:59:21 +02:00
|
|
|
TypeDensity::Compressed => "+",
|
|
|
|
TypeDensity::Wide => " + ",
|
|
|
|
};
|
2017-01-09 06:58:06 +04:00
|
|
|
let used_width = type_str.len() + colon.len();
|
2017-01-31 08:28:48 +13:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(used_width));
|
2017-06-04 15:25:07 +09:00
|
|
|
let bounds_str: String = try_opt!(
|
|
|
|
bounds
|
|
|
|
.iter()
|
|
|
|
.map(|ty_bound| {
|
|
|
|
ty_bound.rewrite(context,
|
|
|
|
Shape::legacy(budget, shape.indent + used_width))
|
|
|
|
})
|
|
|
|
.collect::<Option<Vec<_>>>()
|
2017-06-05 15:32:21 +09:00
|
|
|
).join(joiner);
|
2015-09-09 23:09:39 +02:00
|
|
|
|
2017-01-09 06:58:06 +04:00
|
|
|
format!("{}{}{}", type_str, colon, bounds_str)
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
2015-09-09 23:09:39 +02:00
|
|
|
}
|
2017-03-22 09:05:50 +13:00
|
|
|
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
|
|
|
|
ref lifetime,
|
|
|
|
ref bounds,
|
|
|
|
..
|
|
|
|
}) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
try_opt!(rewrite_bounded_lifetime(lifetime, bounds.iter(), context, shape))
|
2015-09-09 23:09:39 +02:00
|
|
|
}
|
2017-03-22 09:05:50 +13:00
|
|
|
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
|
|
|
|
ref lhs_ty,
|
|
|
|
ref rhs_ty,
|
|
|
|
..
|
|
|
|
}) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
let lhs_ty_str = try_opt!(lhs_ty.rewrite(context, shape));
|
2015-08-14 14:09:19 +02:00
|
|
|
// 3 = " = ".len()
|
2017-01-19 10:47:07 +13:00
|
|
|
let used_width = 3 + lhs_ty_str.len();
|
2017-01-31 08:28:48 +13:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(used_width));
|
|
|
|
let rhs_ty_str = try_opt!(rhs_ty.rewrite(context,
|
2017-02-22 16:20:50 +13:00
|
|
|
Shape::legacy(budget,
|
|
|
|
shape.indent + used_width)));
|
2017-01-19 10:47:07 +13:00
|
|
|
format!("{} = {}", lhs_ty_str, rhs_ty_str)
|
2015-09-09 23:09:39 +02:00
|
|
|
}
|
2015-10-17 14:19:55 +02:00
|
|
|
};
|
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
wrap_str(result, context.config.max_width(), shape)
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
2015-08-14 14:09:19 +02:00
|
|
|
}
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2015-08-14 14:09:19 +02:00
|
|
|
impl Rewrite for ast::LifetimeDef {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
|
|
|
rewrite_bounded_lifetime(&self.lifetime, self.bounds.iter(), context, shape)
|
2015-12-25 20:59:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_bounded_lifetime<'b, I>(lt: &ast::Lifetime,
|
|
|
|
bounds: I,
|
|
|
|
context: &RewriteContext,
|
2017-01-31 08:28:48 +13:00
|
|
|
shape: Shape)
|
2015-12-25 20:59:46 +01:00
|
|
|
-> Option<String>
|
|
|
|
where I: ExactSizeIterator<Item = &'b ast::Lifetime>
|
|
|
|
{
|
2017-01-31 08:28:48 +13:00
|
|
|
let result = try_opt!(lt.rewrite(context, shape));
|
2015-10-17 14:19:55 +02:00
|
|
|
|
2015-12-25 20:59:46 +01:00
|
|
|
if bounds.len() == 0 {
|
|
|
|
Some(result)
|
|
|
|
} else {
|
2017-03-28 11:14:47 +13:00
|
|
|
let appendix: Vec<_> = try_opt!(bounds
|
|
|
|
.into_iter()
|
|
|
|
.map(|b| b.rewrite(context, shape))
|
|
|
|
.collect());
|
2017-01-09 06:58:06 +04:00
|
|
|
let colon = type_bound_colon(context);
|
2017-05-16 15:47:09 +07:00
|
|
|
let joiner = match context.config.type_punctuation_density() {
|
2017-04-24 19:59:21 +02:00
|
|
|
TypeDensity::Compressed => "+",
|
|
|
|
TypeDensity::Wide => " + ",
|
|
|
|
};
|
|
|
|
let result = format!("{}{}{}", result, colon, appendix.join(joiner));
|
2017-05-16 15:47:09 +07:00
|
|
|
wrap_str(result, context.config.max_width(), shape)
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
2015-08-14 14:09:19 +02:00
|
|
|
}
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2015-08-14 14:09:19 +02:00
|
|
|
impl Rewrite for ast::TyParamBound {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-08-14 14:09:19 +02:00
|
|
|
match *self {
|
2015-04-21 21:01:19 +12:00
|
|
|
ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::None) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
tref.rewrite(context, shape)
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
|
|
|
ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(1));
|
|
|
|
Some(format!("?{}",
|
|
|
|
try_opt!(tref.rewrite(context,
|
|
|
|
Shape::legacy(budget, shape.indent + 1)))))
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
2017-01-31 08:28:48 +13:00
|
|
|
ast::TyParamBound::RegionTyParamBound(ref l) => l.rewrite(context, shape),
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
|
|
|
}
|
2015-08-14 14:09:19 +02:00
|
|
|
}
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2015-12-25 20:59:46 +01:00
|
|
|
impl Rewrite for ast::Lifetime {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-12-25 20:59:46 +01:00
|
|
|
wrap_str(pprust::lifetime_to_string(self),
|
2017-05-16 15:47:09 +07:00
|
|
|
context.config.max_width(),
|
2017-01-31 08:28:48 +13:00
|
|
|
shape)
|
2015-12-25 20:59:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 08:57:22 +12:00
|
|
|
impl Rewrite for ast::TyParamBounds {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2017-05-16 15:47:09 +07:00
|
|
|
let joiner = match context.config.type_punctuation_density() {
|
2017-01-19 10:47:07 +13:00
|
|
|
TypeDensity::Compressed => "+",
|
|
|
|
TypeDensity::Wide => " + ",
|
|
|
|
};
|
2017-03-28 11:25:59 +13:00
|
|
|
let strs: Vec<_> = try_opt!(self.iter().map(|b| b.rewrite(context, shape)).collect());
|
2017-05-16 15:47:09 +07:00
|
|
|
wrap_str(strs.join(joiner), context.config.max_width(), shape)
|
2015-09-03 08:57:22 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 14:09:19 +02:00
|
|
|
impl Rewrite for ast::TyParam {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-04-21 21:01:19 +12:00
|
|
|
let mut result = String::with_capacity(128);
|
2017-05-27 23:59:29 +09:00
|
|
|
// FIXME: If there are more than one attributes, this will force multiline.
|
|
|
|
let attr_str = match (&*self.attrs).rewrite(context, shape) {
|
|
|
|
Some(ref rw) if !rw.is_empty() => format!("{} ", rw),
|
|
|
|
_ => String::new(),
|
|
|
|
};
|
|
|
|
result.push_str(&attr_str);
|
2015-08-14 14:09:19 +02:00
|
|
|
result.push_str(&self.ident.to_string());
|
2015-08-25 00:54:47 +03:00
|
|
|
if !self.bounds.is_empty() {
|
2017-05-16 15:47:09 +07:00
|
|
|
if context.config.space_before_bound() {
|
2016-08-02 21:07:56 -04:00
|
|
|
result.push_str(" ");
|
|
|
|
}
|
2016-09-17 01:44:51 +02:00
|
|
|
result.push_str(":");
|
2017-05-16 15:47:09 +07:00
|
|
|
if context.config.space_after_bound_colon() {
|
2016-09-17 01:44:51 +02:00
|
|
|
result.push_str(" ");
|
|
|
|
}
|
2017-05-16 15:47:09 +07:00
|
|
|
let joiner = match context.config.type_punctuation_density() {
|
2017-04-24 19:59:21 +02:00
|
|
|
TypeDensity::Compressed => "+",
|
|
|
|
TypeDensity::Wide => " + ",
|
|
|
|
};
|
2017-05-23 22:13:29 +09:00
|
|
|
let bounds: String = try_opt!(self.bounds
|
|
|
|
.iter()
|
|
|
|
.map(|ty_bound| ty_bound.rewrite(context, shape))
|
2017-05-25 16:23:07 +12:00
|
|
|
.collect::<Option<Vec<_>>>())
|
|
|
|
.join(joiner);
|
2015-08-14 14:09:19 +02:00
|
|
|
|
|
|
|
result.push_str(&bounds);
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
2015-08-14 14:09:19 +02:00
|
|
|
if let Some(ref def) = self.default {
|
2016-01-12 13:42:53 -07:00
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
let eq_str = match context.config.type_punctuation_density() {
|
2016-01-12 13:42:53 -07:00
|
|
|
TypeDensity::Compressed => "=",
|
|
|
|
TypeDensity::Wide => " = ",
|
2016-01-11 12:26:57 -07:00
|
|
|
};
|
|
|
|
result.push_str(eq_str);
|
2017-01-31 08:28:48 +13:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(result.len()));
|
2017-02-22 16:20:50 +13:00
|
|
|
let rewrite = try_opt!(def.rewrite(context,
|
|
|
|
Shape::legacy(budget, shape.indent + result.len())));
|
2015-09-27 17:56:38 +02:00
|
|
|
result.push_str(&rewrite);
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
wrap_str(result, context.config.max_width(), shape)
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
2015-08-14 14:09:19 +02:00
|
|
|
}
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2015-08-14 14:09:19 +02:00
|
|
|
impl Rewrite for ast::PolyTraitRef {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-08-25 00:54:47 +03:00
|
|
|
if !self.bound_lifetimes.is_empty() {
|
2016-06-07 00:03:25 +01:00
|
|
|
let lifetime_str: String = try_opt!(self.bound_lifetimes
|
2017-02-22 16:20:50 +13:00
|
|
|
.iter()
|
|
|
|
.map(|lt| lt.rewrite(context, shape))
|
2017-05-25 16:23:07 +12:00
|
|
|
.collect::<Option<Vec<_>>>())
|
|
|
|
.join(", ");
|
2016-06-06 22:30:40 +01:00
|
|
|
|
2015-08-14 14:09:19 +02:00
|
|
|
// 6 is "for<> ".len()
|
|
|
|
let extra_offset = lifetime_str.len() + 6;
|
2017-01-31 08:28:48 +13:00
|
|
|
let max_path_width = try_opt!(shape.width.checked_sub(extra_offset));
|
2017-05-16 23:24:38 +09:00
|
|
|
let path_str = try_opt!(self.trait_ref.rewrite(context,
|
|
|
|
Shape::legacy(max_path_width,
|
|
|
|
shape.indent +
|
|
|
|
extra_offset)));
|
2015-04-21 21:01:19 +12:00
|
|
|
|
2017-05-16 15:47:09 +07:00
|
|
|
Some(if context.config.spaces_within_angle_brackets() && lifetime_str.len() > 0 {
|
2017-02-22 16:20:50 +13:00
|
|
|
format!("for< {} > {}", lifetime_str, path_str)
|
|
|
|
} else {
|
|
|
|
format!("for<{}> {}", lifetime_str, path_str)
|
|
|
|
})
|
2015-04-21 21:01:19 +12:00
|
|
|
} else {
|
2017-01-31 08:28:48 +13:00
|
|
|
self.trait_ref.rewrite(context, shape)
|
2015-04-21 21:01:19 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 08:57:22 +12:00
|
|
|
|
2015-11-23 12:00:22 +13:00
|
|
|
impl Rewrite for ast::TraitRef {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
|
|
|
rewrite_path(context, PathContext::Type, None, &self.path, shape)
|
2015-11-23 12:00:22 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 08:57:22 +12:00
|
|
|
impl Rewrite for ast::Ty {
|
2017-01-31 08:28:48 +13:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-09-03 08:57:22 +12:00
|
|
|
match self.node {
|
2017-01-31 08:28:48 +13:00
|
|
|
ast::TyKind::TraitObject(ref bounds) => bounds.rewrite(context, shape),
|
2016-03-01 17:27:19 -05:00
|
|
|
ast::TyKind::Ptr(ref mt) => {
|
2015-10-17 14:19:55 +02:00
|
|
|
let prefix = match mt.mutbl {
|
2016-03-01 17:27:19 -05:00
|
|
|
Mutability::Mutable => "*mut ",
|
|
|
|
Mutability::Immutable => "*const ",
|
2015-10-17 14:19:55 +02:00
|
|
|
};
|
|
|
|
|
2017-05-15 22:55:45 +09:00
|
|
|
rewrite_unary_prefix(context, prefix, &*mt.ty, shape)
|
2015-10-17 14:19:55 +02:00
|
|
|
}
|
2016-03-01 17:27:19 -05:00
|
|
|
ast::TyKind::Rptr(ref lifetime, ref mt) => {
|
2015-09-24 08:36:21 +12:00
|
|
|
let mut_str = format_mutability(mt.mutbl);
|
|
|
|
let mut_len = mut_str.len();
|
2015-09-27 17:56:38 +02:00
|
|
|
Some(match *lifetime {
|
2017-02-22 16:20:50 +13:00
|
|
|
Some(ref lifetime) => {
|
2017-04-15 16:22:54 +09:00
|
|
|
let lt_budget = try_opt!(shape.width.checked_sub(2 + mut_len));
|
|
|
|
let lt_str = try_opt!(lifetime.rewrite(context,
|
|
|
|
Shape::legacy(lt_budget,
|
2017-05-23 22:13:29 +09:00
|
|
|
shape.indent + 2 +
|
2017-04-15 16:22:54 +09:00
|
|
|
mut_len)));
|
|
|
|
let lt_len = lt_str.len();
|
|
|
|
let budget = try_opt!(shape.width.checked_sub(2 + mut_len + lt_len));
|
|
|
|
format!("&{} {}{}",
|
|
|
|
lt_str,
|
|
|
|
mut_str,
|
2017-05-16 23:24:38 +09:00
|
|
|
try_opt!(mt.ty.rewrite(context,
|
|
|
|
Shape::legacy(budget,
|
|
|
|
shape.indent + 2 +
|
|
|
|
mut_len +
|
|
|
|
lt_len))))
|
2017-04-15 16:22:54 +09:00
|
|
|
}
|
2017-02-22 16:20:50 +13:00
|
|
|
None => {
|
2017-04-15 16:22:54 +09:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(1 + mut_len));
|
|
|
|
format!("&{}{}",
|
|
|
|
mut_str,
|
2017-05-16 23:24:38 +09:00
|
|
|
try_opt!(mt.ty.rewrite(context,
|
|
|
|
Shape::legacy(budget,
|
|
|
|
shape.indent + 1 +
|
|
|
|
mut_len))))
|
2017-04-15 16:22:54 +09:00
|
|
|
}
|
2017-02-22 16:20:50 +13:00
|
|
|
})
|
2015-09-24 08:36:21 +12:00
|
|
|
}
|
2015-10-17 14:19:55 +02:00
|
|
|
// FIXME: we drop any comments here, even though it's a silly place to put
|
|
|
|
// comments.
|
2016-03-01 17:27:19 -05:00
|
|
|
ast::TyKind::Paren(ref ty) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(2));
|
|
|
|
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
|
2017-05-16 15:47:09 +07:00
|
|
|
.map(|ty_str| if context.config.spaces_within_parens() {
|
2017-02-22 16:20:50 +13:00
|
|
|
format!("( {} )", ty_str)
|
|
|
|
} else {
|
|
|
|
format!("({})", ty_str)
|
|
|
|
})
|
2015-09-03 08:57:22 +12:00
|
|
|
}
|
2016-11-21 08:37:35 +13:00
|
|
|
ast::TyKind::Slice(ref ty) => {
|
2017-05-16 15:47:09 +07:00
|
|
|
let budget = if context.config.spaces_within_square_brackets() {
|
2017-01-31 08:28:48 +13:00
|
|
|
try_opt!(shape.width.checked_sub(4))
|
2016-10-17 23:09:49 +03:00
|
|
|
} else {
|
2017-01-31 08:28:48 +13:00
|
|
|
try_opt!(shape.width.checked_sub(2))
|
2016-10-17 23:09:49 +03:00
|
|
|
};
|
2017-01-31 08:28:48 +13:00
|
|
|
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
|
2017-05-16 15:47:09 +07:00
|
|
|
.map(|ty_str| if context.config.spaces_within_square_brackets() {
|
2017-02-22 16:20:50 +13:00
|
|
|
format!("[ {} ]", ty_str)
|
|
|
|
} else {
|
|
|
|
format!("[{}]", ty_str)
|
|
|
|
})
|
2015-10-17 14:19:55 +02:00
|
|
|
}
|
2016-03-01 17:27:19 -05:00
|
|
|
ast::TyKind::Tup(ref items) => {
|
2017-05-23 11:20:29 +09:00
|
|
|
rewrite_tuple_type(context, items.iter().map(|x| &**x), self.span, shape)
|
2015-12-06 01:11:26 +01:00
|
|
|
}
|
2016-03-01 17:27:19 -05:00
|
|
|
ast::TyKind::Path(ref q_self, ref path) => {
|
2017-01-31 08:28:48 +13:00
|
|
|
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
|
2015-10-17 14:19:55 +02:00
|
|
|
}
|
2016-11-21 08:37:35 +13:00
|
|
|
ast::TyKind::Array(ref ty, ref repeats) => {
|
2017-05-16 15:47:09 +07:00
|
|
|
let use_spaces = context.config.spaces_within_square_brackets();
|
2016-10-17 23:09:49 +03:00
|
|
|
let lbr = if use_spaces { "[ " } else { "[" };
|
|
|
|
let rbr = if use_spaces { " ]" } else { "]" };
|
2017-01-31 08:28:48 +13:00
|
|
|
rewrite_pair(&**ty, &**repeats, lbr, "; ", rbr, context, shape)
|
2015-10-17 14:19:55 +02:00
|
|
|
}
|
2016-03-01 17:27:19 -05:00
|
|
|
ast::TyKind::Infer => {
|
2017-01-31 08:28:48 +13:00
|
|
|
if shape.width >= 1 {
|
2015-10-17 14:19:55 +02:00
|
|
|
Some("_".to_owned())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2017-01-31 08:28:48 +13:00
|
|
|
ast::TyKind::BareFn(ref bare_fn) => rewrite_bare_fn(bare_fn, self.span, context, shape),
|
2016-09-16 15:19:18 +12:00
|
|
|
ast::TyKind::Never => Some(String::from("!")),
|
2017-01-06 16:06:09 +13:00
|
|
|
ast::TyKind::Mac(..) => None,
|
2016-05-30 14:53:04 +02:00
|
|
|
ast::TyKind::ImplicitSelf => Some(String::from("")),
|
2016-10-13 12:15:06 -07:00
|
|
|
ast::TyKind::ImplTrait(ref it) => {
|
2017-03-28 11:14:47 +13:00
|
|
|
it.rewrite(context, shape)
|
|
|
|
.map(|it_str| format!("impl {}", it_str))
|
2016-09-16 15:19:18 +12:00
|
|
|
}
|
2017-01-06 16:06:09 +13:00
|
|
|
ast::TyKind::Typeof(..) => unreachable!(),
|
2015-09-03 08:57:22 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-22 13:45:51 +01:00
|
|
|
|
2015-11-22 16:07:38 +01:00
|
|
|
fn rewrite_bare_fn(bare_fn: &ast::BareFnTy,
|
|
|
|
span: Span,
|
|
|
|
context: &RewriteContext,
|
2017-01-31 08:28:48 +13:00
|
|
|
shape: Shape)
|
2015-11-22 16:07:38 +01:00
|
|
|
-> Option<String> {
|
|
|
|
let mut result = String::with_capacity(128);
|
|
|
|
|
2016-05-27 10:39:28 +12:00
|
|
|
if !bare_fn.lifetimes.is_empty() {
|
|
|
|
result.push_str("for<");
|
|
|
|
// 6 = "for<> ".len(), 4 = "for<".
|
|
|
|
// This doesn't work out so nicely for mutliline situation with lots of
|
|
|
|
// rightward drift. If that is a problem, we could use the list stuff.
|
2017-06-04 15:25:07 +09:00
|
|
|
result.push_str(&try_opt!(
|
|
|
|
bare_fn
|
|
|
|
.lifetimes
|
|
|
|
.iter()
|
|
|
|
.map(|l| {
|
|
|
|
l.rewrite(context,
|
|
|
|
Shape::legacy(try_opt!(shape.width.checked_sub(6)), shape.indent + 4))
|
|
|
|
})
|
|
|
|
.collect::<Option<Vec<_>>>()
|
|
|
|
).join(", "));
|
2016-05-27 10:39:28 +12:00
|
|
|
result.push_str("> ");
|
|
|
|
}
|
|
|
|
|
2016-08-23 23:14:45 +09:00
|
|
|
result.push_str(::utils::format_unsafety(bare_fn.unsafety));
|
2015-11-22 16:07:38 +01:00
|
|
|
|
2016-03-01 17:27:19 -05:00
|
|
|
if bare_fn.abi != abi::Abi::Rust {
|
2017-05-16 15:47:09 +07:00
|
|
|
result.push_str(&::utils::format_abi(bare_fn.abi, context.config.force_explicit_abi()));
|
2015-11-22 13:45:51 +01:00
|
|
|
}
|
2015-11-22 16:07:38 +01:00
|
|
|
|
|
|
|
result.push_str("fn");
|
|
|
|
|
2017-01-31 08:28:48 +13:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(result.len()));
|
|
|
|
let indent = shape.indent + result.len();
|
2015-11-22 16:07:38 +01:00
|
|
|
|
2015-12-06 01:11:26 +01:00
|
|
|
let rewrite = try_opt!(format_function_type(bare_fn.decl.inputs.iter(),
|
2015-11-23 13:23:41 +13:00
|
|
|
&bare_fn.decl.output,
|
2016-04-11 21:20:03 +12:00
|
|
|
bare_fn.decl.variadic,
|
2015-11-22 16:07:38 +01:00
|
|
|
span,
|
|
|
|
context,
|
2017-01-31 08:28:48 +13:00
|
|
|
Shape::legacy(budget, indent)));
|
2015-11-22 16:07:38 +01:00
|
|
|
|
|
|
|
result.push_str(&rewrite);
|
|
|
|
|
|
|
|
Some(result)
|
2015-11-22 13:45:51 +01:00
|
|
|
}
|