async_codegen/rust/
syntax.rs

1/*
2 * Copyright © 2025 Anand Beh
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use 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> Writable<O> for ModUnsafe
168where
169    O: Output,
170{
171    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
172        output.write("unsafe").await
173    }
174}
175
176impl<O, Abi> Writable<O> for ModUnsafeExtern<Abi>
177where
178    O: Output,
179    Abi: Writable<O>,
180{
181    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
182        output.write("unsafe extern \"").await?;
183        self.0.write_to(output).await?;
184        output.write("\"").await
185    }
186}
187
188impl<O, Abi, Body> Writable<O> for ExternBlock<Abi, Body>
189where
190    O: Output,
191    O::Ctx: ContextProvides<Edition>,
192    Abi: Writable<O>,
193    Body: Writable<O>,
194{
195    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
196        let edition = output.get_ctx();
197        if edition >= &Edition::Rust2024 {
198            output.write("unsafe ").await?;
199        }
200        output.write("extern \"").await?;
201        self.abi.write_to(output).await?;
202        output.write("\" {").await?;
203        self.body.write_to(output).await?;
204        output.write("}").await
205    }
206}
207
208impl<O, Name, Body> Writable<O> for ModBlock<Name, Body>
209where
210    O: Output,
211    Name: Writable<O>,
212    Body: Writable<O>,
213{
214    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
215        output.write("mod ").await?;
216        self.name.write_to(output).await?;
217        output.write(" {\n").await?;
218        self.body.write_to(output).await?;
219        output.write("}\n").await
220    }
221}
222
223impl<O, Cond, Body> Writable<O> for IfBlock<Cond, Body>
224where
225    O: Output,
226    Cond: Writable<O>,
227    Body: Writable<O>,
228{
229    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
230        output.write("if ").await?;
231        self.0.write_to(output).await?;
232        output.write(" {\n").await?;
233        self.1.write_to(output).await?;
234        output.write("\n}").await
235    }
236}
237
238impl<O, Before, After> Writable<O> for Else<Before, After>
239where
240    O: Output,
241    Before: Writable<O>,
242    After: Writable<O>,
243{
244    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
245        self.0.write_to(output).await?;
246        output.write(" else ").await?;
247        self.1.write_to(output).await
248    }
249}
250
251impl<O, Body> Writable<O> for Block<Body>
252where
253    O: Output,
254    Body: Writable<O>,
255{
256    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
257        output.write("{\n").await?;
258        self.0.write_to(output).await?;
259        output.write("\n}").await
260    }
261}
262
263impl<O: Output, Expr> Writable<O> for UnsafeBlock<Expr>
264where
265    Expr: Writable<O>,
266{
267    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
268        o.write("unsafe {\n").await?;
269        self.0.write_to(o).await?;
270        o.write("\n}").await?;
271        Ok(())
272    }
273}
274
275impl<O, InputVars, Expr> Writable<O> for Closure<InputVars, Expr>
276where
277    O: Output,
278    InputVars: WritableSeq<O>,
279    Expr: Writable<O>,
280{
281    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
282        o.write("|").await?;
283        self.input_vars
284            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
285            .await?;
286        o.write("| {\n").await?;
287        self.inside_block.write_to(o).await?;
288        o.write("\n}").await?;
289        Ok(())
290    }
291}
292
293impl<O: Output, Recv, FuncName, Args> Writable<O> for FunctionCall<Recv, FuncName, Args>
294where
295    Recv: Writable<O>,
296    FuncName: Writable<O>,
297    Args: WritableSeq<O>,
298{
299    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
300        self.receiver.write_to(o).await?;
301        o.write(if self.is_assoc { "::" } else { "." }).await?;
302        self.name.write_to(o).await?;
303        AnonTuple(&self.args).write_to(o).await
304    }
305}
306
307impl<O: Output, Name, Args> Writable<O> for Turbofish<Name, Args>
308where
309    Name: Writable<O>,
310    Args: WritableSeq<O>,
311{
312    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
313        self.0.write_to(output).await?;
314        let mut acceptor = SeparatedSeqAccept::new(output, Str("::<"), Str(", "));
315        self.1.for_each(&mut acceptor).await?;
316        if acceptor.wrote_any {
317            output.write(">").await?;
318        }
319        Ok(())
320    }
321}
322
323impl<O, Owner, Field> Writable<O> for FieldAccess<Owner, Field>
324where
325    O: Output,
326    Owner: Writable<O>,
327    Field: Writable<O>,
328{
329    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
330        self.0.write_to(output).await?;
331        output.write(".").await?;
332        self.1.write_to(output).await
333    }
334}
335
336impl<O, Expr> Writable<O> for Stmt<Expr>
337where
338    O: Output,
339    Expr: Writable<O>,
340{
341    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
342        self.0.write_to(output).await?;
343        output.write(";\n").await
344    }
345}
346
347impl<O: Output, Variable, Expr> Writable<O> for LetStmt<Variable, Expr>
348where
349    Variable: Writable<O>,
350    Expr: Writable<O>,
351{
352    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
353        Stmt(LetExpr(&self.0, &self.1)).write_to(o).await
354    }
355}
356
357impl<O: Output, Variable, Expr> Writable<O> for AssignStmt<Variable, Expr>
358where
359    Variable: Writable<O>,
360    Expr: Writable<O>,
361{
362    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
363        Stmt(AssignExpr(&self.0, &self.1)).write_to(o).await
364    }
365}
366
367impl<O, Expr> Writable<O> for ReturnStmt<Expr>
368where
369    O: Output,
370    Expr: Writable<O>,
371{
372    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
373        o.write("return ").await?;
374        self.0.write_to(o).await?;
375        o.write(";\n").await
376    }
377}
378
379impl<O, Pattern, Expr> Writable<O> for LetExpr<Pattern, Expr>
380where
381    O: Output,
382    Pattern: Writable<O>,
383    Expr: Writable<O>,
384{
385    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
386        output.write("let ").await?;
387        AssignExpr(&self.0, &self.1).write_to(output).await
388    }
389}
390
391struct AssignExpr<Variable, Expr>(Variable, Expr);
392
393impl<O, Variable, Expr> Writable<O> for AssignExpr<Variable, Expr>
394where
395    O: Output,
396    Variable: Writable<O>,
397    Expr: Writable<O>,
398{
399    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
400        self.0.write_to(o).await?;
401        o.write(" = ").await?;
402        self.1.write_to(o).await
403    }
404}
405
406impl<O, Content> Writable<O> for RawStringLiteral<Content>
407where
408    O: Output,
409    Content: Writable<O>,
410{
411    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
412        output.write("r#\"").await?;
413        self.0.write_to(output).await?;
414        output.write("\"#").await
415    }
416}
417
418impl<O: Output, Elements> Writable<O> for ArrayFromElements<Elements>
419where
420    Elements: WritableSeq<O>,
421{
422    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
423        o.write("[").await?;
424        self.0
425            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
426            .await?;
427        o.write("]").await
428    }
429}
430
431impl<O: Output, Cont, Item> Writable<O> for AssociatedItem<Cont, Item>
432where
433    Cont: Writable<O>,
434    Item: Writable<O>,
435{
436    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
437        self.0.write_to(o).await?;
438        o.write("::").await?;
439        self.1.write_to(o).await
440    }
441}
442
443impl<O: Output, Expr> Writable<O> for QuestionMarkAfter<Expr>
444where
445    Expr: Writable<O>,
446{
447    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
448        self.0.write_to(o).await?;
449        o.write("?").await
450    }
451}
452
453impl<O: Output, Type, Trait> Writable<O> for TypeAsTrait<Type, Trait>
454where
455    Type: Writable<O>,
456    Trait: Writable<O>,
457{
458    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
459        o.write("<").await?;
460        TypeAsType(&self.0, &self.1).write_to(o).await?;
461        o.write(">").await
462    }
463}
464
465impl<O, Type1, Type2> Writable<O> for TypeAsType<Type1, Type2>
466where
467    O: Output,
468    Type1: Writable<O>,
469    Type2: Writable<O>,
470{
471    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
472        self.0.write_to(o).await?;
473        o.write(" as ").await?;
474        self.1.write_to(o).await
475    }
476}
477
478impl<O, Mods, Name, Args, Return, Where, Body> Writable<O>
479    for FunctionDef<Mods, Name, Args, Return, Where, Body>
480where
481    O: Output,
482    Mods: WritableSeq<O>,
483    Name: Writable<O>,
484    Args: WritableSeq<O>,
485    Return: Writable<O>,
486    Where: WritableSeq<O>,
487    Body: Writable<O>,
488{
489    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
490        self.mods.for_each(&mut ModsAccept(o)).await?;
491        o.write("fn ").await?;
492        self.name.write_to(o).await?;
493        AnonTuple(&self.args).write_to(o).await?;
494        o.write(" -> ").await?;
495        self.return_type.write_to(o).await?;
496        self.where_conds.for_each(&mut where_cond_accept(o)).await?;
497        self.body.write_to(o).await?;
498        o.write("\n\n").await
499    }
500}
501
502impl<O: Output> Writable<O> for FunctionBodyDeclare {
503    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
504        output.write(";").await
505    }
506}
507
508impl<O: Output, Inner> Writable<O> for FunctionBodyImplement<Inner>
509where
510    Inner: Writable<O>,
511{
512    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
513        output.write(" ").await?;
514        Block(&self.0).write_to(output).await
515    }
516}
517
518impl<O, Args, Return> Writable<O> for FunctionPtr<Args, Return>
519where
520    O: Output,
521    Args: WritableSeq<O>,
522    Return: Writable<O>,
523{
524    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
525        let kind = match self.kind {
526            FunctionPtrKind::FnPtr => "fn",
527            FunctionPtrKind::Fn => "Fn",
528            FunctionPtrKind::FnMut => "FnMut",
529            FunctionPtrKind::FnOnce => "FnOnce",
530        };
531        output.write(kind).await?;
532        AnonTuple(&self.args).write_to(output).await?;
533        output.write(" -> ").await?;
534        self.return_type.write_to(output).await
535    }
536}
537
538impl<O: Output, Type, Value> Writable<O> for AssociatedTypeEquals<Type, Value>
539where
540    Type: Writable<O>,
541    Value: Writable<O>,
542{
543    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
544        AssignExpr(&self.0, &self.1).write_to(o).await
545    }
546}
547
548impl<O: Output, Type> Writable<O> for DynOf<Type>
549where
550    Type: Writable<O>,
551{
552    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
553        o.write("dyn ").await?;
554        self.0.write_to(o).await
555    }
556}
557
558impl<O: Output, Expr> Writable<O> for RefOf<Expr>
559where
560    Expr: Writable<O>,
561{
562    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
563        o.write("&").await?;
564        self.0.write_to(o).await
565    }
566}
567
568impl<O, Expr> Writable<O> for Dereference<Expr> where O: Output, Expr: Writable<O> {
569    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
570        output.write("*").await?;
571        self.0.write_to(output).await
572    }
573}
574
575impl<O, Expr> Writable<O> for ParenthesesAround<Expr> where O: Output, Expr: Writable<O> {
576    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
577        output.write("(").await?;
578        self.0.write_to(output).await?;
579        output.write(")").await
580    }
581}
582
583impl<O: Output, Type> Writable<O> for ImplOf<Type>
584where
585    Type: Writable<O>,
586{
587    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
588        o.write("impl ").await?;
589        self.0.write_to(o).await
590    }
591}
592
593impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
594where
595    Type: Writable<O>,
596{
597    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
598        o.write("&'").await?;
599        o.write(self.0).await?;
600        o.write(" ").await?;
601        self.1.write_to(o).await
602    }
603}
604
605impl<O, Expr> Writable<O> for RawConstOf<Expr> where O: Output, Expr: Writable<O> {
606    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
607        output.write("&raw const ").await?;
608        self.0.write_to(output).await
609    }
610}
611
612impl<O, Expr> Writable<O> for RawMutOf<Expr> where O: Output, Expr: Writable<O> {
613    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
614        output.write("&raw mut ").await?;
615        self.0.write_to(output).await
616    }
617}
618
619impl<O, Expr> Writable<O> for ConstPtr<Expr> where O: Output, Expr: Writable<O> {
620    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
621        output.write("*const ").await?;
622        self.0.write_to(output).await
623    }
624}
625
626impl<O, Expr> Writable<O> for MutPtr<Expr> where O: Output, Expr: Writable<O> {
627    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
628        output.write("*mut ").await?;
629        self.0.write_to(output).await
630    }
631}
632
633impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
634where
635    VarName: Writable<O>,
636    Value: Writable<O>,
637{
638    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
639        o.write("type ").await?;
640        self.0.write_to(o).await?;
641        o.write(" = ").await?;
642        self.1.write_to(o).await?;
643        o.write(";\n\n").await?;
644        Ok(())
645    }
646}
647
648impl<O, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
649    for TraitDef<Mods, Name, TypeVars, SuperTraits, Body>
650where
651    O: Output,
652    Mods: WritableSeq<O>,
653    Name: Writable<O>,
654    TypeVars: WritableSeq<O>,
655    SuperTraits: WritableSeq<O>,
656    Body: Writable<O>,
657{
658    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
659        self.mods.for_each(&mut ModsAccept(o)).await?;
660        o.write("trait ").await?;
661        self.name.write_to(o).await?;
662        TypeArgAccept::new(o)
663            .write_inside_diamond(&self.type_variables)
664            .await?;
665        self.super_traits
666            .for_each(&mut type_arg_bound_accept(o))
667            .await?;
668        o.write(" {").await?;
669        self.body.write_to(o).await?;
670        o.write("}\n\n").await?;
671        Ok(())
672    }
673}
674
675impl<O, Mods, TypeVars, Trait, Recv, Where, Body> Writable<O>
676    for TraitImpl<Mods, TypeVars, Trait, Recv, Where, Body>
677where
678    O: Output,
679    Mods: WritableSeq<O>,
680    TypeVars: WritableSeq<O>,
681    Trait: Writable<O>,
682    Recv: Writable<O>,
683    Where: WritableSeq<O>,
684    Body: Writable<O>,
685{
686    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
687        self.mods.for_each(&mut ModsAccept(o)).await?;
688        o.write("impl").await?;
689        TypeArgAccept::new(o)
690            .write_inside_diamond(&self.type_variables)
691            .await?;
692        o.write(" ").await?;
693        self.the_trait.write_to(o).await?;
694        o.write(" for ").await?;
695        self.receiver.write_to(o).await?;
696        self.where_conds.for_each(&mut where_cond_accept(o)).await?;
697        o.write(" {").await?;
698        self.body.write_to(o).await?;
699        o.write("}\n\n").await?;
700        Ok(())
701    }
702}
703
704impl<O, Mods, Name, Elements> Writable<O> for StructDef<Mods, Name, Elements>
705where
706    O: Output,
707    Mods: WritableSeq<O>,
708    Name: Writable<O>,
709    Elements: WritableSeq<O>,
710{
711    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
712        self.mods.for_each(&mut ModsAccept(o)).await?;
713        o.write("struct ").await?;
714        match &self.kind {
715            StructKind::Tuple(name, elements) => {
716                NamedTuple {
717                    name,
718                    args: elements,
719                }
720                .write_to(o)
721                .await?;
722                o.write(";").await
723            }
724            StructKind::NamedFields(name, elements) => {
725                StructCall {
726                    name,
727                    body: StructFields(elements),
728                }
729                .write_to(o)
730                .await
731            }
732        }?;
733        o.write("\n\n").await
734    }
735}
736
737impl<O, Name, Body> Writable<O> for StructCall<Name, Body>
738where
739    O: Output,
740    Name: Writable<O>,
741    Body: Writable<O>,
742{
743    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
744        self.name.write_to(o).await?;
745        o.write(" { ").await?;
746        self.body.write_to(o).await?;
747        o.write(" }").await
748    }
749}
750
751impl<O, Fields> Writable<O> for StructFields<Fields>
752where
753    O: Output,
754    Fields: WritableSeq<O>,
755{
756    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
757        self.0.for_each(&mut struct_fields_accept(output)).await
758    }
759}
760
761impl<O, Name, Value> Writable<O> for DeclareField<Name, Value>
762where
763    O: Output,
764    Name: Writable<O>,
765    Value: Writable<O>,
766{
767    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
768        FunctionParam(&self.0, &self.1).write_to(o).await
769    }
770}
771
772impl<O, Name, Args> Writable<O> for NamedTuple<Name, Args>
773where
774    O: Output,
775    Name: Writable<O>,
776    Args: WritableSeq<O>,
777{
778    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
779        self.name.write_to(output).await?;
780        // Only write the parentheses if arguments is non-empty
781        let mut separator = SeparatedSeqAccept::new(output, Str("("), Str(", "));
782        self.args.for_each(&mut separator).await?;
783        if separator.wrote_any {
784            output.write(")").await?;
785        }
786        Ok(())
787    }
788}
789
790impl<O, Args> Writable<O> for AnonTuple<Args>
791where
792    O: Output,
793    Args: WritableSeq<O>,
794{
795    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
796        o.write("(").await?;
797        self.0
798            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
799            .await?;
800        o.write(")").await
801    }
802}
803
804impl<O, Attr, Value> Writable<O> for WithAttributes<Attr, Value>
805where
806    O: Output,
807    Attr: WritableSeq<O>,
808    Value: Writable<O>,
809{
810    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
811        self.attr
812            .for_each(&mut AttributesAccept::with_separator(
813                output,
814                self.separator,
815            ))
816            .await?;
817        self.value.write_to(output).await
818    }
819}
820
821impl<O> Writable<O> for AttributeSeparator
822where
823    O: Output,
824{
825    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
826        output
827            .write(match self {
828                AttributeSeparator::Space => " ",
829                AttributeSeparator::NewLine => "\n",
830            })
831            .await
832    }
833}
834
835impl<O, Mods, Name, Entries> Writable<O> for EnumDef<Mods, Name, Entries>
836where
837    O: Output,
838    Mods: WritableSeq<O>,
839    Name: Writable<O>,
840    Entries: WritableSeq<O>,
841{
842    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
843        self.mods.for_each(&mut ModsAccept(output)).await?;
844        output.write("enum ").await?;
845        self.name.write_to(output).await?;
846        output.write(" {").await?;
847        self.entries
848            .for_each(&mut SeparatedSeqAccept::comma_separated(output))
849            .await?;
850        output.write("}\n\n").await
851    }
852}
853
854impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
855where
856    O: Output,
857    Name: Writable<O>,
858    TypeArgs: WritableSeq<O>,
859{
860    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
861        self.0.write_to(o).await?;
862        TypeArgAccept::new(o).write_inside_diamond(&self.1).await
863    }
864}
865
866impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
867where
868    O: Output,
869    TypeVar: Writable<O>,
870    Bounds: WritableSeq<O>,
871{
872    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
873        self.0.write_to(output).await?;
874        self.1.for_each(&mut type_arg_bound_accept(output)).await
875    }
876}
877
878impl<O> Writable<O> for Lifetime<'_>
879where
880    O: Output,
881{
882    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
883        output.write("'").await?;
884        output.write(self.0).await
885    }
886}
887
888impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
889where
890    O: Output,
891    Name: Writable<O>,
892    Type: Writable<O>,
893{
894    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
895        self.0.write_to(output).await?;
896        output.write(": ").await?;
897        self.1.write_to(output).await
898    }
899}
900
901//
902// Sequence acceptors
903//
904
905struct ModsAccept<'o, O>(&'o mut O);
906
907impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
908    async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
909    where
910        W: Writable<O>,
911    {
912        writable.write_to(&mut self.0).await?;
913        self.0.write(" ").await
914    }
915}
916
917struct TypeArgAccept<'o, O> {
918    inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
919}
920
921impl<'o, O> TypeArgAccept<'o, O> {
922    fn new(output: &'o mut O) -> Self {
923        Self {
924            inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
925        }
926    }
927}
928
929impl<'o, O> TypeArgAccept<'o, O>
930where
931    O: Output,
932{
933    async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
934    where
935        TypeArgs: WritableSeq<O>,
936    {
937        type_args.for_each(&mut self.inner).await?;
938        if self.inner.wrote_any {
939            self.inner.output.write(">").await?;
940        }
941        Ok(())
942    }
943}
944
945fn type_arg_bound_accept<'o, O>(
946    output: &'o mut O,
947) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
948    SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
949}
950
951fn struct_fields_accept<'o, O>(
952    output: &'o mut O,
953) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
954    SeparatedSeqAccept::new(output, Str("\n"), Str(",\n"))
955}
956
957fn where_cond_accept<'o, O>(
958    output: &'o mut O,
959) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
960    SeparatedSeqAccept::new(output, Str(" where "), Str(", "))
961}