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