]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/typeck/infer/to_str.rs
eaca07b782f7df4b95faa1415a285038abeaf5e1
[rust.git] / src / librustc / middle / typeck / infer / to_str.rs
1 // Copyright 2012 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
12 use middle::ty::{FnSig, Vid};
13 use middle::ty::IntVarValue;
14 use middle::ty;
15 use middle::typeck::infer::{Bound, Bounds};
16 use middle::typeck::infer::InferCtxt;
17 use middle::typeck::infer::unify::{Redirect, Root, VarValue};
18 use util::ppaux::{mt_to_str, ty_to_str, trait_ref_to_str};
19
20 use syntax::ast;
21
22 pub trait InferStr {
23     fn inf_str(&self, cx: &InferCtxt) -> String;
24 }
25
26 impl InferStr for ty::t {
27     fn inf_str(&self, cx: &InferCtxt) -> String {
28         ty_to_str(cx.tcx, *self)
29     }
30 }
31
32 impl InferStr for FnSig {
33     fn inf_str(&self, cx: &InferCtxt) -> String {
34         format_strbuf!("({}) -> {}",
35                        self.inputs
36                            .iter()
37                            .map(|a| a.inf_str(cx))
38                            .collect::<Vec<String>>().connect(", "),
39                        self.output.inf_str(cx))
40     }
41 }
42
43 impl InferStr for ty::mt {
44     fn inf_str(&self, cx: &InferCtxt) -> String {
45         mt_to_str(cx.tcx, self)
46     }
47 }
48
49 impl InferStr for ty::Region {
50     fn inf_str(&self, _cx: &InferCtxt) -> String {
51         format_strbuf!("{:?}", *self)
52     }
53 }
54
55 impl<V:InferStr> InferStr for Bound<V> {
56     fn inf_str(&self, cx: &InferCtxt) -> String {
57         match *self {
58             Some(ref v) => v.inf_str(cx),
59             None => "none".to_string()
60         }
61     }
62 }
63
64 impl<T:InferStr> InferStr for Bounds<T> {
65     fn inf_str(&self, cx: &InferCtxt) -> String {
66         format_strbuf!("\\{{} <: {}\\}",
67                        self.lb.inf_str(cx),
68                        self.ub.inf_str(cx))
69     }
70 }
71
72 impl<V:Vid + ToStr,T:InferStr> InferStr for VarValue<V, T> {
73     fn inf_str(&self, cx: &InferCtxt) -> String {
74         match *self {
75           Redirect(ref vid) => format_strbuf!("Redirect({})", vid.to_str()),
76           Root(ref pt, rk) => {
77               format_strbuf!("Root({}, {})", pt.inf_str(cx), rk)
78           }
79         }
80     }
81 }
82
83 impl InferStr for IntVarValue {
84     fn inf_str(&self, _cx: &InferCtxt) -> String {
85         self.to_str().to_string()
86     }
87 }
88
89 impl InferStr for ast::FloatTy {
90     fn inf_str(&self, _cx: &InferCtxt) -> String {
91         self.to_str().to_string()
92     }
93 }
94
95 impl InferStr for ty::TraitRef {
96     fn inf_str(&self, cx: &InferCtxt) -> String {
97         trait_ref_to_str(cx.tcx, self)
98     }
99 }