1 use {Ident
, Lifetime
, LifetimeDef
, Ty
, TyParamBound
, WhereBoundPredicate
, WherePredicate
,
3 use aster
::invoke
::{Invoke, Identity}
;
4 use aster
::lifetime
::{IntoLifetime, IntoLifetimeDef, LifetimeDefBuilder}
;
5 use aster
::path
::IntoPath
;
6 use aster
::ty
::TyBuilder
;
7 use aster
::ty_param
::{TyParamBoundBuilder, PolyTraitRefBuilder, TraitTyParamBoundBuilder}
;
9 // ////////////////////////////////////////////////////////////////////////////
11 pub struct WherePredicateBuilder
<F
= Identity
> {
15 impl WherePredicateBuilder
{
16 pub fn new() -> Self {
17 WherePredicateBuilder
::with_callback(Identity
)
21 impl<F
> WherePredicateBuilder
<F
>
22 where F
: Invoke
<WherePredicate
>
24 pub fn with_callback(callback
: F
) -> Self {
25 WherePredicateBuilder { callback: callback }
28 pub fn bound(self) -> TyBuilder
<Self> {
29 TyBuilder
::with_callback(self)
32 pub fn lifetime
<L
>(self, lifetime
: L
) -> WhereRegionPredicateBuilder
<F
>
35 WhereRegionPredicateBuilder
{
36 callback
: self.callback
,
37 lifetime
: lifetime
.into_lifetime(),
43 impl<F
> Invoke
<Ty
> for WherePredicateBuilder
<F
>
44 where F
: Invoke
<WherePredicate
>
46 type Result
= WhereBoundPredicateTyBuilder
<F
>;
48 fn invoke(self, ty
: Ty
) -> Self::Result
{
49 WhereBoundPredicateTyBuilder
{
50 callback
: self.callback
,
52 bound_lifetimes
: Vec
::new(),
57 // ////////////////////////////////////////////////////////////////////////////
59 pub struct WhereBoundPredicateBuilder
<F
> {
63 impl<F
> Invoke
<Ty
> for WhereBoundPredicateBuilder
<F
>
64 where F
: Invoke
<WherePredicate
>
66 type Result
= WhereBoundPredicateTyBuilder
<F
>;
68 fn invoke(self, ty
: Ty
) -> Self::Result
{
69 WhereBoundPredicateTyBuilder
{
70 callback
: self.callback
,
72 bound_lifetimes
: Vec
::new(),
77 // ////////////////////////////////////////////////////////////////////////////
79 pub struct WhereBoundPredicateTyBuilder
<F
> {
82 bound_lifetimes
: Vec
<LifetimeDef
>,
85 impl<F
> WhereBoundPredicateTyBuilder
<F
>
86 where F
: Invoke
<WherePredicate
>
88 pub fn with_for_lifetime
<L
>(mut self, lifetime
: L
) -> Self
89 where L
: IntoLifetimeDef
91 self.bound_lifetimes
.push(lifetime
.into_lifetime_def());
95 pub fn for_lifetime
<N
>(self, name
: N
) -> LifetimeDefBuilder
<Self>
98 LifetimeDefBuilder
::with_callback(name
, self)
101 pub fn with_bound(self, bound
: TyParamBound
) -> WhereBoundPredicateTyBoundsBuilder
<F
> {
102 WhereBoundPredicateTyBoundsBuilder
{
103 callback
: self.callback
,
105 bound_lifetimes
: self.bound_lifetimes
,
110 pub fn bound(self) -> TyParamBoundBuilder
<WhereBoundPredicateTyBoundsBuilder
<F
>> {
111 let builder
= WhereBoundPredicateTyBoundsBuilder
{
112 callback
: self.callback
,
114 bound_lifetimes
: self.bound_lifetimes
,
117 TyParamBoundBuilder
::with_callback(builder
)
123 -> PolyTraitRefBuilder
<TraitTyParamBoundBuilder
<WhereBoundPredicateTyBoundsBuilder
<F
>>>
126 self.bound().trait_(path
)
129 pub fn lifetime
<L
>(self, lifetime
: L
) -> WhereBoundPredicateTyBoundsBuilder
<F
>
130 where L
: IntoLifetime
132 self.bound().lifetime(lifetime
)
136 impl<F
> Invoke
<LifetimeDef
> for WhereBoundPredicateTyBuilder
<F
>
137 where F
: Invoke
<WherePredicate
>
141 fn invoke(self, lifetime
: LifetimeDef
) -> Self {
142 self.with_for_lifetime(lifetime
)
146 impl<F
> Invoke
<TyParamBound
> for WhereBoundPredicateTyBuilder
<F
>
147 where F
: Invoke
<WherePredicate
>
149 type Result
= WhereBoundPredicateTyBoundsBuilder
<F
>;
151 fn invoke(self, bound
: TyParamBound
) -> Self::Result
{
152 self.with_bound(bound
)
156 // ////////////////////////////////////////////////////////////////////////////
158 pub struct WhereBoundPredicateTyBoundsBuilder
<F
> {
161 bound_lifetimes
: Vec
<LifetimeDef
>,
162 bounds
: Vec
<TyParamBound
>,
165 impl<F
> WhereBoundPredicateTyBoundsBuilder
<F
>
166 where F
: Invoke
<WherePredicate
>
168 pub fn with_for_lifetime
<L
>(mut self, lifetime
: L
) -> Self
169 where L
: IntoLifetimeDef
171 self.bound_lifetimes
.push(lifetime
.into_lifetime_def());
175 pub fn for_lifetime
<N
>(self, name
: N
) -> LifetimeDefBuilder
<Self>
178 LifetimeDefBuilder
::with_callback(name
, self)
181 pub fn with_bound(mut self, bound
: TyParamBound
) -> Self {
182 self.bounds
.push(bound
);
186 pub fn bound(self) -> TyParamBoundBuilder
<Self> {
187 TyParamBoundBuilder
::with_callback(self)
190 pub fn trait_
<P
>(self, path
: P
) -> PolyTraitRefBuilder
<TraitTyParamBoundBuilder
<Self>>
193 self.bound().trait_(path
)
196 pub fn lifetime
<L
>(self, lifetime
: L
) -> Self
197 where L
: IntoLifetime
199 self.bound().lifetime(lifetime
)
202 pub fn build(self) -> F
::Result
{
203 let predicate
= WhereBoundPredicate
{
204 bound_lifetimes
: self.bound_lifetimes
,
209 self.callback
.invoke(WherePredicate
::BoundPredicate(predicate
))
213 impl<F
> Invoke
<LifetimeDef
> for WhereBoundPredicateTyBoundsBuilder
<F
>
214 where F
: Invoke
<WherePredicate
>
218 fn invoke(self, lifetime
: LifetimeDef
) -> Self {
219 self.with_for_lifetime(lifetime
)
223 impl<F
> Invoke
<TyParamBound
> for WhereBoundPredicateTyBoundsBuilder
<F
>
224 where F
: Invoke
<WherePredicate
>
228 fn invoke(self, bound
: TyParamBound
) -> Self {
229 self.with_bound(bound
)
233 // ////////////////////////////////////////////////////////////////////////////
235 pub struct WhereRegionPredicateBuilder
<F
> {
238 bounds
: Vec
<Lifetime
>,
241 impl<F
> WhereRegionPredicateBuilder
<F
>
242 where F
: Invoke
<WherePredicate
>
244 pub fn bound
<L
>(mut self, lifetime
: L
) -> Self
245 where L
: IntoLifetime
247 self.bounds
.push(lifetime
.into_lifetime());
251 pub fn build(self) -> F
::Result
{
252 let predicate
= WhereRegionPredicate
{
253 lifetime
: self.lifetime
,
257 self.callback
.invoke(WherePredicate
::RegionPredicate(predicate
))