1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use visitor::FmtVisitor;
14 use syntax::parse::token;
15 use syntax::print::pprust;
17 impl<'a> FmtVisitor<'a> {
18 pub fn rewrite_pred(&self, predicate: &ast::WherePredicate) -> String {
20 // TODO assumes we'll always fit on one line...
22 &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bound_lifetimes,
26 if bound_lifetimes.len() > 0 {
27 format!("for<{}> {}: {}",
28 bound_lifetimes.iter().map(|l| self.rewrite_lifetime_def(l)).collect::<Vec<_>>().connect(", "),
29 pprust::ty_to_string(bounded_ty),
30 bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect(" + "))
34 pprust::ty_to_string(bounded_ty),
35 bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect(" + "))
38 &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
42 pprust::lifetime_to_string(lifetime),
43 bounds.iter().map(|l| pprust::lifetime_to_string(l)).collect::<Vec<_>>().connect(" + "))
45 &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
46 format!("{} = {}", pprust::path_to_string(path), pprust::ty_to_string(ty))
51 pub fn rewrite_lifetime_def(&self, lifetime: &ast::LifetimeDef) -> String {
52 if lifetime.bounds.len() == 0 {
53 return pprust::lifetime_to_string(&lifetime.lifetime);
57 pprust::lifetime_to_string(&lifetime.lifetime),
58 lifetime.bounds.iter().map(|l| pprust::lifetime_to_string(l)).collect::<Vec<_>>().connect(" + "))
61 pub fn rewrite_ty_bound(&self, bound: &ast::TyParamBound) -> String {
63 ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::None) => {
64 self.rewrite_poly_trait_ref(tref)
66 ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => {
67 format!("?{}", self.rewrite_poly_trait_ref(tref))
69 ast::TyParamBound::RegionTyParamBound(ref l) => {
70 pprust::lifetime_to_string(l)
75 pub fn rewrite_ty_param(&self, ty_param: &ast::TyParam) -> String {
76 let mut result = String::with_capacity(128);
77 result.push_str(&token::get_ident(ty_param.ident));
78 if ty_param.bounds.len() > 0 {
79 result.push_str(": ");
80 result.push_str(&ty_param.bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect(" + "));
82 if let Some(ref def) = ty_param.default {
83 result.push_str(" = ");
84 result.push_str(&pprust::ty_to_string(&def));
90 fn rewrite_poly_trait_ref(&self, t: &ast::PolyTraitRef) -> String {
91 if t.bound_lifetimes.len() > 0 {
93 t.bound_lifetimes.iter().map(|l| self.rewrite_lifetime_def(l)).collect::<Vec<_>>().connect(", "),
94 pprust::path_to_string(&t.trait_ref.path))
97 pprust::path_to_string(&t.trait_ref.path)