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