]> git.lizzy.rs Git - rust.git/blob - src/types.rs
Fix fn decl rewriting in case of generics
[rust.git] / src / types.rs
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.
4 //
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.
10
11 use visitor::FmtVisitor;
12
13 use syntax::ast;
14 use syntax::parse::token;
15 use syntax::print::pprust;
16
17 impl<'a> FmtVisitor<'a> {
18     pub fn rewrite_pred(&self, predicate: &ast::WherePredicate) -> String
19     {
20         // TODO dead spans
21         // TODO assumes we'll always fit on one line...
22         match predicate {
23             &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bound_lifetimes,
24                                                                           ref bounded_ty,
25                                                                           ref bounds,
26                                                                           ..}) => {
27                 if bound_lifetimes.len() > 0 {
28                     format!("for<{}> {}: {}",
29                             bound_lifetimes.iter().map(|l| self.rewrite_lifetime_def(l)).collect::<Vec<_>>().connect(", "),
30                             pprust::ty_to_string(bounded_ty),
31                             bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect(" + "))
32
33                 } else {
34                     format!("{}: {}",
35                             pprust::ty_to_string(bounded_ty),
36                             bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect(" + "))
37                 }
38             }
39             &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
40                                                                             ref bounds,
41                                                                             ..}) => {
42                 format!("{}: {}",
43                         pprust::lifetime_to_string(lifetime),
44                         bounds.iter().map(|l| pprust::lifetime_to_string(l)).collect::<Vec<_>>().connect(" + "))
45             }
46             &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
47                 format!("{} = {}", pprust::path_to_string(path), pprust::ty_to_string(ty))
48             }
49         }
50     }
51
52     pub fn rewrite_lifetime_def(&self, lifetime: &ast::LifetimeDef) -> String
53     {
54         if lifetime.bounds.len() == 0 {
55             return pprust::lifetime_to_string(&lifetime.lifetime);
56         }
57
58         format!("{}: {}",
59                 pprust::lifetime_to_string(&lifetime.lifetime),
60                 lifetime.bounds.iter().map(|l| pprust::lifetime_to_string(l)).collect::<Vec<_>>().connect(" + "))
61     }
62
63     pub fn rewrite_ty_bound(&self, bound: &ast::TyParamBound) -> String
64     {
65         match *bound {
66             ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::None) => {
67                 self.rewrite_poly_trait_ref(tref)
68             }
69             ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => {
70                 format!("?{}", self.rewrite_poly_trait_ref(tref))
71             }
72             ast::TyParamBound::RegionTyParamBound(ref l) => {
73                 pprust::lifetime_to_string(l)
74             }
75         }
76     }
77
78     pub fn rewrite_ty_param(&self, ty_param: &ast::TyParam) -> String
79     {
80         let mut result = String::with_capacity(128);
81         result.push_str(&token::get_ident(ty_param.ident));
82         if ty_param.bounds.len() > 0 {
83             result.push_str(": ");
84             result.push_str(&ty_param.bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect(" + "));
85         }
86         if let Some(ref def) = ty_param.default {
87             result.push_str(" = ");
88             result.push_str(&pprust::ty_to_string(&def));
89         }
90
91         result
92     }
93
94     fn rewrite_poly_trait_ref(&self, t: &ast::PolyTraitRef) -> String
95     {
96         if t.bound_lifetimes.len() > 0 {
97             format!("for<{}> {}",
98                     t.bound_lifetimes.iter().map(|l| self.rewrite_lifetime_def(l)).collect::<Vec<_>>().connect(", "),
99                     pprust::path_to_string(&t.trait_ref.path))
100
101         } else {
102             pprust::path_to_string(&t.trait_ref.path)
103         }
104     }
105 }