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, Default, Count> Writable<O> for ArrayInit<Default, Count>
400where
401    Default: Writable<O>,
402    Count: Writable<O>,
403{
404    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
405        o.write("[").await?;
406        self.0.write_to(o).await?;
407        o.write("; ").await?;
408        self.1.write_to(o).await?;
409        o.write("]").await
410    }
411}
412
413impl<O: Output, Cont, Item> Writable<O> for AssociatedItem<Cont, Item>
414where
415    Cont: Writable<O>,
416    Item: Writable<O>,
417{
418    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
419        self.0.write_to(o).await?;
420        o.write("::").await?;
421        self.1.write_to(o).await
422    }
423}
424
425impl<O: Output, Expr> Writable<O> for QuestionMarkAfter<Expr>
426where
427    Expr: Writable<O>,
428{
429    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
430        self.0.write_to(o).await?;
431        o.write("?").await
432    }
433}
434
435impl<O: Output, Type, Trait> Writable<O> for TypeAsTrait<Type, Trait>
436where
437    Type: Writable<O>,
438    Trait: Writable<O>,
439{
440    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
441        o.write("<").await?;
442        TypeAsType(&self.0, &self.1).write_to(o).await?;
443        o.write(">").await
444    }
445}
446
447impl<O, Type1, Type2> Writable<O> for TypeAsType<Type1, Type2>
448where
449    O: Output,
450    Type1: Writable<O>,
451    Type2: Writable<O>,
452{
453    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
454        self.0.write_to(o).await?;
455        o.write(" as ").await?;
456        self.1.write_to(o).await
457    }
458}
459
460impl<O, Mods, Name, Args, Return, Where, Body> Writable<O>
461    for FunctionDef<Mods, Name, Args, Return, Where, Body>
462where
463    O: Output,
464    Mods: WritableSeq<O>,
465    Name: Writable<O>,
466    Args: WritableSeq<O>,
467    Return: Writable<O>,
468    Where: WritableSeq<O>,
469    Body: Writable<O>,
470{
471    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
472        self.mods.for_each(&mut ModsAccept(o)).await?;
473        o.write("fn ").await?;
474        self.name.write_to(o).await?;
475        AnonTuple(&self.args).write_to(o).await?;
476        o.write(" -> ").await?;
477        self.return_type.write_to(o).await?;
478        self.where_conds.for_each(&mut where_cond_accept(o)).await?;
479        self.body.write_to(o).await?;
480        o.write("\n\n").await
481    }
482}
483
484impl<O: Output> Writable<O> for FunctionBodyDeclare {
485    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
486        output.write(";").await
487    }
488}
489
490impl<O: Output, Inner> Writable<O> for FunctionBodyImplement<Inner>
491where
492    Inner: Writable<O>,
493{
494    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
495        output.write(" ").await?;
496        Block(&self.0).write_to(output).await
497    }
498}
499
500impl<O, Args, Return> Writable<O> for FunctionPtr<Args, Return>
501where
502    O: Output,
503    Args: WritableSeq<O>,
504    Return: Writable<O>,
505{
506    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
507        let kind = match self.kind {
508            FunctionPtrKind::FnPtr => "fn",
509            FunctionPtrKind::Fn => "Fn",
510            FunctionPtrKind::FnMut => "FnMut",
511            FunctionPtrKind::FnOnce => "FnOnce",
512        };
513        output.write(kind).await?;
514        AnonTuple(&self.args).write_to(output).await?;
515        output.write(" -> ").await?;
516        self.return_type.write_to(output).await
517    }
518}
519
520impl<O: Output, Type, Value> Writable<O> for AssociatedTypeEquals<Type, Value>
521where
522    Type: Writable<O>,
523    Value: Writable<O>,
524{
525    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
526        AssignExpr(&self.0, &self.1).write_to(o).await
527    }
528}
529
530impl<O: Output, Type> Writable<O> for DynOf<Type>
531where
532    Type: Writable<O>,
533{
534    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
535        o.write("dyn ").await?;
536        self.0.write_to(o).await
537    }
538}
539
540impl<O: Output, Expr> Writable<O> for RefOf<Expr>
541where
542    Expr: Writable<O>,
543{
544    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
545        o.write("&").await?;
546        self.0.write_to(o).await
547    }
548}
549
550impl<O: Output, Expr> Writable<O> for RefMutOf<Expr>
551where
552    Expr: Writable<O>,
553{
554    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
555        o.write("&mut ").await?;
556        self.0.write_to(o).await
557    }
558}
559
560impl<O, Expr> Writable<O> for Dereference<Expr>
561where
562    O: Output,
563    Expr: Writable<O>,
564{
565    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
566        output.write("*").await?;
567        self.0.write_to(output).await
568    }
569}
570
571impl<O, Expr> Writable<O> for ParenthesesAround<Expr>
572where
573    O: Output,
574    Expr: Writable<O>,
575{
576    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
577        output.write("(").await?;
578        self.0.write_to(output).await?;
579        output.write(")").await
580    }
581}
582
583impl<O: Output, Type> Writable<O> for ImplOf<Type>
584where
585    Type: Writable<O>,
586{
587    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
588        o.write("impl ").await?;
589        self.0.write_to(o).await
590    }
591}
592
593impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
594where
595    Type: Writable<O>,
596{
597    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
598        o.write("&'").await?;
599        o.write(self.0).await?;
600        o.write(" ").await?;
601        self.1.write_to(o).await
602    }
603}
604
605impl<O, Expr> Writable<O> for RawConstOf<Expr>
606where
607    O: Output,
608    Expr: Writable<O>,
609{
610    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
611        output.write("&raw const ").await?;
612        self.0.write_to(output).await
613    }
614}
615
616impl<O, Expr> Writable<O> for RawMutOf<Expr>
617where
618    O: Output,
619    Expr: Writable<O>,
620{
621    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
622        output.write("&raw mut ").await?;
623        self.0.write_to(output).await
624    }
625}
626
627impl<O, Expr> Writable<O> for ConstPtr<Expr>
628where
629    O: Output,
630    Expr: Writable<O>,
631{
632    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
633        output.write("*const ").await?;
634        self.0.write_to(output).await
635    }
636}
637
638impl<O, Expr> Writable<O> for MutPtr<Expr>
639where
640    O: Output,
641    Expr: Writable<O>,
642{
643    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
644        output.write("*mut ").await?;
645        self.0.write_to(output).await
646    }
647}
648
649impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
650where
651    VarName: Writable<O>,
652    Value: Writable<O>,
653{
654    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
655        o.write("type ").await?;
656        self.0.write_to(o).await?;
657        o.write(" = ").await?;
658        self.1.write_to(o).await?;
659        o.write(";\n\n").await?;
660        Ok(())
661    }
662}
663
664impl<O, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
665    for TraitDef<Mods, Name, TypeVars, SuperTraits, Body>
666where
667    O: Output,
668    Mods: WritableSeq<O>,
669    Name: Writable<O>,
670    TypeVars: WritableSeq<O>,
671    SuperTraits: WritableSeq<O>,
672    Body: Writable<O>,
673{
674    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
675        self.mods.for_each(&mut ModsAccept(o)).await?;
676        o.write("trait ").await?;
677        self.name.write_to(o).await?;
678        TypeArgAccept::new(o)
679            .write_in_diamond(&self.type_variables)
680            .await?;
681        self.super_traits
682            .for_each(&mut type_arg_bound_accept(o))
683            .await?;
684        o.write(" {").await?;
685        self.body.write_to(o).await?;
686        o.write("}\n\n").await?;
687        Ok(())
688    }
689}
690
691impl<O, Mods, TypeVars, Trait, Recv, Where, Body> Writable<O>
692    for TraitImpl<Mods, TypeVars, Trait, Recv, Where, Body>
693where
694    O: Output,
695    Mods: WritableSeq<O>,
696    TypeVars: WritableSeq<O>,
697    Trait: Writable<O>,
698    Recv: Writable<O>,
699    Where: WritableSeq<O>,
700    Body: Writable<O>,
701{
702    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
703        let impl_block = Impl {
704            mods: &self.mods,
705            type_variables: &self.type_variables,
706            receiver: Combined(&self.the_trait, Combined(Str(" for "), &self.receiver)),
707            where_conds: &self.where_conds,
708            body: &self.body,
709        };
710        impl_block.write_to(output).await
711    }
712}
713
714impl<O, Mods, TypeVars, Recv, Where, Body> Writable<O> for Impl<Mods, TypeVars, Recv, Where, Body>
715where
716    O: Output,
717    Mods: WritableSeq<O>,
718    TypeVars: WritableSeq<O>,
719    Recv: Writable<O>,
720    Where: WritableSeq<O>,
721    Body: Writable<O>,
722{
723    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
724        self.mods.for_each(&mut ModsAccept(o)).await?;
725        o.write("impl").await?;
726        TypeArgAccept::new(o)
727            .write_in_diamond(&self.type_variables)
728            .await?;
729        o.write(" ").await?;
730        self.receiver.write_to(o).await?;
731        self.where_conds.for_each(&mut where_cond_accept(o)).await?;
732        o.write(" {").await?;
733        self.body.write_to(o).await?;
734        o.write("}\n\n").await?;
735        Ok(())
736    }
737}
738
739impl<O, Mods, Name, Elements> Writable<O> for StructDef<Mods, Name, Elements>
740where
741    O: Output,
742    Mods: WritableSeq<O>,
743    Name: Writable<O>,
744    Elements: WritableSeq<O>,
745{
746    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
747        self.mods.for_each(&mut ModsAccept(o)).await?;
748        o.write("struct ").await?;
749        match &self.kind {
750            StructKind::Tuple(name, elements) => {
751                NamedTuple {
752                    name,
753                    args: elements,
754                }
755                .write_to(o)
756                .await?;
757                o.write(";").await
758            }
759            StructKind::NamedFields(name, elements) => {
760                StructCall {
761                    name,
762                    body: StructFields(elements),
763                }
764                .write_to(o)
765                .await
766            }
767        }?;
768        o.write("\n\n").await
769    }
770}
771
772impl<O, Name, Body> Writable<O> for StructCall<Name, Body>
773where
774    O: Output,
775    Name: Writable<O>,
776    Body: Writable<O>,
777{
778    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
779        self.name.write_to(o).await?;
780        o.write(" { ").await?;
781        self.body.write_to(o).await?;
782        o.write(" }").await
783    }
784}
785
786impl<O, Fields> Writable<O> for StructFields<Fields>
787where
788    O: Output,
789    Fields: WritableSeq<O>,
790{
791    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
792        self.0.for_each(&mut struct_fields_accept(output)).await
793    }
794}
795
796impl<O, Mods, Name, FieldType> Writable<O> for DeclareField<Mods, Name, FieldType>
797where
798    O: Output,
799    Mods: WritableSeq<O>,
800    Name: Writable<O>,
801    FieldType: Writable<O>,
802{
803    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
804        self.mods.for_each(&mut ModsAccept(output)).await?;
805        FunctionParam(&self.name, &self.field_type)
806            .write_to(output)
807            .await
808    }
809}
810
811impl<O, Name, Value> Writable<O> for FillOutField<Name, Value>
812where
813    O: Output,
814    Name: Writable<O>,
815    Value: Writable<O>,
816{
817    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
818        FunctionParam(&self.0, &self.1).write_to(output).await
819    }
820}
821
822impl<O, Name, Args> Writable<O> for NamedTuple<Name, Args>
823where
824    O: Output,
825    Name: Writable<O>,
826    Args: WritableSeq<O>,
827{
828    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
829        self.name.write_to(output).await?;
830        // Only write the parentheses if arguments is non-empty
831        let mut separator = SeparatedSeqAccept::new(output, Str("("), Str(", "));
832        self.args.for_each(&mut separator).await?;
833        if separator.wrote_any {
834            output.write(")").await?;
835        }
836        Ok(())
837    }
838}
839
840impl<O, Args> Writable<O> for AnonTuple<Args>
841where
842    O: Output,
843    Args: WritableSeq<O>,
844{
845    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
846        o.write("(").await?;
847        self.0
848            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
849            .await?;
850        o.write(")").await
851    }
852}
853
854impl<O, Attr, Value> Writable<O> for WithAttributes<Attr, Value>
855where
856    O: Output,
857    Attr: WritableSeq<O>,
858    Value: Writable<O>,
859{
860    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
861        self.attr
862            .for_each(&mut AttributesAccept::with_separator(
863                output,
864                self.separator,
865            ))
866            .await?;
867        self.value.write_to(output).await
868    }
869}
870
871impl<O> Writable<O> for AttributeSeparator
872where
873    O: Output,
874{
875    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
876        output
877            .write(match self {
878                AttributeSeparator::Space => " ",
879                AttributeSeparator::NewLine => "\n",
880            })
881            .await
882    }
883}
884
885impl<O, Mods, Name, Entries> Writable<O> for EnumDef<Mods, Name, Entries>
886where
887    O: Output,
888    Mods: WritableSeq<O>,
889    Name: Writable<O>,
890    Entries: WritableSeq<O>,
891{
892    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
893        self.mods.for_each(&mut ModsAccept(output)).await?;
894        output.write("enum ").await?;
895        self.name.write_to(output).await?;
896        output.write(" {").await?;
897        self.entries
898            .for_each(&mut SeparatedSeqAccept::comma_separated(output))
899            .await?;
900        output.write("}\n\n").await
901    }
902}
903
904delegate_tuple_ws!(Parameterized);
905
906impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
907where
908    O: Output,
909    TypeVar: Writable<O>,
910    Bounds: WritableSeq<O>,
911{
912    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
913        self.0.write_to(output).await?;
914        self.1.for_each(&mut type_arg_bound_accept(output)).await
915    }
916}
917
918impl<O> Writable<O> for Lifetime<'_>
919where
920    O: Output,
921{
922    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
923        output.write("'").await?;
924        output.write(self.0).await
925    }
926}
927
928impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
929where
930    O: Output,
931    Name: Writable<O>,
932    Type: Writable<O>,
933{
934    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
935        self.0.write_to(output).await?;
936        output.write(": ").await?;
937        self.1.write_to(output).await
938    }
939}
940
941//
942// Sequence acceptors
943//
944
945fn type_arg_bound_accept<'o, O>(
946    output: &'o mut O,
947) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
948    SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
949}
950
951fn struct_fields_accept<'o, O>(
952    output: &'o mut O,
953) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
954    SeparatedSeqAccept::new(output, Str("\n"), Str(",\n"))
955}
956
957fn where_cond_accept<'o, O>(
958    output: &'o mut O,
959) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
960    SeparatedSeqAccept::new(output, Str(" where "), Str(", "))
961}