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