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 where O: Output {
168    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
169        output.write("unsafe").await
170    }
171}
172
173impl<O, Abi> Writable<O> for ModUnsafeExtern<Abi>
174where
175    O: Output,
176    Abi: Writable<O>,
177{
178    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
179        output.write("unsafe extern \"").await?;
180        self.0.write_to(output).await?;
181        output.write("\"").await
182    }
183}
184
185impl<O, Abi, Body> Writable<O> for ExternBlock<Abi, Body>
186where
187    O: Output,
188    O::Ctx: ContextProvides<Edition>,
189    Abi: Writable<O>,
190    Body: Writable<O>,
191{
192    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
193        let edition = output.get_ctx();
194        if edition >= &Edition::Rust2024 {
195            output.write("unsafe ").await?;
196        }
197        output.write("extern \"").await?;
198        self.abi.write_to(output).await?;
199        output.write("\" {").await?;
200        self.body.write_to(output).await?;
201        output.write("}").await
202    }
203}
204
205impl<O, Name, Body> Writable<O> for ModBlock<Name, Body>
206where
207    O: Output,
208    Name: Writable<O>,
209    Body: Writable<O>,
210{
211    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
212        output.write("mod ").await?;
213        self.name.write_to(output).await?;
214        output.write(" {").await?;
215        self.body.write_to(output).await?;
216        output.write("}\n").await
217    }
218}
219
220impl<O, Cond, Body> Writable<O> for IfBlock<Cond, Body>
221where
222    O: Output,
223    Cond: Writable<O>,
224    Body: Writable<O>,
225{
226    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
227        output.write("if ").await?;
228        self.0.write_to(output).await?;
229        output.write(" {\n").await?;
230        self.1.write_to(output).await?;
231        output.write("\n}").await
232    }
233}
234
235impl<O, Before, After> Writable<O> for Else<Before, After>
236where
237    O: Output,
238    Before: Writable<O>,
239    After: Writable<O>,
240{
241    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
242        self.0.write_to(output).await?;
243        output.write(" else ").await?;
244        self.1.write_to(output).await
245    }
246}
247
248impl<O, Body> Writable<O> for Block<Body>
249where
250    O: Output,
251    Body: Writable<O>,
252{
253    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
254        output.write("{\n").await?;
255        self.0.write_to(output).await?;
256        output.write("\n}").await
257    }
258}
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
320impl<O, Expr> Writable<O> for Stmt<Expr>
321where
322    O: Output,
323    Expr: Writable<O>,
324{
325    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
326        self.0.write_to(output).await?;
327        output.write(";\n").await
328    }
329}
330
331impl<O: Output, Variable, Expr> Writable<O> for LetStmt<Variable, Expr>
332where
333    Variable: Writable<O>,
334    Expr: Writable<O>,
335{
336    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
337        Stmt(LetExpr(&self.0, &self.1)).write_to(o).await
338    }
339}
340
341impl<O: Output, Variable, Expr> Writable<O> for AssignStmt<Variable, Expr>
342where
343    Variable: Writable<O>,
344    Expr: Writable<O>,
345{
346    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
347        Stmt(AssignExpr(&self.0, &self.1)).write_to(o).await
348    }
349}
350
351impl<O, Expr> Writable<O> for ReturnStmt<Expr> where O: Output, Expr: Writable<O> {
352    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
353        o.write("return ").await?;
354        self.0.write_to(o).await?;
355        o.write(";\n").await
356    }
357}
358
359impl<O, Pattern, Expr> Writable<O> for LetExpr<Pattern, Expr>
360where
361    O: Output,
362    Pattern: Writable<O>,
363    Expr: Writable<O>,
364{
365    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
366        output.write("let ").await?;
367        AssignExpr(&self.0, &self.1).write_to(output).await
368    }
369}
370
371struct AssignExpr<Variable, Expr>(Variable, Expr);
372
373impl<O, Variable, Expr> Writable<O> for AssignExpr<Variable, Expr>
374where
375    O: Output,
376    Variable: Writable<O>,
377    Expr: Writable<O>,
378{
379    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
380        self.0.write_to(o).await?;
381        o.write(" = ").await?;
382        self.1.write_to(o).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        Ok(())
419    }
420}
421
422impl<O: Output, Expr> Writable<O> for OkResultOf<Expr>
423where
424    Expr: Writable<O>,
425{
426    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
427        o.write("Ok(").await?;
428        self.0.write_to(o).await?;
429        o.write(")").await?;
430        Ok(())
431    }
432}
433
434impl<O: Output, Type, Trait> Writable<O> for TypeAsTrait<Type, Trait>
435where
436    Type: Writable<O>,
437    Trait: Writable<O>,
438{
439    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
440        o.write("<").await?;
441        self.0.write_to(o).await?;
442        o.write(" as ").await?;
443        self.1.write_to(o).await?;
444        o.write(">").await?;
445        Ok(())
446    }
447}
448
449impl<O, Mods, Name, Args, Return, Where, Body> Writable<O>
450    for FunctionDef<Mods, Name, Args, Return, Where, Body>
451where
452    O: Output,
453    Mods: WritableSeq<O>,
454    Name: Writable<O>,
455    Args: WritableSeq<O>,
456    Return: Writable<O>,
457    Where: WritableSeq<O>,
458    Body: Writable<O>,
459{
460    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
461        self.mods.for_each(&mut ModsAccept(o)).await?;
462        o.write("fn ").await?;
463        self.name.write_to(o).await?;
464        AnonTuple(&self.args).write_to(o).await?;
465        o.write(" -> ").await?;
466        self.return_type.write_to(o).await?;
467        self.where_conds.for_each(&mut where_cond_accept(o)).await?;
468        self.body.write_to(o).await?;
469        o.write("\n").await?;
470        Ok(())
471    }
472}
473
474impl<O: Output> Writable<O> for FunctionBodyDeclare {
475    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
476        output.write(";").await
477    }
478}
479
480impl<O: Output, Inner> Writable<O> for FunctionBodyImplement<Inner>
481where
482    Inner: Writable<O>,
483{
484    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
485        Block(&self.0).write_to(output).await
486    }
487}
488
489impl<O, Args, Return> Writable<O> for FunctionPtr<Args, Return>
490where
491    O: Output,
492    Args: WritableSeq<O>,
493    Return: Writable<O>,
494{
495    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
496        let kind = match self.kind {
497            FunctionPtrKind::FnPtr => "fn",
498            FunctionPtrKind::Fn => "Fn",
499            FunctionPtrKind::FnMut => "FnMut",
500            FunctionPtrKind::FnOnce => "FnOnce",
501        };
502        output.write(kind).await?;
503        AnonTuple(&self.args).write_to(output).await?;
504        output.write(" -> ").await?;
505        self.return_type.write_to(output).await
506    }
507}
508
509impl<O: Output, Type, Value> Writable<O> for AssociatedTypeEquals<Type, Value>
510where
511    Type: Writable<O>,
512    Value: Writable<O>,
513{
514    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
515        self.0.write_to(o).await?;
516        o.write("=").await?;
517        self.1.write_to(o).await
518    }
519}
520
521impl<O: Output, Type> Writable<O> for DynOf<Type>
522where
523    Type: Writable<O>,
524{
525    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
526        o.write("dyn ").await?;
527        self.0.write_to(o).await
528    }
529}
530
531impl<O: Output, Type> Writable<O> for RefOf<Type>
532where
533    Type: Writable<O>,
534{
535    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
536        o.write("&").await?;
537        self.0.write_to(o).await
538    }
539}
540
541impl<O: Output, Type> Writable<O> for ImplOf<Type>
542where
543    Type: Writable<O>,
544{
545    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
546        o.write("impl ").await?;
547        self.0.write_to(o).await
548    }
549}
550
551impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
552where
553    Type: Writable<O>,
554{
555    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
556        o.write("&'").await?;
557        o.write(self.0).await?;
558        o.write(" ").await?;
559        self.1.write_to(o).await
560    }
561}
562
563impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
564where
565    VarName: Writable<O>,
566    Value: Writable<O>,
567{
568    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
569        o.write("\ntype ").await?;
570        self.0.write_to(o).await?;
571        o.write(" = ").await?;
572        self.1.write_to(o).await?;
573        o.write(";\n").await?;
574        Ok(())
575    }
576}
577
578impl<O, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
579    for TraitDef<Mods, Name, TypeVars, SuperTraits, Body>
580where
581    O: Output,
582    Mods: WritableSeq<O>,
583    Name: Writable<O>,
584    TypeVars: WritableSeq<O>,
585    SuperTraits: WritableSeq<O>,
586    Body: Writable<O>,
587{
588    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
589        self.mods.for_each(&mut ModsAccept(o)).await?;
590        o.write("trait ").await?;
591        self.name.write_to(o).await?;
592        TypeArgAccept::new(o)
593            .write_inside_diamond(&self.type_variables)
594            .await?;
595        self.super_traits
596            .for_each(&mut type_arg_bound_accept(o))
597            .await?;
598        o.write(" {").await?;
599        self.body.write_to(o).await?;
600        o.write("}\n\n").await?;
601        Ok(())
602    }
603}
604
605impl<O, Mods, TypeVars, Trait, Recv, Where, Body> Writable<O>
606    for TraitImpl<Mods, TypeVars, Trait, Recv, Where, Body>
607where
608    O: Output,
609    Mods: WritableSeq<O>,
610    TypeVars: WritableSeq<O>,
611    Trait: Writable<O>,
612    Recv: Writable<O>,
613    Where: WritableSeq<O>,
614    Body: Writable<O>,
615{
616    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
617        self.mods.for_each(&mut ModsAccept(o)).await?;
618        o.write("impl").await?;
619        TypeArgAccept::new(o)
620            .write_inside_diamond(&self.type_variables)
621            .await?;
622        o.write(" ").await?;
623        self.the_trait.write_to(o).await?;
624        o.write(" for ").await?;
625        self.receiver.write_to(o).await?;
626        self.where_conds.for_each(&mut where_cond_accept(o)).await?;
627        o.write(" {").await?;
628        self.body.write_to(o).await?;
629        o.write("}\n\n").await?;
630        Ok(())
631    }
632}
633
634impl<O, Mods, Name, Elements> Writable<O> for StructDef<Mods, Name, Elements>
635where
636    O: Output,
637    Mods: WritableSeq<O>,
638    Name: Writable<O>,
639    Elements: WritableSeq<O>,
640{
641    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
642        self.mods.for_each(&mut ModsAccept(o)).await?;
643        o.write("struct ").await?;
644        match &self.kind {
645            StructKind::Tuple(name, elements) => {
646                NamedTuple {
647                    name,
648                    args: elements,
649                }
650                .write_to(o)
651                .await?;
652                o.write(";").await
653            }
654            StructKind::NamedFields(name, elements) => {
655                StructCall {
656                    name,
657                    body: StructFields(elements),
658                }
659                .write_to(o)
660                .await
661            }
662        }?;
663        o.write("\n\n").await
664    }
665}
666
667impl<O, Name, Body> Writable<O> for StructCall<Name, Body>
668where
669    O: Output,
670    Name: Writable<O>,
671    Body: Writable<O>,
672{
673    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
674        self.name.write_to(o).await?;
675        o.write(" { ").await?;
676        self.body.write_to(o).await?;
677        o.write(" }").await
678    }
679}
680
681impl<O, Fields> Writable<O> for StructFields<Fields>
682where
683    O: Output,
684    Fields: WritableSeq<O>,
685{
686    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
687        self.0.for_each(&mut struct_fields_accept(output)).await
688    }
689}
690
691impl<O, Name, Value> Writable<O> for DeclareField<Name, Value>
692where
693    O: Output,
694    Name: Writable<O>,
695    Value: Writable<O>,
696{
697    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
698        FunctionParam(&self.0, &self.1).write_to(o).await
699    }
700}
701
702impl<O, Name, Args> Writable<O> for NamedTuple<Name, Args>
703where
704    O: Output,
705    Name: Writable<O>,
706    Args: WritableSeq<O>,
707{
708    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
709        self.name.write_to(output).await?;
710        // Only write the parentheses if arguments is non-empty
711        let mut separator = SeparatedSeqAccept::new(output, Str("("), Str(", "));
712        self.args.for_each(&mut separator).await?;
713        if separator.wrote_any {
714            output.write(")").await?;
715        }
716        Ok(())
717    }
718}
719
720impl<O, Args> Writable<O> for AnonTuple<Args>
721where
722    O: Output,
723    Args: WritableSeq<O>,
724{
725    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
726        o.write("(").await?;
727        self.0
728            .for_each(&mut SeparatedSeqAccept::comma_separated(o))
729            .await?;
730        o.write(")").await
731    }
732}
733
734impl<O, Attr, Value> Writable<O> for WithAttributes<Attr, Value>
735where
736    O: Output,
737    Attr: WritableSeq<O>,
738    Value: Writable<O>,
739{
740    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
741        self.attr
742            .for_each(&mut AttributesAccept::with_separator(
743                output,
744                self.separator,
745            ))
746            .await?;
747        self.value.write_to(output).await
748    }
749}
750
751impl<O, Mods, Name, Entries> Writable<O> for EnumDef<Mods, Name, Entries>
752where
753    O: Output,
754    Mods: WritableSeq<O>,
755    Name: Writable<O>,
756    Entries: WritableSeq<O>,
757{
758    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
759        self.mods.for_each(&mut ModsAccept(output)).await?;
760        output.write("enum ").await?;
761        self.name.write_to(output).await?;
762        output.write(" {").await?;
763        self.entries
764            .for_each(&mut SeparatedSeqAccept::comma_separated(output))
765            .await?;
766        output.write("}\n\n").await
767    }
768}
769
770impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
771where
772    O: Output,
773    Name: Writable<O>,
774    TypeArgs: WritableSeq<O>,
775{
776    async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
777        self.0.write_to(o).await?;
778        TypeArgAccept::new(o)
779            .write_inside_diamond(&self.1)
780            .await
781    }
782}
783
784impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
785where
786    O: Output,
787    TypeVar: Writable<O>,
788    Bounds: WritableSeq<O>,
789{
790    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
791        self.0.write_to(output).await?;
792        self.1.for_each(&mut type_arg_bound_accept(output)).await
793    }
794}
795
796impl<O> Writable<O> for Lifetime<'_>
797where
798    O: Output,
799{
800    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
801        output.write("'").await?;
802        output.write(self.0).await
803    }
804}
805
806impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
807where
808    O: Output,
809    Name: Writable<O>,
810    Type: Writable<O>,
811{
812    async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
813        self.0.write_to(output).await?;
814        output.write(": ").await?;
815        self.1.write_to(output).await
816    }
817}
818
819//
820// Sequence acceptors
821//
822
823struct ModsAccept<'o, O>(&'o mut O);
824
825impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
826    async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
827    where
828        W: Writable<O>,
829    {
830        writable.write_to(&mut self.0).await?;
831        self.0.write(" ").await
832    }
833}
834
835struct TypeArgAccept<'o, O> {
836    inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
837}
838
839impl<'o, O> TypeArgAccept<'o, O> {
840    fn new(output: &'o mut O) -> Self {
841        Self {
842            inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
843        }
844    }
845}
846
847impl<'o, O> TypeArgAccept<'o, O>
848where
849    O: Output,
850{
851    async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
852    where
853        TypeArgs: WritableSeq<O>,
854    {
855        type_args.for_each(&mut self.inner).await?;
856        if self.inner.wrote_any {
857            self.inner.output.write(">").await?;
858        }
859        Ok(())
860    }
861}
862
863fn type_arg_bound_accept<'o, O>(
864    output: &'o mut O,
865) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
866    SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
867}
868
869fn struct_fields_accept<'o, O>(
870    output: &'o mut O,
871) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
872    SeparatedSeqAccept::new(output, Str("\n"), Str(",\n"))
873}
874
875fn where_cond_accept<'o, O>(
876    output: &'o mut O,
877) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
878    SeparatedSeqAccept::new(output, Str(" where "), Str(", "))
879}