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