1use super::*;
18use crate::common::{SeparatedSeqAccept, Str};
19use crate::context::ContextProvides;
20use crate::{Output, SequenceAccept, Writable, WritableSeq};
21
22impl<O, Type> Writable<O> for UseType<Type>
23where
24 O: Output,
25 Type: Writable<O>,
26{
27 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
28 output.write("use ").await?;
29 self.0.write_to(output).await?;
30 output.write(";\n").await
31 }
32}
33
34impl<O, Cond, Attr> Writable<O> for CfgAttr<Cond, Attr>
35where
36 O: Output,
37 Cond: Writable<O>,
38 Attr: Writable<O>,
39{
40 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
41 output.write("cfg_attr(").await?;
42 self.0.write_to(output).await?;
43 output.write(", ").await?;
44 self.1.write_to(output).await?;
45 output.write(")").await
46 }
47}
48
49impl<O, Cond> Writable<O> for Cfg<Cond>
50where
51 O: Output,
52 Cond: Writable<O>,
53{
54 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
55 output.write("cfg(").await?;
56 self.0.write_to(output).await?;
57 output.write(")").await
58 }
59}
60
61impl<O, Value> Writable<O> for Target<Value>
62where
63 O: Output,
64 Value: Writable<O>,
65{
66 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
67 let (kind, value) = match self {
68 Self::Os(value) => ("os", value),
69 Self::Family(value) => ("family", value),
70 Self::Arch(value) => ("arch", value),
71 };
72 output.write("target_").await?;
73 output.write(kind).await?;
74 output.write(" = \"").await?;
75 value.write_to(output).await?;
76 output.write("\"").await
77 }
78}
79
80impl<O, Arg> Writable<O> for Link<Arg>
81where
82 O: Output,
83 Arg: Writable<O>,
84{
85 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
86 output.write("link(").await?;
87 self.0.write_to(output).await?;
88 output.write(")").await
89 }
90}
91
92impl<O> Writable<O> for NoMangle
93where
94 O: Output,
95 O::Ctx: ContextProvides<Edition>,
96{
97 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
98 let edition = output.get_ctx();
99 output
100 .write(if edition >= &Edition::Rust2024 {
101 "unsafe(no_mangle)"
102 } else {
103 "no_mangle"
104 })
105 .await
106 }
107}
108
109impl<O, Lints> Writable<O> for AllowLints<Lints>
110where
111 O: Output,
112 Lints: WritableSeq<O>,
113{
114 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
115 let mut acceptor = SeparatedSeqAccept::new(output, Str("allow("), Str(","));
116 self.0.for_each(&mut acceptor).await?;
117 if acceptor.wrote_any {
118 output.write(")").await?;
119 }
120 Ok(())
121 }
122}
123
124impl<O, Msg, Since> Writable<O> for Deprecated<Msg, Since>
125where
126 O: Output,
127 Msg: Writable<O>,
128 Since: Writable<O>,
129{
130 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
131 match self {
132 Self::Basic => output.write("deprecated").await,
133 Self::Message(msg) => {
134 output.write("deprecated = \"").await?;
135 msg.write_to(output).await?;
136 output.write("\"").await
137 }
138 Self::Full { since, note } => {
139 output.write("deprecated(since = \"").await?;
140 since.write_to(output).await?;
141 output.write("\", note = \"").await?;
142 note.write_to(output).await?;
143 output.write("\")").await
144 }
145 }
146 }
147}
148
149impl<O> Writable<O> for MustUse
150where
151 O: Output,
152{
153 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
154 output.write("must_use").await
155 }
156}
157
158impl<O> Writable<O> for ModPub
159where
160 O: Output,
161{
162 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
163 output.write("pub").await
164 }
165}
166
167impl<O, Abi> Writable<O> for ModUnsafeExtern<Abi>
168where
169 O: Output,
170 Abi: Writable<O>,
171{
172 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
173 output.write("unsafe extern \"").await?;
174 self.0.write_to(output).await?;
175 output.write("\"").await
176 }
177}
178
179impl<O, Abi, Body> Writable<O> for ExternBlock<Abi, Body>
180where
181 O: Output,
182 O::Ctx: ContextProvides<Edition>,
183 Abi: Writable<O>,
184 Body: Writable<O>,
185{
186 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
187 let edition = output.get_ctx();
188 if edition >= &Edition::Rust2024 {
189 output.write("unsafe ").await?;
190 }
191 output.write("extern \"").await?;
192 self.abi.write_to(output).await?;
193 output.write("\" {").await?;
194 self.body.write_to(output).await?;
195 output.write("}").await
196 }
197}
198
199impl<O, Mod, Body> Writable<O> for ModBlock<Mod, Body>
200where
201 O: Output,
202 Mod: Writable<O>,
203 Body: Writable<O>,
204{
205 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
206 output.write("mod ").await?;
207 self.modname.write_to(output).await?;
208 output.write(" {").await?;
209 self.body.write_to(output).await?;
210 output.write("}\n").await
211 }
212}
213
214impl<O: Output, Expr> Writable<O> for UnsafeBlock<Expr>
215where
216 Expr: Writable<O>,
217{
218 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
219 o.write("unsafe {\n").await?;
220 self.0.write_to(o).await?;
221 o.write("\n}").await?;
222 Ok(())
223 }
224}
225
226impl<O, InputVars, Expr> Writable<O> for Closure<InputVars, Expr>
227where
228 O: Output,
229 InputVars: WritableSeq<O>,
230 Expr: Writable<O>,
231{
232 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
233 o.write("|").await?;
234 self.input_vars
235 .for_each(&mut SeparatedSeqAccept::comma_separated(o))
236 .await?;
237 o.write("| {\n").await?;
238 self.inside_block.write_to(o).await?;
239 o.write("\n}").await?;
240 Ok(())
241 }
242}
243
244impl<O: Output, Recv, FuncName, Args> Writable<O> for FunctionCall<Recv, FuncName, Args>
245where
246 Recv: Writable<O>,
247 FuncName: Writable<O>,
248 Args: WritableSeq<O>,
249{
250 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
251 self.receiver.write_to(o).await?;
252 o.write(if self.is_assoc { "::" } else { "." }).await?;
253 self.name.write_to(o).await?;
254 AnonTuple(&self.args).write_to(o).await
255 }
256}
257
258impl<O: Output, Name, Args> Writable<O> for Turbofish<Name, Args>
259where
260 Name: Writable<O>,
261 Args: WritableSeq<O>,
262{
263 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
264 self.0.write_to(output).await?;
265 let mut acceptor = SeparatedSeqAccept::new(output, Str("::<"), Str(", "));
266 self.1.for_each(&mut acceptor).await?;
267 if acceptor.wrote_any {
268 output.write(">").await?;
269 }
270 Ok(())
271 }
272}
273
274impl<O: Output, Variable, Expr> Writable<O> for LetStmt<Variable, Expr>
275where
276 Variable: Writable<O>,
277 Expr: Writable<O>,
278{
279 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
280 o.write("let ").await?;
281 AssignStmt(&self.0, &self.1).write_to(o).await
282 }
283}
284
285impl<O: Output, Variable, Expr> Writable<O> for AssignStmt<Variable, Expr>
286where
287 Variable: Writable<O>,
288 Expr: Writable<O>,
289{
290 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
291 self.0.write_to(o).await?;
292 o.write(" = ").await?;
293 self.1.write_to(o).await?;
294 o.write(";\n").await?;
295 Ok(())
296 }
297}
298
299impl<O: Output, Elements> Writable<O> for ArrayFromElements<Elements>
300where
301 Elements: WritableSeq<O>,
302{
303 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
304 o.write("[").await?;
305 self.0
306 .for_each(&mut SeparatedSeqAccept::comma_separated(o))
307 .await?;
308 o.write("]").await
309 }
310}
311
312impl<O: Output, Cont, Item> Writable<O> for AssociatedItem<Cont, Item>
313where
314 Cont: Writable<O>,
315 Item: Writable<O>,
316{
317 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
318 self.0.write_to(o).await?;
319 o.write("::").await?;
320 self.1.write_to(o).await
321 }
322}
323
324impl<O: Output, Expr> Writable<O> for QuestionMarkAfter<Expr>
325where
326 Expr: Writable<O>,
327{
328 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
329 self.0.write_to(o).await?;
330 o.write("?").await?;
331 Ok(())
332 }
333}
334
335impl<O: Output, Expr> Writable<O> for OkResultOf<Expr>
336where
337 Expr: Writable<O>,
338{
339 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
340 o.write("Ok(").await?;
341 self.0.write_to(o).await?;
342 o.write(")").await?;
343 Ok(())
344 }
345}
346
347impl<O: Output, Type, Trait> Writable<O> for TypeAsTrait<Type, Trait>
348where
349 Type: Writable<O>,
350 Trait: Writable<O>,
351{
352 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
353 o.write("<").await?;
354 self.0.write_to(o).await?;
355 o.write(" as ").await?;
356 self.1.write_to(o).await?;
357 o.write(">").await?;
358 Ok(())
359 }
360}
361
362impl<O, Mods, Name, Args, Return, Where, Body> Writable<O>
363 for FunctionDef<Mods, Name, Args, Return, Where, Body>
364where
365 O: Output,
366 Mods: WritableSeq<O>,
367 Name: Writable<O>,
368 Args: WritableSeq<O>,
369 Return: Writable<O>,
370 Where: WritableSeq<O>,
371 Body: Writable<O>,
372{
373 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
374 self.mods.for_each(&mut ModsAccept(o)).await?;
375 o.write("fn ").await?;
376 self.name.write_to(o).await?;
377 AnonTuple(&self.args).write_to(o).await?;
378 o.write(" -> ").await?;
379 self.return_type.write_to(o).await?;
380 self.where_conds.for_each(&mut where_cond_accept(o)).await?;
381 self.body.write_to(o).await?;
382 o.write("\n").await?;
383 Ok(())
384 }
385}
386
387impl<O: Output> Writable<O> for FunctionBodyDeclare {
388 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
389 output.write(";").await
390 }
391}
392
393impl<O: Output, Inner> Writable<O> for FunctionBodyImplement<Inner>
394where
395 Inner: Writable<O>,
396{
397 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
398 output.write("{\n").await?;
399 self.0.write_to(output).await?;
400 output.write("\n}").await
401 }
402}
403
404impl<O, Args, Return> Writable<O> for FunctionPtr<Args, Return>
405where
406 O: Output,
407 Args: WritableSeq<O>,
408 Return: Writable<O>,
409{
410 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
411 let kind = match self.kind {
412 FunctionPtrKind::FnPtr => "fn",
413 FunctionPtrKind::Fn => "Fn",
414 FunctionPtrKind::FnMut => "FnMut",
415 FunctionPtrKind::FnOnce => "FnOnce",
416 };
417 output.write(kind).await?;
418 AnonTuple(&self.args).write_to(output).await?;
419 output.write(" -> ").await?;
420 self.return_type.write_to(output).await
421 }
422}
423
424impl<O: Output, Type, Value> Writable<O> for AssociatedTypeEquals<Type, Value>
425where
426 Type: Writable<O>,
427 Value: Writable<O>,
428{
429 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
430 self.0.write_to(o).await?;
431 o.write("=").await?;
432 self.1.write_to(o).await
433 }
434}
435
436impl<O: Output, Type> Writable<O> for DynOf<Type>
437where
438 Type: Writable<O>,
439{
440 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
441 o.write("dyn ").await?;
442 self.0.write_to(o).await
443 }
444}
445
446impl<O: Output, Type> Writable<O> for RefOf<Type>
447where
448 Type: Writable<O>,
449{
450 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
451 o.write("&").await?;
452 self.0.write_to(o).await
453 }
454}
455
456impl<O: Output, Type> Writable<O> for ImplOf<Type>
457where
458 Type: Writable<O>,
459{
460 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
461 o.write("impl ").await?;
462 self.0.write_to(o).await
463 }
464}
465
466impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
467where
468 Type: Writable<O>,
469{
470 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
471 o.write("&'").await?;
472 o.write(self.0).await?;
473 o.write(" ").await?;
474 self.1.write_to(o).await
475 }
476}
477
478impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
479where
480 VarName: Writable<O>,
481 Value: Writable<O>,
482{
483 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
484 o.write("\ntype ").await?;
485 self.0.write_to(o).await?;
486 o.write(" = ").await?;
487 self.1.write_to(o).await?;
488 o.write(";\n").await?;
489 Ok(())
490 }
491}
492
493impl<O, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
494 for TraitDef<Mods, Name, TypeVars, SuperTraits, Body>
495where
496 O: Output,
497 Mods: WritableSeq<O>,
498 Name: Writable<O>,
499 TypeVars: WritableSeq<O>,
500 SuperTraits: WritableSeq<O>,
501 Body: Writable<O>,
502{
503 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
504 self.mods.for_each(&mut ModsAccept(o)).await?;
505 o.write("trait ").await?;
506 self.name.write_to(o).await?;
507 TypeArgAccept::new(o)
508 .write_inside_diamond(&self.type_variables)
509 .await?;
510 self.super_traits
511 .for_each(&mut type_arg_bound_accept(o))
512 .await?;
513 o.write(" {").await?;
514 self.body.write_to(o).await?;
515 o.write("}").await?;
516 Ok(())
517 }
518}
519
520impl<O, TypeVars, Trait, Recv, Where, Body> Writable<O>
521 for TraitImpl<TypeVars, Trait, Recv, Where, Body>
522where
523 O: Output,
524 TypeVars: WritableSeq<O>,
525 Trait: Writable<O>,
526 Recv: Writable<O>,
527 Where: WritableSeq<O>,
528 Body: Writable<O>,
529{
530 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
531 o.write("impl").await?;
532 TypeArgAccept::new(o)
533 .write_inside_diamond(&self.type_variables)
534 .await?;
535 o.write(" ").await?;
536 self.the_trait.write_to(o).await?;
537 o.write(" for ").await?;
538 self.receiver.write_to(o).await?;
539 self.where_conds.for_each(&mut where_cond_accept(o)).await?;
540 o.write(" {").await?;
541 self.body.write_to(o).await?;
542 o.write("}").await?;
543 Ok(())
544 }
545}
546
547impl<O, Mods, Name, Elements> Writable<O> for StructDef<Mods, Name, Elements>
548where
549 O: Output,
550 Mods: WritableSeq<O>,
551 Name: Writable<O>,
552 Elements: WritableSeq<O>,
553{
554 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
555 self.mods.for_each(&mut ModsAccept(o)).await?;
556 o.write("struct ").await?;
557 match &self.kind {
558 StructKind::Tuple(name, elements) => {
559 NamedTuple {
560 name,
561 args: elements,
562 }
563 .write_to(o)
564 .await?;
565 o.write(";").await
566 }
567 StructKind::NamedFields(name, elements) => {
568 StructCall {
569 name,
570 body: StructFields(elements),
571 }
572 .write_to(o)
573 .await
574 }
575 }
576 }
577}
578
579impl<O, Name, Body> Writable<O> for StructCall<Name, Body>
580where
581 O: Output,
582 Name: Writable<O>,
583 Body: Writable<O>,
584{
585 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
586 self.name.write_to(o).await?;
587 o.write(" { ").await?;
588 self.body.write_to(o).await?;
589 o.write(" }").await
590 }
591}
592
593impl<O, Fields> Writable<O> for StructFields<Fields>
594where
595 O: Output,
596 Fields: WritableSeq<O>,
597{
598 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
599 self.0.for_each(&mut struct_fields_accept(output)).await
600 }
601}
602
603impl<O, Name, Value> Writable<O> for DeclareField<Name, Value>
604where
605 O: Output,
606 Name: Writable<O>,
607 Value: Writable<O>,
608{
609 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
610 FunctionParam(&self.0, &self.1).write_to(o).await
611 }
612}
613
614impl<O, Name, Args> Writable<O> for NamedTuple<Name, Args>
615where
616 O: Output,
617 Name: Writable<O>,
618 Args: WritableSeq<O>,
619{
620 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
621 self.name.write_to(output).await?;
622 let mut separator = SeparatedSeqAccept::new(output, Str("("), Str(", "));
624 self.args.for_each(&mut separator).await?;
625 if separator.wrote_any {
626 output.write(")").await?;
627 }
628 Ok(())
629 }
630}
631
632impl<O, Args> Writable<O> for AnonTuple<Args>
633where
634 O: Output,
635 Args: WritableSeq<O>,
636{
637 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
638 o.write("(").await?;
639 self.0
640 .for_each(&mut SeparatedSeqAccept::comma_separated(o))
641 .await?;
642 o.write(")").await
643 }
644}
645
646impl<O, Attr, Value> Writable<O> for WithAttributes<Attr, Value>
647where
648 O: Output,
649 Attr: WritableSeq<O>,
650 Value: Writable<O>,
651{
652 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
653 self.attr
654 .for_each(&mut AttributesAccept::with_separator(
655 output,
656 self.separator,
657 ))
658 .await?;
659 self.value.write_to(output).await
660 }
661}
662
663impl<O, Mods, Name, Entries> Writable<O> for EnumDef<Mods, Name, Entries>
664where
665 O: Output,
666 Mods: WritableSeq<O>,
667 Name: Writable<O>,
668 Entries: WritableSeq<O>,
669{
670 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
671 self.mods.for_each(&mut ModsAccept(output)).await?;
672 output.write("enum ").await?;
673 self.name.write_to(output).await?;
674 output.write(" {").await?;
675 self.entries
676 .for_each(&mut SeparatedSeqAccept::comma_separated(output))
677 .await?;
678 output.write("}").await
679 }
680}
681
682impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
683where
684 O: Output,
685 Name: Writable<O>,
686 TypeArgs: WritableSeq<O>,
687{
688 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
689 self.name.write_to(o).await?;
690 TypeArgAccept::new(o)
691 .write_inside_diamond(&self.type_args)
692 .await
693 }
694}
695
696impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
697where
698 O: Output,
699 TypeVar: Writable<O>,
700 Bounds: WritableSeq<O>,
701{
702 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
703 self.0.write_to(output).await?;
704 self.1.for_each(&mut type_arg_bound_accept(output)).await
705 }
706}
707
708impl<O> Writable<O> for Lifetime<'_>
709where
710 O: Output,
711{
712 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
713 output.write("'").await?;
714 output.write(self.0).await
715 }
716}
717
718impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
719where
720 O: Output,
721 Name: Writable<O>,
722 Type: Writable<O>,
723{
724 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
725 self.0.write_to(output).await?;
726 output.write(": ").await?;
727 self.1.write_to(output).await
728 }
729}
730
731struct ModsAccept<'o, O>(&'o mut O);
736
737impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
738 async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
739 where
740 W: Writable<O>,
741 {
742 writable.write_to(&mut self.0).await?;
743 self.0.write(" ").await
744 }
745}
746
747struct TypeArgAccept<'o, O> {
748 inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
749}
750
751impl<'o, O> TypeArgAccept<'o, O> {
752 fn new(output: &'o mut O) -> Self {
753 Self {
754 inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
755 }
756 }
757}
758
759impl<'o, O> TypeArgAccept<'o, O>
760where
761 O: Output,
762{
763 async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
764 where
765 TypeArgs: WritableSeq<O>,
766 {
767 type_args.for_each(&mut self.inner).await?;
768 if self.inner.wrote_any {
769 self.inner.output.write(">").await?;
770 }
771 Ok(())
772 }
773}
774
775fn type_arg_bound_accept<'o, O>(
776 output: &'o mut O,
777) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
778 SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
779}
780
781fn struct_fields_accept<'o, O>(
782 output: &'o mut O,
783) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
784 SeparatedSeqAccept::new(output, Str("\n"), Str(",\n"))
785}
786
787fn where_cond_accept<'o, O>(
788 output: &'o mut O,
789) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
790 SeparatedSeqAccept::new(output, Str(" where "), Str(", "))
791}