1 use {Ident, LifetimeDef, Path, PolyTraitRef, TraitBoundModifier, Ty, TyParam, TyParamBound}
;
2 use aster
::invoke
::{Invoke, Identity}
;
3 use aster
::lifetime
::{IntoLifetime, IntoLifetimeDef, LifetimeDefBuilder}
;
4 use aster
::path
::{IntoPath, PathBuilder}
;
5 use aster
::ty
::TyBuilder
;
7 // ////////////////////////////////////////////////////////////////////////////
9 pub struct TyParamBuilder
<F
= Identity
> {
12 bounds
: Vec
<TyParamBound
>,
17 pub fn new
<I
>(id
: I
) -> Self
20 TyParamBuilder
::with_callback(id
, Identity
)
23 pub fn from_ty_param(ty_param
: TyParam
) -> Self {
24 TyParamBuilder
::from_ty_param_with_callback(Identity
, ty_param
)
28 impl<F
> TyParamBuilder
<F
>
29 where F
: Invoke
<TyParam
>
31 pub fn with_callback
<I
>(id
: I
, callback
: F
) -> Self
42 pub fn from_ty_param_with_callback(callback
: F
, ty_param
: TyParam
) -> Self {
46 bounds
: ty_param
.bounds
,
47 default: ty_param
.default,
51 pub fn with_default(mut self, ty
: Ty
) -> Self {
52 self.default = Some(ty
);
56 pub fn default(self) -> TyBuilder
<Self> {
57 TyBuilder
::with_callback(self)
60 pub fn with_bound(mut self, bound
: TyParamBound
) -> Self {
61 self.bounds
.push(bound
);
65 pub fn bound(self) -> TyParamBoundBuilder
<Self> {
66 TyParamBoundBuilder
::with_callback(self)
69 pub fn with_trait_bound(self, trait_ref
: PolyTraitRef
) -> Self {
70 self.bound().build_trait(trait_ref
, TraitBoundModifier
::None
)
73 pub fn trait_bound
<P
>(self, path
: P
) -> PolyTraitRefBuilder
<Self>
76 PolyTraitRefBuilder
::with_callback(path
, self)
79 pub fn lifetime_bound
<L
>(mut self, lifetime
: L
) -> Self
82 let lifetime
= lifetime
.into_lifetime();
84 self.bounds
.push(TyParamBound
::Region(lifetime
));
88 pub fn build(self) -> F
::Result
{
89 self.callback
.invoke(TyParam
{
93 default: self.default,
98 impl<F
> Invoke
<Ty
> for TyParamBuilder
<F
>
99 where F
: Invoke
<TyParam
>
103 fn invoke(self, ty
: Ty
) -> Self {
104 self.with_default(ty
)
108 impl<F
> Invoke
<TyParamBound
> for TyParamBuilder
<F
>
109 where F
: Invoke
<TyParam
>
113 fn invoke(self, bound
: TyParamBound
) -> Self {
114 self.with_bound(bound
)
118 impl<F
> Invoke
<PolyTraitRef
> for TyParamBuilder
<F
>
119 where F
: Invoke
<TyParam
>
123 fn invoke(self, trait_ref
: PolyTraitRef
) -> Self {
124 self.with_trait_bound(trait_ref
)
128 // ////////////////////////////////////////////////////////////////////////////
130 pub struct TyParamBoundBuilder
<F
= Identity
> {
134 impl TyParamBoundBuilder
{
135 pub fn new() -> Self {
136 TyParamBoundBuilder
::with_callback(Identity
)
140 impl<F
> TyParamBoundBuilder
<F
>
141 where F
: Invoke
<TyParamBound
>
143 pub fn with_callback(callback
: F
) -> Self {
144 TyParamBoundBuilder { callback: callback }
147 pub fn build_trait(self, poly_trait
: PolyTraitRef
, modifier
: TraitBoundModifier
) -> F
::Result
{
148 let bound
= TyParamBound
::Trait(poly_trait
, modifier
);
149 self.callback
.invoke(bound
)
152 pub fn trait_
<P
>(self, path
: P
) -> PolyTraitRefBuilder
<TraitTyParamBoundBuilder
<F
>>
155 let builder
= TraitTyParamBoundBuilder
{
157 modifier
: TraitBoundModifier
::None
,
160 PolyTraitRefBuilder
::with_callback(path
, builder
)
163 pub fn maybe_trait
<P
>(self, path
: P
) -> PolyTraitRefBuilder
<TraitTyParamBoundBuilder
<F
>>
166 let builder
= TraitTyParamBoundBuilder
{
168 modifier
: TraitBoundModifier
::Maybe
,
171 PolyTraitRefBuilder
::with_callback(path
, builder
)
174 pub fn iterator(self, ty
: Ty
) -> PolyTraitRefBuilder
<TraitTyParamBoundBuilder
<F
>> {
175 let path
= PathBuilder
::new()
187 pub fn lifetime
<L
>(self, lifetime
: L
) -> F
::Result
188 where L
: IntoLifetime
190 let lifetime
= lifetime
.into_lifetime();
191 self.callback
.invoke(TyParamBound
::Region(lifetime
))
195 // ////////////////////////////////////////////////////////////////////////////
197 pub struct TraitTyParamBoundBuilder
<F
> {
198 builder
: TyParamBoundBuilder
<F
>,
199 modifier
: TraitBoundModifier
,
202 impl<F
> Invoke
<PolyTraitRef
> for TraitTyParamBoundBuilder
<F
>
203 where F
: Invoke
<TyParamBound
>
205 type Result
= F
::Result
;
207 fn invoke(self, poly_trait
: PolyTraitRef
) -> Self::Result
{
208 self.builder
.build_trait(poly_trait
, self.modifier
)
212 // ////////////////////////////////////////////////////////////////////////////
214 pub struct PolyTraitRefBuilder
<F
> {
217 lifetimes
: Vec
<LifetimeDef
>,
220 impl<F
> PolyTraitRefBuilder
<F
>
221 where F
: Invoke
<PolyTraitRef
>
223 pub fn with_callback
<P
>(path
: P
, callback
: F
) -> Self
226 PolyTraitRefBuilder
{
228 trait_ref
: path
.into_path(),
229 lifetimes
: Vec
::new(),
233 pub fn with_lifetime
<L
>(mut self, lifetime
: L
) -> Self
234 where L
: IntoLifetimeDef
236 self.lifetimes
.push(lifetime
.into_lifetime_def());
240 pub fn lifetime
<N
>(self, name
: N
) -> LifetimeDefBuilder
<Self>
243 LifetimeDefBuilder
::with_callback(name
, self)
246 pub fn build(self) -> F
::Result
{
247 self.callback
.invoke(PolyTraitRef
{
248 bound_lifetimes
: self.lifetimes
,
249 trait_ref
: self.trait_ref
,
254 impl<F
> Invoke
<LifetimeDef
> for PolyTraitRefBuilder
<F
>
255 where F
: Invoke
<PolyTraitRef
>
259 fn invoke(self, lifetime
: LifetimeDef
) -> Self {
260 self.with_lifetime(lifetime
)