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