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