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