rust/src/types.rs
Marcus Klaas 54a96355dd Phase out token::get_ident in favour of ToString
token::get_ident was removed in rust commit 00a5e66f81
2015-07-31 13:10:19 +02:00

106 lines
4.5 KiB
Rust

// 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.
use visitor::FmtVisitor;
use syntax::ast;
use syntax::print::pprust;
impl<'a> FmtVisitor<'a> {
pub fn rewrite_pred(&self, predicate: &ast::WherePredicate) -> String {
// TODO dead spans
// TODO assumes we'll always fit on one line...
match predicate {
&ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bound_lifetimes,
ref bounded_ty,
ref bounds,
..}) => {
if bound_lifetimes.len() > 0 {
format!("for<{}> {}: {}",
bound_lifetimes.iter().map(|l| self.rewrite_lifetime_def(l))
.collect::<Vec<_>>().join(", "),
pprust::ty_to_string(bounded_ty),
bounds.iter().map(|b| self.rewrite_ty_bound(b))
.collect::<Vec<_>>().join(" + "))
} else {
format!("{}: {}",
pprust::ty_to_string(bounded_ty),
bounds.iter().map(|b| self.rewrite_ty_bound(b))
.collect::<Vec<_>>().join(" + "))
}
}
&ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
ref bounds,
..}) => {
format!("{}: {}",
pprust::lifetime_to_string(lifetime),
bounds.iter().map(|l| pprust::lifetime_to_string(l))
.collect::<Vec<_>>().join(" + "))
}
&ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
format!("{} = {}", pprust::path_to_string(path), pprust::ty_to_string(ty))
}
}
}
pub fn rewrite_lifetime_def(&self, lifetime: &ast::LifetimeDef) -> String {
if lifetime.bounds.len() == 0 {
return pprust::lifetime_to_string(&lifetime.lifetime);
}
format!("{}: {}",
pprust::lifetime_to_string(&lifetime.lifetime),
lifetime.bounds.iter().map(|l| pprust::lifetime_to_string(l))
.collect::<Vec<_>>().join(" + "))
}
pub fn rewrite_ty_bound(&self, bound: &ast::TyParamBound) -> String {
match *bound {
ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::None) => {
self.rewrite_poly_trait_ref(tref)
}
ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => {
format!("?{}", self.rewrite_poly_trait_ref(tref))
}
ast::TyParamBound::RegionTyParamBound(ref l) => {
pprust::lifetime_to_string(l)
}
}
}
pub fn rewrite_ty_param(&self, ty_param: &ast::TyParam) -> String {
let mut result = String::with_capacity(128);
result.push_str(&ty_param.ident.to_string());
if ty_param.bounds.len() > 0 {
result.push_str(": ");
result.push_str(&ty_param.bounds.iter().map(|b| self.rewrite_ty_bound(b))
.collect::<Vec<_>>().join(" + "));
}
if let Some(ref def) = ty_param.default {
result.push_str(" = ");
result.push_str(&pprust::ty_to_string(&def));
}
result
}
fn rewrite_poly_trait_ref(&self, t: &ast::PolyTraitRef) -> String {
if t.bound_lifetimes.len() > 0 {
format!("for<{}> {}",
t.bound_lifetimes.iter().map(|l| self.rewrite_lifetime_def(l))
.collect::<Vec<_>>().join(", "),
pprust::path_to_string(&t.trait_ref.path))
} else {
pprust::path_to_string(&t.trait_ref.path)
}
}
}