blob: 517aff52f161453ef0777d6b96265e1cc6d33737 [file] [log] [blame]
David Tolnay55337722016-09-11 12:58:56 -07001use {
2 Ident,
3 Lifetime,
4 LifetimeDef,
5};
6use aster::invoke::{Invoke, Identity};
7
8//////////////////////////////////////////////////////////////////////////////
9
10pub trait IntoLifetime {
11 fn into_lifetime(self) -> Lifetime;
12}
13
14impl IntoLifetime for Lifetime {
15 fn into_lifetime(self) -> Lifetime {
16 self
17 }
18}
19
20impl<'a> IntoLifetime for &'a str {
21 fn into_lifetime(self) -> Lifetime {
22 Lifetime {
23 ident: self.into(),
24 }
25 }
26}
27
28//////////////////////////////////////////////////////////////////////////////
29
30pub trait IntoLifetimeDef {
31 fn into_lifetime_def(self) -> LifetimeDef;
32}
33
34impl IntoLifetimeDef for LifetimeDef {
35 fn into_lifetime_def(self) -> LifetimeDef {
36 self
37 }
38}
39
40impl IntoLifetimeDef for Lifetime {
41 fn into_lifetime_def(self) -> LifetimeDef {
42 LifetimeDef {
43 lifetime: self,
44 bounds: vec![],
45 }
46 }
47}
48
49impl<'a> IntoLifetimeDef for &'a str {
50 fn into_lifetime_def(self) -> LifetimeDef {
51 self.into_lifetime().into_lifetime_def()
52 }
53}
54
55impl IntoLifetimeDef for String {
56 fn into_lifetime_def(self) -> LifetimeDef {
57 (*self).into_lifetime().into_lifetime_def()
58 }
59}
60
61//////////////////////////////////////////////////////////////////////////////
62
63pub struct LifetimeDefBuilder<F=Identity> {
64 callback: F,
65 lifetime: Lifetime,
66 bounds: Vec<Lifetime>,
67}
68
69impl LifetimeDefBuilder {
70 pub fn new<N>(name: N) -> Self
71 where N: Into<Ident>,
72 {
73 LifetimeDefBuilder::with_callback(name, Identity)
74 }
75}
76
77impl<F> LifetimeDefBuilder<F>
78 where F: Invoke<LifetimeDef>,
79{
80 pub fn with_callback<N>(name: N, callback: F) -> Self
81 where N: Into<Ident>,
82 {
83 let lifetime = Lifetime {
84 ident: name.into(),
85 };
86
87 LifetimeDefBuilder {
88 callback: callback,
89 lifetime: lifetime,
90 bounds: Vec::new(),
91 }
92 }
93
94 pub fn bound<N>(mut self, name: N) -> Self
95 where N: Into<Ident>,
96 {
97 let lifetime = Lifetime {
98 ident: name.into(),
99 };
100
101 self.bounds.push(lifetime);
102 self
103 }
104
105 pub fn build(self) -> F::Result {
106 self.callback.invoke(LifetimeDef {
107 lifetime: self.lifetime,
108 bounds: self.bounds,
109 })
110 }
111}