blob: 5f0d8c4fe430dd1e4bf051ad0d0fd77171fe3847 [file] [log] [blame]
David Tolnaydaaf7742016-10-03 11:11:43 -07001use {Ident, Lifetime, LifetimeDef};
David Tolnay55337722016-09-11 12:58:56 -07002use aster::invoke::{Invoke, Identity};
3
David Tolnaydaaf7742016-10-03 11:11:43 -07004// ////////////////////////////////////////////////////////////////////////////
David Tolnay55337722016-09-11 12:58:56 -07005
6pub trait IntoLifetime {
7 fn into_lifetime(self) -> Lifetime;
8}
9
10impl IntoLifetime for Lifetime {
11 fn into_lifetime(self) -> Lifetime {
12 self
13 }
14}
15
16impl<'a> IntoLifetime for &'a str {
17 fn into_lifetime(self) -> Lifetime {
David Tolnaydaaf7742016-10-03 11:11:43 -070018 Lifetime { ident: self.into() }
David Tolnay55337722016-09-11 12:58:56 -070019 }
20}
21
David Tolnaydaaf7742016-10-03 11:11:43 -070022// ////////////////////////////////////////////////////////////////////////////
David Tolnay55337722016-09-11 12:58:56 -070023
24pub trait IntoLifetimeDef {
25 fn into_lifetime_def(self) -> LifetimeDef;
26}
27
28impl IntoLifetimeDef for LifetimeDef {
29 fn into_lifetime_def(self) -> LifetimeDef {
30 self
31 }
32}
33
34impl IntoLifetimeDef for Lifetime {
35 fn into_lifetime_def(self) -> LifetimeDef {
36 LifetimeDef {
37 lifetime: self,
38 bounds: vec![],
39 }
40 }
41}
42
43impl<'a> IntoLifetimeDef for &'a str {
44 fn into_lifetime_def(self) -> LifetimeDef {
45 self.into_lifetime().into_lifetime_def()
46 }
47}
48
49impl IntoLifetimeDef for String {
50 fn into_lifetime_def(self) -> LifetimeDef {
51 (*self).into_lifetime().into_lifetime_def()
52 }
53}
54
David Tolnaydaaf7742016-10-03 11:11:43 -070055// ////////////////////////////////////////////////////////////////////////////
David Tolnay55337722016-09-11 12:58:56 -070056
David Tolnaydaaf7742016-10-03 11:11:43 -070057pub struct LifetimeDefBuilder<F = Identity> {
David Tolnay55337722016-09-11 12:58:56 -070058 callback: F,
59 lifetime: Lifetime,
60 bounds: Vec<Lifetime>,
61}
62
63impl LifetimeDefBuilder {
64 pub fn new<N>(name: N) -> Self
David Tolnaydaaf7742016-10-03 11:11:43 -070065 where N: Into<Ident>
David Tolnay55337722016-09-11 12:58:56 -070066 {
67 LifetimeDefBuilder::with_callback(name, Identity)
68 }
69}
70
71impl<F> LifetimeDefBuilder<F>
David Tolnaydaaf7742016-10-03 11:11:43 -070072 where F: Invoke<LifetimeDef>
David Tolnay55337722016-09-11 12:58:56 -070073{
74 pub fn with_callback<N>(name: N, callback: F) -> Self
David Tolnaydaaf7742016-10-03 11:11:43 -070075 where N: Into<Ident>
David Tolnay55337722016-09-11 12:58:56 -070076 {
David Tolnaydaaf7742016-10-03 11:11:43 -070077 let lifetime = Lifetime { ident: name.into() };
David Tolnay55337722016-09-11 12:58:56 -070078
79 LifetimeDefBuilder {
80 callback: callback,
81 lifetime: lifetime,
82 bounds: Vec::new(),
83 }
84 }
85
86 pub fn bound<N>(mut self, name: N) -> Self
David Tolnaydaaf7742016-10-03 11:11:43 -070087 where N: Into<Ident>
David Tolnay55337722016-09-11 12:58:56 -070088 {
David Tolnaydaaf7742016-10-03 11:11:43 -070089 let lifetime = Lifetime { ident: name.into() };
David Tolnay55337722016-09-11 12:58:56 -070090
91 self.bounds.push(lifetime);
92 self
93 }
94
95 pub fn build(self) -> F::Result {
96 self.callback.invoke(LifetimeDef {
97 lifetime: self.lifetime,
98 bounds: self.bounds,
99 })
100 }
101}