]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/hair/cx/to_ref.rs
New upstream version 1.34.2+dfsg1
[rustc.git] / src / librustc_mir / hair / cx / to_ref.rs
1 use crate::hair::*;
2
3 use rustc::hir;
4 use syntax::ptr::P;
5
6 pub trait ToRef {
7 type Output;
8 fn to_ref(self) -> Self::Output;
9 }
10
11 impl<'a, 'tcx: 'a> ToRef for &'tcx hir::Expr {
12 type Output = ExprRef<'tcx>;
13
14 fn to_ref(self) -> ExprRef<'tcx> {
15 ExprRef::Hair(self)
16 }
17 }
18
19 impl<'a, 'tcx: 'a> ToRef for &'tcx P<hir::Expr> {
20 type Output = ExprRef<'tcx>;
21
22 fn to_ref(self) -> ExprRef<'tcx> {
23 ExprRef::Hair(&**self)
24 }
25 }
26
27 impl<'a, 'tcx: 'a> ToRef for Expr<'tcx> {
28 type Output = ExprRef<'tcx>;
29
30 fn to_ref(self) -> ExprRef<'tcx> {
31 ExprRef::Mirror(Box::new(self))
32 }
33 }
34
35 impl<'a, 'tcx: 'a, T, U> ToRef for &'tcx Option<T>
36 where &'tcx T: ToRef<Output = U>
37 {
38 type Output = Option<U>;
39
40 fn to_ref(self) -> Option<U> {
41 self.as_ref().map(|expr| expr.to_ref())
42 }
43 }
44
45 impl<'a, 'tcx: 'a, T, U> ToRef for &'tcx Vec<T>
46 where &'tcx T: ToRef<Output = U>
47 {
48 type Output = Vec<U>;
49
50 fn to_ref(self) -> Vec<U> {
51 self.iter().map(|expr| expr.to_ref()).collect()
52 }
53 }
54
55 impl<'a, 'tcx: 'a, T, U> ToRef for &'tcx P<[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 }