]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/hair/cx/to_ref.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_mir / hair / cx / to_ref.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 hair::*;
12
13 use rustc::hir;
14 use syntax::ptr::P;
15
16 pub trait ToRef {
17 type Output;
18 fn to_ref(self) -> Self::Output;
19 }
20
21 impl<'a,'tcx:'a> ToRef for &'tcx hir::Expr {
22 type Output = ExprRef<'tcx>;
23
24 fn to_ref(self) -> ExprRef<'tcx> {
25 ExprRef::Hair(self)
26 }
27 }
28
29 impl<'a,'tcx:'a> ToRef for &'tcx P<hir::Expr> {
30 type Output = ExprRef<'tcx>;
31
32 fn to_ref(self) -> ExprRef<'tcx> {
33 ExprRef::Hair(&**self)
34 }
35 }
36
37 impl<'a,'tcx:'a> ToRef for Expr<'tcx> {
38 type Output = ExprRef<'tcx>;
39
40 fn to_ref(self) -> ExprRef<'tcx> {
41 ExprRef::Mirror(Box::new(self))
42 }
43 }
44
45 impl<'a,'tcx:'a,T,U> ToRef for &'tcx Option<T>
46 where &'tcx T: ToRef<Output=U>
47 {
48 type Output = Option<U>;
49
50 fn to_ref(self) -> Option<U> {
51 self.as_ref().map(|expr| expr.to_ref())
52 }
53 }
54
55 impl<'a,'tcx:'a,T,U> ToRef for &'tcx Vec<T>
56 where &'tcx T: ToRef<Output=U>
57 {
58 type Output = Vec<U>;
59
60 fn to_ref(self) -> Vec<U> {
61 self.iter().map(|expr| expr.to_ref()).collect()
62 }
63 }
64
65 impl<'a,'tcx:'a,T,U> ToRef for &'tcx P<[T]>
66 where &'tcx T: ToRef<Output=U>
67 {
68 type Output = Vec<U>;
69
70 fn to_ref(self) -> Vec<U> {
71 self.iter().map(|expr| expr.to_ref()).collect()
72 }
73 }