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 Doc<Value>
62where
63    O: Output,
64    Value: Writable<O>,
65{
66    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
67        output.write("doc(").await?;
68        match self {
69            Self::Hidden => output.write("hidden").await,
70            Self::Inline => output.write("inline").await,
71            Self::NoInline => output.write("no_inline").await,
72            Self::Alias(value) => {
73                AssignExpr(Str("alias"), Quoted(value))
74                    .write_to(output)
75                    .await
76            }
77            Self::Test(value) => {
78                output.write("test").await?;
79                ParenthesesAround(value).write_to(output).await
80            }
81        }?;
82        output.write(")").await
83    }
84}
85
86impl<O, Value> Writable<O> for Target<Value>
87where
88    O: Output,
89    Value: Writable<O>,
90{
91    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
92        let (kind, value) = match self {
93            Self::Os(value) => ("os", value),
94            Self::Family(value) => ("family", value),
95            Self::Arch(value) => ("arch", value),
96        };
97        AssignExpr(Combined(Str("target_"), Str(kind)), Quoted(value))
98            .write_to(output)
99            .await
100    }
101}
102
103struct Quoted<A>(A);
104
105impl<O, A> Writable<O> for Quoted<A>
106where
107    O: Output,
108    A: Writable<O>,
109{
110    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
111        output.write("\"").await?;
112        self.0.write_to(output).await?;
113        output.write("\"").await
114    }
115}
116
117impl<O, Arg> Writable<O> for Link<Arg>
118where
119    O: Output,
120    Arg: Writable<O>,
121{
122    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
123        output.write("link(").await?;
124        self.0.write_to(output).await?;
125        output.write(")").await
126    }
127}
128
129impl<O> Writable<O> for NoMangle
130where
131    O: Output,
132    O::Ctx: ContextProvides<Edition>,
133{
134    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
135        let edition = output.get_ctx();
136        output
137            .write(if edition >= &Edition::Rust2024 {
138                "unsafe(no_mangle)"
139            } else {
140                "no_mangle"
141            })
142            .await
143    }
144}
145
146impl<O, Lints> Writable<O> for AllowLints<Lints>
147where
148    O: Output,
149    Lints: WritableSeq<O>,
150{
151    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
152        let mut acceptor = SeparatedSeqAccept::new(output, Str("allow("), Str(","));
153        self.0.for_each(&mut acceptor).await?;
154        if acceptor.wrote_any {
155            output.write(")").await?;
156        }
157        Ok(())
158    }
159}
160
161impl<O, Msg, Since> Writable<O> for Deprecated<Msg, Since>
162where
163    O: Output,
164    Msg: Writable<O>,
165    Since: Writable<O>,
166{
167    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
168        match self {
169            Self::Basic => output.write("deprecated").await,
170            Self::Message(msg) => {
171                output.write("deprecated = \"").await?;
172                msg.write_to(output).await?;
173                output.write("\"").await
174            }
175            Self::Full { since, note } => {
176                output.write("deprecated(since = \"").await?;
177                since.write_to(output).await?;
178                output.write("\", note = \"").await?;
179                note.write_to(output).await?;
180                output.write("\")").await
181            }
182        }
183    }
184}
185
186impl<O> Writable<O> for MustUse
187where
188    O: Output,
189{
190    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
191        output.write("must_use").await
192    }
193}
194
195impl<O> Writable<O> for ModPub
196where
197    O: Output,
198{
199    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
200        output.write("pub").await
201    }
202}
203
204impl<O> Writable<O> for ModUnsafe
205where
206    O: Output,
207{
208    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
209        output.write("unsafe").await
210    }
211}
212
213impl<O, Abi> Writable<O> for ModUnsafeExtern<Abi>
214where
215    O: Output,
216    Abi: Writable<O>,
217{
218    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
219        output.write("unsafe extern \"").await?;
220        self.0.write_to(output).await?;
221        output.write("\"").await
222    }
223}
224
225impl<O, Abi, Body> Writable<O> for ExternBlock<Abi, Body>
226where
227    O: Output,
228    O::Ctx: ContextProvides<Edition>,
229    Abi: Writable<O>,
230    Body: Writable<O>,
231{
232    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
233        let edition = output.get_ctx();
234        if edition >= &Edition::Rust2024 {
235            output.write("unsafe ").await?;
236        }
237        output.write("extern \"").await?;
238        self.abi.write_to(output).await?;
239        output.write("\" {").await?;
240        self.body.write_to(output).await?;
241        output.write("}").await
242    }
243}
244
245impl<O, Name, Body> Writable<O> for ModBlock<Name, Body>
246where
247    O: Output,
248    Name: Writable<O>,
249    Body: Writable<O>,
250{
251    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
252        output.write("mod ").await?;
253        self.name.write_to(output).await?;
254        output.write(" {\n").await?;
255        self.body.write_to(output).await?;
256        output.write("}\n").await
257    }
258}
259
260impl<O, Cond, Body> Writable<O> for IfBlock<Cond, Body>
261where
262    O: Output,
263    Cond: Writable<O>,
264    Body: Writable<O>,
265{
266    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
267        output.write("if ").await?;
268        self.0.write_to(output).await?;
269        output.write(" {\n").await?;
270        self.1.write_to(output).await?;
271        output.write("\n}").await
272    }
273}
274
275impl<O, Before, After> Writable<O> for Else<Before, After>
276where
277    O: Output,
278    Before: Writable<O>,
279    After: Writable<O>,
280{
281    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
282        self.0.write_to(output).await?;
283        output.write(" else ").await?;
284        self.1.write_to(output).await
285    }
286}
287
288impl<O, Body> Writable<O> for Block<Body>
289where
290    O: Output,
291    Body: Writable<O>,
292{
293    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
294        output.write("{\n").await?;
295        self.0.write_to(output).await?;
296        output.write("\n}").await
297    }
298}
299
300impl<O: Output, Expr> Writable<O> for UnsafeBlock<Expr>
301where
302    Expr: Writable<O>,
303{
304    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
305        o.write("unsafe {\n").await?;
306        self.0.write_to(o).await?;
307        o.write("\n}").await?;
308        Ok(())
309    }
310}
311
312impl<O, InputVars, Expr> Writable<O> for Closure<InputVars, Expr>
313where
314    O: Output,
315    InputVars: WritableSeq<O>,
316    Expr: Writable<O>,
317{
318    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
319        o.write("|").await?;
320        self.input_vars
321            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
322            .await?;
323        o.write("| {\n").await?;
324        self.inside_block.write_to(o).await?;
325        o.write("\n}").await?;
326        Ok(())
327    }
328}
329
330impl<O: Output, Recv, FuncName, Args> Writable<O> for FunctionCall<Recv, FuncName, Args>
331where
332    Recv: Writable<O>,
333    FuncName: Writable<O>,
334    Args: WritableSeq<O>,
335{
336    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
337        self.receiver.write_to(o).await?;
338        o.write(if self.is_assoc { "::" } else { "." }).await?;
339        self.name.write_to(o).await?;
340        AnonTuple(&self.args).write_to(o).await
341    }
342}
343
344impl<O: Output, Name, Args> Writable<O> for Turbofish<Name, Args>
345where
346    Name: Writable<O>,
347    Args: WritableSeq<O>,
348{
349    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
350        self.0.write_to(output).await?;
351        let mut acceptor = SeparatedSeqAccept::new(output, Str("::<"), Str(", "));
352        self.1.for_each(&mut acceptor).await?;
353        if acceptor.wrote_any {
354            output.write(">").await?;
355        }
356        Ok(())
357    }
358}
359
360impl<O, Owner, Field> Writable<O> for FieldAccess<Owner, Field>
361where
362    O: Output,
363    Owner: Writable<O>,
364    Field: Writable<O>,
365{
366    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
367        self.0.write_to(output).await?;
368        output.write(".").await?;
369        self.1.write_to(output).await
370    }
371}
372
373impl<O, Expr> Writable<O> for Stmt<Expr>
374where
375    O: Output,
376    Expr: Writable<O>,
377{
378    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
379        self.0.write_to(output).await?;
380        output.write(";\n").await
381    }
382}
383
384impl<O: Output, Variable, Expr> Writable<O> for LetStmt<Variable, Expr>
385where
386    Variable: Writable<O>,
387    Expr: Writable<O>,
388{
389    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
390        Stmt(LetExpr(&self.0, &self.1)).write_to(o).await
391    }
392}
393
394impl<O: Output, Variable, Expr> Writable<O> for LetMutStmt<Variable, Expr>
395where
396    Variable: Writable<O>,
397    Expr: Writable<O>,
398{
399    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
400        Stmt(LetMutExpr(&self.0, &self.1)).write_to(o).await
401    }
402}
403
404impl<O: Output, Variable, Expr> Writable<O> for AssignStmt<Variable, Expr>
405where
406    Variable: Writable<O>,
407    Expr: Writable<O>,
408{
409    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
410        Stmt(AssignExpr(&self.0, &self.1)).write_to(o).await
411    }
412}
413
414impl<O, Expr> Writable<O> for ReturnStmt<Expr>
415where
416    O: Output,
417    Expr: Writable<O>,
418{
419    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
420        o.write("return ").await?;
421        self.0.write_to(o).await?;
422        o.write(";\n").await
423    }
424}
425
426impl<O, Pattern, Expr> Writable<O> for LetExpr<Pattern, Expr>
427where
428    O: Output,
429    Pattern: Writable<O>,
430    Expr: Writable<O>,
431{
432    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
433        output.write("let ").await?;
434        AssignExpr(&self.0, &self.1).write_to(output).await
435    }
436}
437
438struct LetMutExpr<Pattern, Expr>(Pattern, Expr);
439
440impl<O, Pattern, Expr> Writable<O> for LetMutExpr<Pattern, Expr>
441where
442    O: Output,
443    Pattern: Writable<O>,
444    Expr: Writable<O>,
445{
446    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
447        output.write("let mut ").await?;
448        AssignExpr(&self.0, &self.1).write_to(output).await
449    }
450}
451
452struct AssignExpr<Variable, Expr>(Variable, Expr);
453
454impl<O, Variable, Expr> Writable<O> for AssignExpr<Variable, Expr>
455where
456    O: Output,
457    Variable: Writable<O>,
458    Expr: Writable<O>,
459{
460    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
461        self.0.write_to(o).await?;
462        o.write(" = ").await?;
463        self.1.write_to(o).await
464    }
465}
466
467impl<O, Content> Writable<O> for RawStringLiteral<Content>
468where
469    O: Output,
470    Content: Writable<O>,
471{
472    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
473        output.write("r#\"").await?;
474        self.0.write_to(output).await?;
475        output.write("\"#").await
476    }
477}
478
479impl<O: Output, Elements> Writable<O> for ArrayFromElements<Elements>
480where
481    Elements: WritableSeq<O>,
482{
483    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
484        o.write("[").await?;
485        self.0
486            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
487            .await?;
488        o.write("]").await
489    }
490}
491
492impl<O: Output, Cont, Item> Writable<O> for AssociatedItem<Cont, Item>
493where
494    Cont: Writable<O>,
495    Item: Writable<O>,
496{
497    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
498        self.0.write_to(o).await?;
499        o.write("::").await?;
500        self.1.write_to(o).await
501    }
502}
503
504impl<O: Output, Expr> Writable<O> for QuestionMarkAfter<Expr>
505where
506    Expr: Writable<O>,
507{
508    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
509        self.0.write_to(o).await?;
510        o.write("?").await
511    }
512}
513
514impl<O: Output, Type, Trait> Writable<O> for TypeAsTrait<Type, Trait>
515where
516    Type: Writable<O>,
517    Trait: Writable<O>,
518{
519    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
520        o.write("<").await?;
521        TypeAsType(&self.0, &self.1).write_to(o).await?;
522        o.write(">").await
523    }
524}
525
526impl<O, Type1, Type2> Writable<O> for TypeAsType<Type1, Type2>
527where
528    O: Output,
529    Type1: Writable<O>,
530    Type2: Writable<O>,
531{
532    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
533        self.0.write_to(o).await?;
534        o.write(" as ").await?;
535        self.1.write_to(o).await
536    }
537}
538
539impl<O, Mods, Name, Args, Return, Where, Body> Writable<O>
540    for FunctionDef<Mods, Name, Args, Return, Where, Body>
541where
542    O: Output,
543    Mods: WritableSeq<O>,
544    Name: Writable<O>,
545    Args: WritableSeq<O>,
546    Return: Writable<O>,
547    Where: WritableSeq<O>,
548    Body: Writable<O>,
549{
550    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
551        self.mods.for_each(&mut ModsAccept(o)).await?;
552        o.write("fn ").await?;
553        self.name.write_to(o).await?;
554        AnonTuple(&self.args).write_to(o).await?;
555        o.write(" -> ").await?;
556        self.return_type.write_to(o).await?;
557        self.where_conds.for_each(&mut where_cond_accept(o)).await?;
558        self.body.write_to(o).await?;
559        o.write("\n\n").await
560    }
561}
562
563impl<O: Output> Writable<O> for FunctionBodyDeclare {
564    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
565        output.write(";").await
566    }
567}
568
569impl<O: Output, Inner> Writable<O> for FunctionBodyImplement<Inner>
570where
571    Inner: Writable<O>,
572{
573    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
574        output.write(" ").await?;
575        Block(&self.0).write_to(output).await
576    }
577}
578
579impl<O, Args, Return> Writable<O> for FunctionPtr<Args, Return>
580where
581    O: Output,
582    Args: WritableSeq<O>,
583    Return: Writable<O>,
584{
585    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
586        let kind = match self.kind {
587            FunctionPtrKind::FnPtr => "fn",
588            FunctionPtrKind::Fn => "Fn",
589            FunctionPtrKind::FnMut => "FnMut",
590            FunctionPtrKind::FnOnce => "FnOnce",
591        };
592        output.write(kind).await?;
593        AnonTuple(&self.args).write_to(output).await?;
594        output.write(" -> ").await?;
595        self.return_type.write_to(output).await
596    }
597}
598
599impl<O: Output, Type, Value> Writable<O> for AssociatedTypeEquals<Type, Value>
600where
601    Type: Writable<O>,
602    Value: Writable<O>,
603{
604    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
605        AssignExpr(&self.0, &self.1).write_to(o).await
606    }
607}
608
609impl<O: Output, Type> Writable<O> for DynOf<Type>
610where
611    Type: Writable<O>,
612{
613    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
614        o.write("dyn ").await?;
615        self.0.write_to(o).await
616    }
617}
618
619impl<O: Output, Expr> Writable<O> for RefOf<Expr>
620where
621    Expr: Writable<O>,
622{
623    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
624        o.write("&").await?;
625        self.0.write_to(o).await
626    }
627}
628
629impl<O, Expr> Writable<O> for Dereference<Expr>
630where
631    O: Output,
632    Expr: Writable<O>,
633{
634    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
635        output.write("*").await?;
636        self.0.write_to(output).await
637    }
638}
639
640impl<O, Expr> Writable<O> for ParenthesesAround<Expr>
641where
642    O: Output,
643    Expr: Writable<O>,
644{
645    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
646        output.write("(").await?;
647        self.0.write_to(output).await?;
648        output.write(")").await
649    }
650}
651
652impl<O: Output, Type> Writable<O> for ImplOf<Type>
653where
654    Type: Writable<O>,
655{
656    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
657        o.write("impl ").await?;
658        self.0.write_to(o).await
659    }
660}
661
662impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
663where
664    Type: Writable<O>,
665{
666    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
667        o.write("&'").await?;
668        o.write(self.0).await?;
669        o.write(" ").await?;
670        self.1.write_to(o).await
671    }
672}
673
674impl<O, Expr> Writable<O> for RawConstOf<Expr>
675where
676    O: Output,
677    Expr: Writable<O>,
678{
679    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
680        output.write("&raw const ").await?;
681        self.0.write_to(output).await
682    }
683}
684
685impl<O, Expr> Writable<O> for RawMutOf<Expr>
686where
687    O: Output,
688    Expr: Writable<O>,
689{
690    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
691        output.write("&raw mut ").await?;
692        self.0.write_to(output).await
693    }
694}
695
696impl<O, Expr> Writable<O> for ConstPtr<Expr>
697where
698    O: Output,
699    Expr: Writable<O>,
700{
701    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
702        output.write("*const ").await?;
703        self.0.write_to(output).await
704    }
705}
706
707impl<O, Expr> Writable<O> for MutPtr<Expr>
708where
709    O: Output,
710    Expr: Writable<O>,
711{
712    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
713        output.write("*mut ").await?;
714        self.0.write_to(output).await
715    }
716}
717
718impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
719where
720    VarName: Writable<O>,
721    Value: Writable<O>,
722{
723    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
724        o.write("type ").await?;
725        self.0.write_to(o).await?;
726        o.write(" = ").await?;
727        self.1.write_to(o).await?;
728        o.write(";\n\n").await?;
729        Ok(())
730    }
731}
732
733impl<O, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
734    for TraitDef<Mods, Name, TypeVars, SuperTraits, Body>
735where
736    O: Output,
737    Mods: WritableSeq<O>,
738    Name: Writable<O>,
739    TypeVars: WritableSeq<O>,
740    SuperTraits: WritableSeq<O>,
741    Body: Writable<O>,
742{
743    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
744        self.mods.for_each(&mut ModsAccept(o)).await?;
745        o.write("trait ").await?;
746        self.name.write_to(o).await?;
747        TypeArgAccept::new(o)
748            .write_inside_diamond(&self.type_variables)
749            .await?;
750        self.super_traits
751            .for_each(&mut type_arg_bound_accept(o))
752            .await?;
753        o.write(" {").await?;
754        self.body.write_to(o).await?;
755        o.write("}\n\n").await?;
756        Ok(())
757    }
758}
759
760impl<O, Mods, TypeVars, Trait, Recv, Where, Body> Writable<O>
761    for TraitImpl<Mods, TypeVars, Trait, Recv, Where, Body>
762where
763    O: Output,
764    Mods: WritableSeq<O>,
765    TypeVars: WritableSeq<O>,
766    Trait: Writable<O>,
767    Recv: Writable<O>,
768    Where: WritableSeq<O>,
769    Body: Writable<O>,
770{
771    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
772        self.mods.for_each(&mut ModsAccept(o)).await?;
773        o.write("impl").await?;
774        TypeArgAccept::new(o)
775            .write_inside_diamond(&self.type_variables)
776            .await?;
777        o.write(" ").await?;
778        self.the_trait.write_to(o).await?;
779        o.write(" for ").await?;
780        self.receiver.write_to(o).await?;
781        self.where_conds.for_each(&mut where_cond_accept(o)).await?;
782        o.write(" {").await?;
783        self.body.write_to(o).await?;
784        o.write("}\n\n").await?;
785        Ok(())
786    }
787}
788
789impl<O, Mods, Name, Elements> Writable<O> for StructDef<Mods, Name, Elements>
790where
791    O: Output,
792    Mods: WritableSeq<O>,
793    Name: Writable<O>,
794    Elements: WritableSeq<O>,
795{
796    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
797        self.mods.for_each(&mut ModsAccept(o)).await?;
798        o.write("struct ").await?;
799        match &self.kind {
800            StructKind::Tuple(name, elements) => {
801                NamedTuple {
802                    name,
803                    args: elements,
804                }
805                .write_to(o)
806                .await?;
807                o.write(";").await
808            }
809            StructKind::NamedFields(name, elements) => {
810                StructCall {
811                    name,
812                    body: StructFields(elements),
813                }
814                .write_to(o)
815                .await
816            }
817        }?;
818        o.write("\n\n").await
819    }
820}
821
822impl<O, Name, Body> Writable<O> for StructCall<Name, Body>
823where
824    O: Output,
825    Name: Writable<O>,
826    Body: Writable<O>,
827{
828    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
829        self.name.write_to(o).await?;
830        o.write(" { ").await?;
831        self.body.write_to(o).await?;
832        o.write(" }").await
833    }
834}
835
836impl<O, Fields> Writable<O> for StructFields<Fields>
837where
838    O: Output,
839    Fields: WritableSeq<O>,
840{
841    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
842        self.0.for_each(&mut struct_fields_accept(output)).await
843    }
844}
845
846impl<O, Mods, Name, Value> Writable<O> for DeclareField<Mods, Name, Value>
847where
848    O: Output,
849    Mods: WritableSeq<O>,
850    Name: Writable<O>,
851    Value: Writable<O>,
852{
853    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
854        self.mods.for_each(&mut ModsAccept(output)).await?;
855        FillOutField(&self.name, &self.value).write_to(output).await
856    }
857}
858
859impl<O, Name, Value> Writable<O> for FillOutField<Name, Value>
860where
861    O: Output,
862    Name: Writable<O>,
863    Value: Writable<O>,
864{
865    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
866        FunctionParam(&self.0, &self.1).write_to(output).await
867    }
868}
869
870impl<O, Name, Args> Writable<O> for NamedTuple<Name, Args>
871where
872    O: Output,
873    Name: Writable<O>,
874    Args: WritableSeq<O>,
875{
876    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
877        self.name.write_to(output).await?;
878        // Only write the parentheses if arguments is non-empty
879        let mut separator = SeparatedSeqAccept::new(output, Str("("), Str(", "));
880        self.args.for_each(&mut separator).await?;
881        if separator.wrote_any {
882            output.write(")").await?;
883        }
884        Ok(())
885    }
886}
887
888impl<O, Args> Writable<O> for AnonTuple<Args>
889where
890    O: Output,
891    Args: WritableSeq<O>,
892{
893    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
894        o.write("(").await?;
895        self.0
896            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
897            .await?;
898        o.write(")").await
899    }
900}
901
902impl<O, Attr, Value> Writable<O> for WithAttributes<Attr, Value>
903where
904    O: Output,
905    Attr: WritableSeq<O>,
906    Value: Writable<O>,
907{
908    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
909        self.attr
910            .for_each(&mut AttributesAccept::with_separator(
911                output,
912                self.separator,
913            ))
914            .await?;
915        self.value.write_to(output).await
916    }
917}
918
919impl<O> Writable<O> for AttributeSeparator
920where
921    O: Output,
922{
923    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
924        output
925            .write(match self {
926                AttributeSeparator::Space => " ",
927                AttributeSeparator::NewLine => "\n",
928            })
929            .await
930    }
931}
932
933impl<O, Mods, Name, Entries> Writable<O> for EnumDef<Mods, Name, Entries>
934where
935    O: Output,
936    Mods: WritableSeq<O>,
937    Name: Writable<O>,
938    Entries: WritableSeq<O>,
939{
940    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
941        self.mods.for_each(&mut ModsAccept(output)).await?;
942        output.write("enum ").await?;
943        self.name.write_to(output).await?;
944        output.write(" {").await?;
945        self.entries
946            .for_each(&mut SeparatedSeqAccept::comma_separated(output))
947            .await?;
948        output.write("}\n\n").await
949    }
950}
951
952impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
953where
954    O: Output,
955    Name: Writable<O>,
956    TypeArgs: WritableSeq<O>,
957{
958    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
959        self.0.write_to(o).await?;
960        TypeArgAccept::new(o).write_inside_diamond(&self.1).await
961    }
962}
963
964impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
965where
966    O: Output,
967    TypeVar: Writable<O>,
968    Bounds: WritableSeq<O>,
969{
970    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
971        self.0.write_to(output).await?;
972        self.1.for_each(&mut type_arg_bound_accept(output)).await
973    }
974}
975
976impl<O> Writable<O> for Lifetime<'_>
977where
978    O: Output,
979{
980    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
981        output.write("'").await?;
982        output.write(self.0).await
983    }
984}
985
986impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
987where
988    O: Output,
989    Name: Writable<O>,
990    Type: Writable<O>,
991{
992    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
993        self.0.write_to(output).await?;
994        output.write(": ").await?;
995        self.1.write_to(output).await
996    }
997}
998
999//
1000// Sequence acceptors
1001//
1002
1003struct ModsAccept<'o, O>(&'o mut O);
1004
1005impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
1006    async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
1007    where
1008        W: Writable<O>,
1009    {
1010        writable.write_to(&mut self.0).await?;
1011        self.0.write(" ").await
1012    }
1013}
1014
1015struct TypeArgAccept<'o, O> {
1016    inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
1017}
1018
1019impl<'o, O> TypeArgAccept<'o, O> {
1020    fn new(output: &'o mut O) -> Self {
1021        Self {
1022            inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
1023        }
1024    }
1025}
1026
1027impl<'o, O> TypeArgAccept<'o, O>
1028where
1029    O: Output,
1030{
1031    async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
1032    where
1033        TypeArgs: WritableSeq<O>,
1034    {
1035        type_args.for_each(&mut self.inner).await?;
1036        if self.inner.wrote_any {
1037            self.inner.output.write(">").await?;
1038        }
1039        Ok(())
1040    }
1041}
1042
1043fn type_arg_bound_accept<'o, O>(
1044    output: &'o mut O,
1045) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
1046    SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
1047}
1048
1049fn struct_fields_accept<'o, O>(
1050    output: &'o mut O,
1051) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
1052    SeparatedSeqAccept::new(output, Str("\n"), Str(",\n"))
1053}
1054
1055fn where_cond_accept<'o, O>(
1056    output: &'o mut O,
1057) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
1058    SeparatedSeqAccept::new(output, Str(" where "), Str(", "))
1059}