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, Type> Writable<O> for RefOf<Type>
559where
560    Type: 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: Output, Type> Writable<O> for ImplOf<Type>
569where
570    Type: Writable<O>,
571{
572    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
573        o.write("impl ").await?;
574        self.0.write_to(o).await
575    }
576}
577
578impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
579where
580    Type: Writable<O>,
581{
582    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
583        o.write("&'").await?;
584        o.write(self.0).await?;
585        o.write(" ").await?;
586        self.1.write_to(o).await
587    }
588}
589
590impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
591where
592    VarName: Writable<O>,
593    Value: Writable<O>,
594{
595    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
596        o.write("type ").await?;
597        self.0.write_to(o).await?;
598        o.write(" = ").await?;
599        self.1.write_to(o).await?;
600        o.write(";\n\n").await?;
601        Ok(())
602    }
603}
604
605impl<O, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
606    for TraitDef<Mods, Name, TypeVars, SuperTraits, Body>
607where
608    O: Output,
609    Mods: WritableSeq<O>,
610    Name: Writable<O>,
611    TypeVars: WritableSeq<O>,
612    SuperTraits: WritableSeq<O>,
613    Body: Writable<O>,
614{
615    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
616        self.mods.for_each(&mut ModsAccept(o)).await?;
617        o.write("trait ").await?;
618        self.name.write_to(o).await?;
619        TypeArgAccept::new(o)
620            .write_inside_diamond(&self.type_variables)
621            .await?;
622        self.super_traits
623            .for_each(&mut type_arg_bound_accept(o))
624            .await?;
625        o.write(" {").await?;
626        self.body.write_to(o).await?;
627        o.write("}\n\n").await?;
628        Ok(())
629    }
630}
631
632impl<O, Mods, TypeVars, Trait, Recv, Where, Body> Writable<O>
633    for TraitImpl<Mods, TypeVars, Trait, Recv, Where, Body>
634where
635    O: Output,
636    Mods: WritableSeq<O>,
637    TypeVars: WritableSeq<O>,
638    Trait: Writable<O>,
639    Recv: Writable<O>,
640    Where: WritableSeq<O>,
641    Body: Writable<O>,
642{
643    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
644        self.mods.for_each(&mut ModsAccept(o)).await?;
645        o.write("impl").await?;
646        TypeArgAccept::new(o)
647            .write_inside_diamond(&self.type_variables)
648            .await?;
649        o.write(" ").await?;
650        self.the_trait.write_to(o).await?;
651        o.write(" for ").await?;
652        self.receiver.write_to(o).await?;
653        self.where_conds.for_each(&mut where_cond_accept(o)).await?;
654        o.write(" {").await?;
655        self.body.write_to(o).await?;
656        o.write("}\n\n").await?;
657        Ok(())
658    }
659}
660
661impl<O, Mods, Name, Elements> Writable<O> for StructDef<Mods, Name, Elements>
662where
663    O: Output,
664    Mods: WritableSeq<O>,
665    Name: Writable<O>,
666    Elements: WritableSeq<O>,
667{
668    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
669        self.mods.for_each(&mut ModsAccept(o)).await?;
670        o.write("struct ").await?;
671        match &self.kind {
672            StructKind::Tuple(name, elements) => {
673                NamedTuple {
674                    name,
675                    args: elements,
676                }
677                .write_to(o)
678                .await?;
679                o.write(";").await
680            }
681            StructKind::NamedFields(name, elements) => {
682                StructCall {
683                    name,
684                    body: StructFields(elements),
685                }
686                .write_to(o)
687                .await
688            }
689        }?;
690        o.write("\n\n").await
691    }
692}
693
694impl<O, Name, Body> Writable<O> for StructCall<Name, Body>
695where
696    O: Output,
697    Name: Writable<O>,
698    Body: Writable<O>,
699{
700    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
701        self.name.write_to(o).await?;
702        o.write(" { ").await?;
703        self.body.write_to(o).await?;
704        o.write(" }").await
705    }
706}
707
708impl<O, Fields> Writable<O> for StructFields<Fields>
709where
710    O: Output,
711    Fields: WritableSeq<O>,
712{
713    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
714        self.0.for_each(&mut struct_fields_accept(output)).await
715    }
716}
717
718impl<O, Name, Value> Writable<O> for DeclareField<Name, Value>
719where
720    O: Output,
721    Name: Writable<O>,
722    Value: Writable<O>,
723{
724    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
725        FunctionParam(&self.0, &self.1).write_to(o).await
726    }
727}
728
729impl<O, Name, Args> Writable<O> for NamedTuple<Name, Args>
730where
731    O: Output,
732    Name: Writable<O>,
733    Args: WritableSeq<O>,
734{
735    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
736        self.name.write_to(output).await?;
737        // Only write the parentheses if arguments is non-empty
738        let mut separator = SeparatedSeqAccept::new(output, Str("("), Str(", "));
739        self.args.for_each(&mut separator).await?;
740        if separator.wrote_any {
741            output.write(")").await?;
742        }
743        Ok(())
744    }
745}
746
747impl<O, Args> Writable<O> for AnonTuple<Args>
748where
749    O: Output,
750    Args: WritableSeq<O>,
751{
752    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
753        o.write("(").await?;
754        self.0
755            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
756            .await?;
757        o.write(")").await
758    }
759}
760
761impl<O, Attr, Value> Writable<O> for WithAttributes<Attr, Value>
762where
763    O: Output,
764    Attr: WritableSeq<O>,
765    Value: Writable<O>,
766{
767    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
768        self.attr
769            .for_each(&mut AttributesAccept::with_separator(
770                output,
771                self.separator,
772            ))
773            .await?;
774        self.value.write_to(output).await
775    }
776}
777
778impl<O> Writable<O> for AttributeSeparator
779where
780    O: Output,
781{
782    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
783        output
784            .write(match self {
785                AttributeSeparator::Space => " ",
786                AttributeSeparator::NewLine => "\n",
787            })
788            .await
789    }
790}
791
792impl<O, Mods, Name, Entries> Writable<O> for EnumDef<Mods, Name, Entries>
793where
794    O: Output,
795    Mods: WritableSeq<O>,
796    Name: Writable<O>,
797    Entries: WritableSeq<O>,
798{
799    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
800        self.mods.for_each(&mut ModsAccept(output)).await?;
801        output.write("enum ").await?;
802        self.name.write_to(output).await?;
803        output.write(" {").await?;
804        self.entries
805            .for_each(&mut SeparatedSeqAccept::comma_separated(output))
806            .await?;
807        output.write("}\n\n").await
808    }
809}
810
811impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
812where
813    O: Output,
814    Name: Writable<O>,
815    TypeArgs: WritableSeq<O>,
816{
817    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
818        self.0.write_to(o).await?;
819        TypeArgAccept::new(o).write_inside_diamond(&self.1).await
820    }
821}
822
823impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
824where
825    O: Output,
826    TypeVar: Writable<O>,
827    Bounds: WritableSeq<O>,
828{
829    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
830        self.0.write_to(output).await?;
831        self.1.for_each(&mut type_arg_bound_accept(output)).await
832    }
833}
834
835impl<O> Writable<O> for Lifetime<'_>
836where
837    O: Output,
838{
839    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
840        output.write("'").await?;
841        output.write(self.0).await
842    }
843}
844
845impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
846where
847    O: Output,
848    Name: Writable<O>,
849    Type: Writable<O>,
850{
851    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
852        self.0.write_to(output).await?;
853        output.write(": ").await?;
854        self.1.write_to(output).await
855    }
856}
857
858//
859// Sequence acceptors
860//
861
862struct ModsAccept<'o, O>(&'o mut O);
863
864impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
865    async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
866    where
867        W: Writable<O>,
868    {
869        writable.write_to(&mut self.0).await?;
870        self.0.write(" ").await
871    }
872}
873
874struct TypeArgAccept<'o, O> {
875    inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
876}
877
878impl<'o, O> TypeArgAccept<'o, O> {
879    fn new(output: &'o mut O) -> Self {
880        Self {
881            inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
882        }
883    }
884}
885
886impl<'o, O> TypeArgAccept<'o, O>
887where
888    O: Output,
889{
890    async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
891    where
892        TypeArgs: WritableSeq<O>,
893    {
894        type_args.for_each(&mut self.inner).await?;
895        if self.inner.wrote_any {
896            self.inner.output.write(">").await?;
897        }
898        Ok(())
899    }
900}
901
902fn type_arg_bound_accept<'o, O>(
903    output: &'o mut O,
904) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
905    SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
906}
907
908fn struct_fields_accept<'o, O>(
909    output: &'o mut O,
910) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
911    SeparatedSeqAccept::new(output, Str("\n"), Str(",\n"))
912}
913
914fn where_cond_accept<'o, O>(
915    output: &'o mut O,
916) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
917    SeparatedSeqAccept::new(output, Str(" where "), Str(", "))
918}