1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use syntax::ast;
use syntax::codemap::{DUMMY_SP, Span, respan};
use syntax::ptr::P;

use invoke::{Invoke, Identity};
use lifetime::IntoLifetime;
use ty::TyBuilder;

//////////////////////////////////////////////////////////////////////////////

pub struct SelfBuilder<F=Identity> {
    callback: F,
    span: Span,
    mutable: ast::Mutability,
}

impl SelfBuilder {
    pub fn new() -> Self {
        SelfBuilder::with_callback(Identity)
    }
}

impl<F> SelfBuilder<F>
    where F: Invoke<ast::ExplicitSelf>,
{
    pub fn with_callback(callback: F) -> Self {
        SelfBuilder {
            callback: callback,
            span: DUMMY_SP,
            mutable: ast::Mutability::Immutable,
        }
    }

    pub fn build(self, self_: ast::SelfKind) -> F::Result {
        let self_ = respan(self.span, self_);
        self.callback.invoke(self_)
    }

    pub fn span(mut self, span: Span) -> Self {
        self.span = span;
        self
    }

    pub fn mut_(mut self) -> Self {
        self.mutable = ast::Mutability::Mutable;
        self
    }

    pub fn value(self) -> F::Result {
        let self_ = ast::SelfKind::Value(self.mutable);
        self.build(self_)
    }

    pub fn ref_(self) -> F::Result {
        let self_ = ast::SelfKind::Region(None, self.mutable);
        self.build(self_)
    }

    pub fn ref_lifetime<L>(self, lifetime: L) -> F::Result
        where L: IntoLifetime,
    {
        let self_ = ast::SelfKind::Region(Some(lifetime.into_lifetime()), self.mutable);
        self.build(self_)
    }

    pub fn ty(self) -> TyBuilder<Self> {
        TyBuilder::with_callback(self)
    }
}

impl<F> Invoke<P<ast::Ty>> for SelfBuilder<F>
    where F: Invoke<ast::ExplicitSelf>,
{
    type Result = F::Result;

    fn invoke(self, ty: P<ast::Ty>) -> F::Result {
        let explicit_self = ast::SelfKind::Explicit(ty, self.mutable);
        self.build(explicit_self)
    }
}