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