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, output: &mut O) -> Result<(), O::Error> {
772        let impl_block = Impl {
773            mods: &self.mods,
774            type_variables: &self.type_variables,
775            receiver: Combined(&self.the_trait, Combined(Str(" for "), &self.receiver)),
776            where_conds: &self.where_conds,
777            body: &self.body,
778        };
779        impl_block.write_to(output).await
780    }
781}
782
783impl<O, Mods, TypeVars, Recv, Where, Body> Writable<O> for Impl<Mods, TypeVars, Recv, Where, Body>
784where
785    O: Output,
786    Mods: WritableSeq<O>,
787    TypeVars: WritableSeq<O>,
788    Recv: Writable<O>,
789    Where: WritableSeq<O>,
790    Body: Writable<O>,
791{
792    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
793        self.mods.for_each(&mut ModsAccept(o)).await?;
794        o.write("impl").await?;
795        TypeArgAccept::new(o)
796            .write_inside_diamond(&self.type_variables)
797            .await?;
798        o.write(" ").await?;
799        self.receiver.write_to(o).await?;
800        self.where_conds.for_each(&mut where_cond_accept(o)).await?;
801        o.write(" {").await?;
802        self.body.write_to(o).await?;
803        o.write("}\n\n").await?;
804        Ok(())
805    }
806}
807
808impl<O, Mods, Name, Elements> Writable<O> for StructDef<Mods, Name, Elements>
809where
810    O: Output,
811    Mods: WritableSeq<O>,
812    Name: Writable<O>,
813    Elements: WritableSeq<O>,
814{
815    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
816        self.mods.for_each(&mut ModsAccept(o)).await?;
817        o.write("struct ").await?;
818        match &self.kind {
819            StructKind::Tuple(name, elements) => {
820                NamedTuple {
821                    name,
822                    args: elements,
823                }
824                .write_to(o)
825                .await?;
826                o.write(";").await
827            }
828            StructKind::NamedFields(name, elements) => {
829                StructCall {
830                    name,
831                    body: StructFields(elements),
832                }
833                .write_to(o)
834                .await
835            }
836        }?;
837        o.write("\n\n").await
838    }
839}
840
841impl<O, Name, Body> Writable<O> for StructCall<Name, Body>
842where
843    O: Output,
844    Name: Writable<O>,
845    Body: Writable<O>,
846{
847    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
848        self.name.write_to(o).await?;
849        o.write(" { ").await?;
850        self.body.write_to(o).await?;
851        o.write(" }").await
852    }
853}
854
855impl<O, Fields> Writable<O> for StructFields<Fields>
856where
857    O: Output,
858    Fields: WritableSeq<O>,
859{
860    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
861        self.0.for_each(&mut struct_fields_accept(output)).await
862    }
863}
864
865impl<O, Mods, Name, Value> Writable<O> for DeclareField<Mods, Name, Value>
866where
867    O: Output,
868    Mods: WritableSeq<O>,
869    Name: Writable<O>,
870    Value: Writable<O>,
871{
872    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
873        self.mods.for_each(&mut ModsAccept(output)).await?;
874        FillOutField(&self.name, &self.value).write_to(output).await
875    }
876}
877
878impl<O, Name, Value> Writable<O> for FillOutField<Name, Value>
879where
880    O: Output,
881    Name: Writable<O>,
882    Value: Writable<O>,
883{
884    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
885        FunctionParam(&self.0, &self.1).write_to(output).await
886    }
887}
888
889impl<O, Name, Args> Writable<O> for NamedTuple<Name, Args>
890where
891    O: Output,
892    Name: Writable<O>,
893    Args: WritableSeq<O>,
894{
895    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
896        self.name.write_to(output).await?;
897        // Only write the parentheses if arguments is non-empty
898        let mut separator = SeparatedSeqAccept::new(output, Str("("), Str(", "));
899        self.args.for_each(&mut separator).await?;
900        if separator.wrote_any {
901            output.write(")").await?;
902        }
903        Ok(())
904    }
905}
906
907impl<O, Args> Writable<O> for AnonTuple<Args>
908where
909    O: Output,
910    Args: WritableSeq<O>,
911{
912    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
913        o.write("(").await?;
914        self.0
915            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
916            .await?;
917        o.write(")").await
918    }
919}
920
921impl<O, Attr, Value> Writable<O> for WithAttributes<Attr, Value>
922where
923    O: Output,
924    Attr: WritableSeq<O>,
925    Value: Writable<O>,
926{
927    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
928        self.attr
929            .for_each(&mut AttributesAccept::with_separator(
930                output,
931                self.separator,
932            ))
933            .await?;
934        self.value.write_to(output).await
935    }
936}
937
938impl<O> Writable<O> for AttributeSeparator
939where
940    O: Output,
941{
942    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
943        output
944            .write(match self {
945                AttributeSeparator::Space => " ",
946                AttributeSeparator::NewLine => "\n",
947            })
948            .await
949    }
950}
951
952impl<O, Mods, Name, Entries> Writable<O> for EnumDef<Mods, Name, Entries>
953where
954    O: Output,
955    Mods: WritableSeq<O>,
956    Name: Writable<O>,
957    Entries: WritableSeq<O>,
958{
959    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
960        self.mods.for_each(&mut ModsAccept(output)).await?;
961        output.write("enum ").await?;
962        self.name.write_to(output).await?;
963        output.write(" {").await?;
964        self.entries
965            .for_each(&mut SeparatedSeqAccept::comma_separated(output))
966            .await?;
967        output.write("}\n\n").await
968    }
969}
970
971impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
972where
973    O: Output,
974    Name: Writable<O>,
975    TypeArgs: WritableSeq<O>,
976{
977    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
978        self.0.write_to(o).await?;
979        TypeArgAccept::new(o).write_inside_diamond(&self.1).await
980    }
981}
982
983impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
984where
985    O: Output,
986    TypeVar: Writable<O>,
987    Bounds: WritableSeq<O>,
988{
989    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
990        self.0.write_to(output).await?;
991        self.1.for_each(&mut type_arg_bound_accept(output)).await
992    }
993}
994
995impl<O> Writable<O> for Lifetime<'_>
996where
997    O: Output,
998{
999    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
1000        output.write("'").await?;
1001        output.write(self.0).await
1002    }
1003}
1004
1005impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
1006where
1007    O: Output,
1008    Name: Writable<O>,
1009    Type: Writable<O>,
1010{
1011    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
1012        self.0.write_to(output).await?;
1013        output.write(": ").await?;
1014        self.1.write_to(output).await
1015    }
1016}
1017
1018//
1019// Sequence acceptors
1020//
1021
1022struct ModsAccept<'o, O>(&'o mut O);
1023
1024impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
1025    async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
1026    where
1027        W: Writable<O>,
1028    {
1029        writable.write_to(&mut self.0).await?;
1030        self.0.write(" ").await
1031    }
1032}
1033
1034struct TypeArgAccept<'o, O> {
1035    inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
1036}
1037
1038impl<'o, O> TypeArgAccept<'o, O> {
1039    fn new(output: &'o mut O) -> Self {
1040        Self {
1041            inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
1042        }
1043    }
1044}
1045
1046impl<'o, O> TypeArgAccept<'o, O>
1047where
1048    O: Output,
1049{
1050    async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
1051    where
1052        TypeArgs: WritableSeq<O>,
1053    {
1054        type_args.for_each(&mut self.inner).await?;
1055        if self.inner.wrote_any {
1056            self.inner.output.write(">").await?;
1057        }
1058        Ok(())
1059    }
1060}
1061
1062fn type_arg_bound_accept<'o, O>(
1063    output: &'o mut O,
1064) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
1065    SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
1066}
1067
1068fn struct_fields_accept<'o, O>(
1069    output: &'o mut O,
1070) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
1071    SeparatedSeqAccept::new(output, Str("\n"), Str(",\n"))
1072}
1073
1074fn where_cond_accept<'o, O>(
1075    output: &'o mut O,
1076) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
1077    SeparatedSeqAccept::new(output, Str(" where "), Str(", "))
1078}