1use super::*;
18use crate::common::{SeparatedSeqAccept, Str};
19use crate::{Output, SequenceAccept, Writable, WritableSeq};
20
21impl<O, Lints> Writable<O> for AllowLints<Lints>
22where
23 O: Output,
24 Lints: WritableSeq<O>,
25{
26 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
27 let mut acceptor = SeparatedSeqAccept::new(output, Str("allow("), Str(","));
28 self.0.for_each(&mut acceptor).await?;
29 if acceptor.wrote_any {
30 output.write(")").await?;
31 }
32 Ok(())
33 }
34}
35
36impl<O> Writable<O> for ModPub
37where
38 O: Output,
39{
40 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
41 output.write("pub").await
42 }
43}
44
45impl<O, ABI> Writable<O> for ModExtern<ABI>
46where
47 O: Output,
48 ABI: Writable<O>,
49{
50 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
51 output.write("extern \"").await?;
52 self.0.write_to(output).await?;
53 output.write("\"").await
54 }
55}
56
57impl<O: Output, Expr> Writable<O> for UnsafeBlock<Expr>
58where
59 Expr: Writable<O>,
60{
61 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
62 o.write("unsafe {\n").await?;
63 self.0.write_to(o).await?;
64 o.write("\n}").await?;
65 Ok(())
66 }
67}
68
69impl<O, InputVars, Expr> Writable<O> for Closure<InputVars, Expr>
70where
71 O: Output,
72 InputVars: WritableSeq<O>,
73 Expr: Writable<O>,
74{
75 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
76 o.write("|").await?;
77 self.input_vars
78 .for_each(&mut SeparatedSeqAccept::comma_separated(o))
79 .await?;
80 o.write("| {\n").await?;
81 self.inside_block.write_to(o).await?;
82 o.write("\n}").await?;
83 Ok(())
84 }
85}
86
87impl<O: Output, Recv, FuncName, Args> Writable<O> for FunctionCall<Recv, FuncName, Args>
88where
89 Recv: Writable<O>,
90 FuncName: Writable<O>,
91 Args: WritableSeq<O>,
92{
93 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
94 self.receiver.write_to(o).await?;
95 o.write(if self.is_assoc { "::" } else { "." }).await?;
96 self.function.write_to(o).await
97 }
98}
99
100impl<O: Output, Variable, Expr> Writable<O> for LetStmt<Variable, Expr>
101where
102 Variable: Writable<O>,
103 Expr: Writable<O>,
104{
105 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
106 o.write("let ").await?;
107 self.0.write_to(o).await?;
108 o.write(" = ").await?;
109 self.1.write_to(o).await?;
110 o.write(";\n").await?;
111 Ok(())
112 }
113}
114
115impl<O: Output, Cont, Item> Writable<O> for AssociatedItem<Cont, Item>
116where
117 Cont: Writable<O>,
118 Item: Writable<O>,
119{
120 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
121 self.0.write_to(o).await?;
122 o.write("::").await?;
123 self.1.write_to(o).await
124 }
125}
126
127impl<O: Output, Expr> Writable<O> for QuestionMarkAfter<Expr>
128where
129 Expr: Writable<O>,
130{
131 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
132 self.0.write_to(o).await?;
133 o.write("?").await?;
134 Ok(())
135 }
136}
137
138impl<O: Output, Expr> Writable<O> for OkResultOf<Expr>
139where
140 Expr: Writable<O>,
141{
142 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
143 o.write("Ok(").await?;
144 self.0.write_to(o).await?;
145 o.write(")").await?;
146 Ok(())
147 }
148}
149
150impl<O: Output, Type, Trait> Writable<O> for TypeAsTrait<Type, Trait>
151where
152 Type: Writable<O>,
153 Trait: Writable<O>,
154{
155 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
156 o.write("<").await?;
157 self.0.write_to(o).await?;
158 o.write(" as ").await?;
159 self.1.write_to(o).await?;
160 o.write(">").await?;
161 Ok(())
162 }
163}
164
165impl<O, Attr, Mods, Name, Args, Return, Body> Writable<O>
166 for FunctionDef<Attr, Mods, Name, Args, Return, Body>
167where
168 O: Output,
169 Attr: WritableSeq<O>,
170 Mods: WritableSeq<O>,
171 Name: Writable<O>,
172 Args: WritableSeq<O>,
173 Return: Writable<O>,
174 Body: Writable<O>,
175{
176 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
177 self.attr.for_each(&mut AttributesAccept(o)).await?;
178 o.write("\n").await?;
179 self.mods.for_each(&mut ModsAccept(o)).await?;
180 o.write("fn ").await?;
181 self.decl.write_to(o).await?;
182 o.write(" -> ").await?;
183 self.return_type.write_to(o).await?;
184 self.body.write_to(o).await?;
185 o.write("\n").await?;
186 Ok(())
187 }
188}
189
190impl<O, Name, Args> Writable<O> for Function<Name, Args>
191where
192 O: Output,
193 Name: Writable<O>,
194 Args: WritableSeq<O>,
195{
196 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
197 self.name.write_to(output).await?;
198 output.write("(").await?;
199 self.args
200 .for_each(&mut SeparatedSeqAccept::comma_separated(output))
201 .await?;
202 output.write(")").await
203 }
204}
205
206impl<O: Output> Writable<O> for FunctionBodyDeclare {
207 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
208 output.write(";").await
209 }
210}
211
212impl<O: Output, Type, Value> Writable<O> for AssociatedTypeEquals<Type, Value>
213where
214 Type: Writable<O>,
215 Value: Writable<O>,
216{
217 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
218 self.0.write_to(o).await?;
219 o.write("=").await?;
220 self.1.write_to(o).await
221 }
222}
223
224impl<O: Output, Type> Writable<O> for DynOf<Type>
225where
226 Type: Writable<O>,
227{
228 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
229 o.write("dyn ").await?;
230 self.0.write_to(o).await
231 }
232}
233
234impl<O: Output, Type> Writable<O> for RefOf<Type>
235where
236 Type: Writable<O>,
237{
238 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
239 o.write("&").await?;
240 self.0.write_to(o).await
241 }
242}
243
244
245impl<O: Output, Type> Writable<O> for ImplOf<Type>
246where
247 Type: Writable<O>,
248{
249 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
250 o.write("impl ").await?;
251 self.0.write_to(o).await
252 }
253}
254
255impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
256where
257 Type: Writable<O>,
258{
259 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
260 o.write("&'").await?;
261 o.write(self.0).await?;
262 o.write(" ").await?;
263 self.1.write_to(o).await
264 }
265}
266
267impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
268where
269 VarName: Writable<O>,
270 Value: Writable<O>,
271{
272 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
273 o.write("\ntype ").await?;
274 self.0.write_to(o).await?;
275 o.write(" = ").await?;
276 self.1.write_to(o).await?;
277 o.write(";\n").await?;
278 Ok(())
279 }
280}
281
282impl<O, Attr, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
283 for TraitDef<Attr, Mods, Name, TypeVars, SuperTraits, Body>
284where
285 O: Output,
286 Attr: WritableSeq<O>,
287 Mods: WritableSeq<O>,
288 Name: Writable<O>,
289 TypeVars: WritableSeq<O>,
290 SuperTraits: WritableSeq<O>,
291 Body: Writable<O>,
292{
293 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
294 self.attr.for_each(&mut AttributesAccept(o)).await?;
295 o.write("\n").await?;
296 self.mods.for_each(&mut ModsAccept(o)).await?;
297 o.write("trait ").await?;
298 self.name.write_to(o).await?;
299 TypeArgAccept::new(o)
300 .write_inside_diamond(&self.type_variables)
301 .await?;
302 self.super_traits
303 .for_each(&mut type_arg_bound_accept(o))
304 .await?;
305 o.write(" {").await?;
306 self.body.write_to(o).await?;
307 o.write("}").await?;
308 Ok(())
309 }
310}
311
312impl<O, TypeVars, Trait, Recv, Body> Writable<O> for TraitImpl<TypeVars, Trait, Recv, Body>
313where
314 O: Output,
315 TypeVars: WritableSeq<O>,
316 Trait: Writable<O>,
317 Recv: Writable<O>,
318 Body: Writable<O>,
319{
320 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
321 o.write("impl").await?;
322 TypeArgAccept::new(o)
323 .write_inside_diamond(&self.type_variables)
324 .await?;
325 o.write(" ").await?;
326 self.the_trait.write_to(o).await?;
327 o.write(" for ").await?;
328 self.receiver.write_to(o).await?;
329 o.write(" {").await?;
330 self.body.write_to(o).await?;
331 o.write("}").await?;
332 Ok(())
333 }
334}
335
336impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
337where
338 O: Output,
339 Name: Writable<O>,
340 TypeArgs: WritableSeq<O>,
341{
342 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
343 self.name.write_to(o).await?;
344 TypeArgAccept::new(o)
345 .write_inside_diamond(&self.type_args)
346 .await
347 }
348}
349
350impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
351where
352 O: Output,
353 TypeVar: Writable<O>,
354 Bounds: WritableSeq<O>,
355{
356 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
357 self.0.write_to(output).await?;
358 self.1.for_each(&mut type_arg_bound_accept(output)).await
359 }
360}
361
362impl<'o, O: Output> SequenceAccept<O> for AttributesAccept<'o, O> {
367 async fn accept<W: Writable<O> + ?Sized>(&mut self, writable: &W) -> Result<(), O::Error> {
368 self.0.write("#[").await?;
369 writable.write_to(&mut self.0).await?;
370 self.0.write("]\n").await?;
371 Ok(())
372 }
373}
374
375struct ModsAccept<'o, O>(&'o mut O);
376
377impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
378 async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
379 where
380 W: Writable<O>,
381 {
382 writable.write_to(&mut self.0).await?;
383 self.0.write(" ").await
384 }
385}
386
387struct TypeArgAccept<'o, O> {
388 inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
389}
390
391impl<'o, O> TypeArgAccept<'o, O> {
392 fn new(output: &'o mut O) -> Self {
393 Self {
394 inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
395 }
396 }
397}
398
399impl<'o, O> TypeArgAccept<'o, O>
400where
401 O: Output,
402{
403 async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
404 where
405 TypeArgs: WritableSeq<O>,
406 {
407 type_args.for_each(&mut self.inner).await?;
408 if self.inner.wrote_any {
409 self.inner.output.write(">").await?;
410 }
411 Ok(())
412 }
413}
414
415fn type_arg_bound_accept<'o, O>(
416 output: &'o mut O,
417) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
418 SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
419}