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