1use super::*;
18use crate::common::{SeparatedSeqAccept, Str};
19use crate::context::ContextProvides;
20use crate::{Output, SequenceAccept, Writable, WritableSeq};
21
22impl<O, Cond, Attr> Writable<O> for CfgAttr<Cond, Attr>
23where
24 O: Output,
25 Cond: Writable<O>,
26 Attr: Writable<O>,
27{
28 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
29 output.write("cfg_attr(").await?;
30 self.0.write_to(output).await?;
31 output.write(", ").await?;
32 self.1.write_to(output).await?;
33 output.write(")").await
34 }
35}
36
37impl<O> Writable<O> for NoMangle
38where
39 O: Output,
40 O::Ctx: ContextProvides<Edition>,
41{
42 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
43 let edition = output.get_ctx();
44 output
45 .write(if edition >= &Edition::Rust2024 {
46 "unsafe(no_mangle)"
47 } else {
48 "no_mangle"
49 })
50 .await
51 }
52}
53
54impl<O, Lints> Writable<O> for AllowLints<Lints>
55where
56 O: Output,
57 Lints: WritableSeq<O>,
58{
59 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
60 let mut acceptor = SeparatedSeqAccept::new(output, Str("allow("), Str(","));
61 self.0.for_each(&mut acceptor).await?;
62 if acceptor.wrote_any {
63 output.write(")").await?;
64 }
65 Ok(())
66 }
67}
68
69impl<O, Msg, Since> Writable<O> for Deprecated<Msg, Since>
70where
71 O: Output,
72 Msg: Writable<O>,
73 Since: Writable<O>,
74{
75 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
76 match self {
77 Self::Basic => output.write("deprecated").await,
78 Self::Message(msg) => {
79 output.write("deprecated = \"").await?;
80 msg.write_to(output).await?;
81 output.write("\"").await
82 }
83 Self::Full { since, note } => {
84 output.write("deprecated(since = \"").await?;
85 since.write_to(output).await?;
86 output.write("\", note = \"").await?;
87 note.write_to(output).await?;
88 output.write("\")").await
89 }
90 }
91 }
92}
93
94impl<O> Writable<O> for MustUse
95where
96 O: Output,
97{
98 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
99 output.write("must_use").await
100 }
101}
102
103impl<O> Writable<O> for ModPub
104where
105 O: Output,
106{
107 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
108 output.write("pub").await
109 }
110}
111
112impl<O, Abi> Writable<O> for ModUnsafeExtern<Abi>
113where
114 O: Output,
115 Abi: Writable<O>,
116{
117 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
118 output.write("unsafe extern \"").await?;
119 self.0.write_to(output).await?;
120 output.write("\"").await
121 }
122}
123
124impl<O, Attr, Abi, Body> Writable<O> for ExternBlock<Attr, Abi, Body>
125where
126 O: Output,
127 O::Ctx: ContextProvides<Edition>,
128 Attr: WritableSeq<O>,
129 Abi: Writable<O>,
130 Body: Writable<O>,
131{
132 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
133 self.attr.for_each(&mut AttributesAccept::new(output)).await?;
134 let edition = output.get_ctx();
135 if edition >= &Edition::Rust2024 {
136 output.write("unsafe ").await?;
137 }
138 output.write("extern \"").await?;
139 self.abi.write_to(output).await?;
140 output.write("\" {").await?;
141 self.body.write_to(output).await?;
142 output.write("}").await
143 }
144}
145
146impl<O: Output, Expr> Writable<O> for UnsafeBlock<Expr>
147where
148 Expr: Writable<O>,
149{
150 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
151 o.write("unsafe {\n").await?;
152 self.0.write_to(o).await?;
153 o.write("\n}").await?;
154 Ok(())
155 }
156}
157
158impl<O, InputVars, Expr> Writable<O> for Closure<InputVars, Expr>
159where
160 O: Output,
161 InputVars: WritableSeq<O>,
162 Expr: Writable<O>,
163{
164 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
165 o.write("|").await?;
166 self.input_vars
167 .for_each(&mut SeparatedSeqAccept::comma_separated(o))
168 .await?;
169 o.write("| {\n").await?;
170 self.inside_block.write_to(o).await?;
171 o.write("\n}").await?;
172 Ok(())
173 }
174}
175
176impl<O: Output, Recv, FuncName, Args> Writable<O> for FunctionCall<Recv, FuncName, Args>
177where
178 Recv: Writable<O>,
179 FuncName: Writable<O>,
180 Args: WritableSeq<O>,
181{
182 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
183 self.receiver.write_to(o).await?;
184 o.write(if self.is_assoc { "::" } else { "." }).await?;
185 self.function.write_to(o).await
186 }
187}
188
189impl<O: Output, Variable, Expr> Writable<O> for LetStmt<Variable, Expr>
190where
191 Variable: Writable<O>,
192 Expr: Writable<O>,
193{
194 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
195 o.write("let ").await?;
196 self.0.write_to(o).await?;
197 o.write(" = ").await?;
198 self.1.write_to(o).await?;
199 o.write(";\n").await?;
200 Ok(())
201 }
202}
203
204impl<O: Output, Cont, Item> Writable<O> for AssociatedItem<Cont, Item>
205where
206 Cont: Writable<O>,
207 Item: Writable<O>,
208{
209 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
210 self.0.write_to(o).await?;
211 o.write("::").await?;
212 self.1.write_to(o).await
213 }
214}
215
216impl<O: Output, Expr> Writable<O> for QuestionMarkAfter<Expr>
217where
218 Expr: Writable<O>,
219{
220 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
221 self.0.write_to(o).await?;
222 o.write("?").await?;
223 Ok(())
224 }
225}
226
227impl<O: Output, Expr> Writable<O> for OkResultOf<Expr>
228where
229 Expr: Writable<O>,
230{
231 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
232 o.write("Ok(").await?;
233 self.0.write_to(o).await?;
234 o.write(")").await?;
235 Ok(())
236 }
237}
238
239impl<O: Output, Type, Trait> Writable<O> for TypeAsTrait<Type, Trait>
240where
241 Type: Writable<O>,
242 Trait: Writable<O>,
243{
244 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
245 o.write("<").await?;
246 self.0.write_to(o).await?;
247 o.write(" as ").await?;
248 self.1.write_to(o).await?;
249 o.write(">").await?;
250 Ok(())
251 }
252}
253
254impl<O, Attr, Mods, Name, Args, Return, Body> Writable<O>
255 for FunctionDef<Attr, Mods, Name, Args, Return, Body>
256where
257 O: Output,
258 Attr: WritableSeq<O>,
259 Mods: WritableSeq<O>,
260 Name: Writable<O>,
261 Args: WritableSeq<O>,
262 Return: Writable<O>,
263 Body: Writable<O>,
264{
265 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
266 self.attr.for_each(&mut AttributesAccept::new(o)).await?;
267 self.mods.for_each(&mut ModsAccept(o)).await?;
268 o.write("fn ").await?;
269 self.decl.write_to(o).await?;
270 o.write(" -> ").await?;
271 self.return_type.write_to(o).await?;
272 self.body.write_to(o).await?;
273 o.write("\n").await?;
274 Ok(())
275 }
276}
277
278impl<O, Name, Args> Writable<O> for Function<Name, Args>
279where
280 O: Output,
281 Name: Writable<O>,
282 Args: WritableSeq<O>,
283{
284 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
285 self.name.write_to(output).await?;
286 output.write("(").await?;
287 self.args
288 .for_each(&mut SeparatedSeqAccept::comma_separated(output))
289 .await?;
290 output.write(")").await
291 }
292}
293
294impl<O: Output> Writable<O> for FunctionBodyDeclare {
295 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
296 output.write(";").await
297 }
298}
299
300impl<O: Output, Type, Value> Writable<O> for AssociatedTypeEquals<Type, Value>
301where
302 Type: Writable<O>,
303 Value: Writable<O>,
304{
305 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
306 self.0.write_to(o).await?;
307 o.write("=").await?;
308 self.1.write_to(o).await
309 }
310}
311
312impl<O: Output, Type> Writable<O> for DynOf<Type>
313where
314 Type: Writable<O>,
315{
316 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
317 o.write("dyn ").await?;
318 self.0.write_to(o).await
319 }
320}
321
322impl<O: Output, Type> Writable<O> for RefOf<Type>
323where
324 Type: Writable<O>,
325{
326 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
327 o.write("&").await?;
328 self.0.write_to(o).await
329 }
330}
331
332impl<O: Output, Type> Writable<O> for ImplOf<Type>
333where
334 Type: Writable<O>,
335{
336 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
337 o.write("impl ").await?;
338 self.0.write_to(o).await
339 }
340}
341
342impl<O: Output, Type> Writable<O> for LifetimedRefOf<'_, Type>
343where
344 Type: Writable<O>,
345{
346 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
347 o.write("&'").await?;
348 o.write(self.0).await?;
349 o.write(" ").await?;
350 self.1.write_to(o).await
351 }
352}
353
354impl<O: Output, VarName, Value> Writable<O> for AssociatedTypeDef<VarName, Value>
355where
356 VarName: Writable<O>,
357 Value: Writable<O>,
358{
359 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
360 o.write("\ntype ").await?;
361 self.0.write_to(o).await?;
362 o.write(" = ").await?;
363 self.1.write_to(o).await?;
364 o.write(";\n").await?;
365 Ok(())
366 }
367}
368
369impl<O, Attr, Mods, Name, TypeVars, SuperTraits, Body> Writable<O>
370 for TraitDef<Attr, Mods, Name, TypeVars, SuperTraits, Body>
371where
372 O: Output,
373 Attr: WritableSeq<O>,
374 Mods: WritableSeq<O>,
375 Name: Writable<O>,
376 TypeVars: WritableSeq<O>,
377 SuperTraits: WritableSeq<O>,
378 Body: Writable<O>,
379{
380 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
381 self.attr.for_each(&mut AttributesAccept::new(o)).await?;
382 self.mods.for_each(&mut ModsAccept(o)).await?;
383 o.write("trait ").await?;
384 self.name.write_to(o).await?;
385 TypeArgAccept::new(o)
386 .write_inside_diamond(&self.type_variables)
387 .await?;
388 self.super_traits
389 .for_each(&mut type_arg_bound_accept(o))
390 .await?;
391 o.write(" {").await?;
392 self.body.write_to(o).await?;
393 o.write("}").await?;
394 Ok(())
395 }
396}
397
398impl<O, TypeVars, Trait, Recv, Body> Writable<O> for TraitImpl<TypeVars, Trait, Recv, Body>
399where
400 O: Output,
401 TypeVars: WritableSeq<O>,
402 Trait: Writable<O>,
403 Recv: Writable<O>,
404 Body: Writable<O>,
405{
406 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
407 o.write("impl").await?;
408 TypeArgAccept::new(o)
409 .write_inside_diamond(&self.type_variables)
410 .await?;
411 o.write(" ").await?;
412 self.the_trait.write_to(o).await?;
413 o.write(" for ").await?;
414 self.receiver.write_to(o).await?;
415 o.write(" {").await?;
416 self.body.write_to(o).await?;
417 o.write("}").await?;
418 Ok(())
419 }
420}
421
422impl<O, Name, TypeArgs> Writable<O> for Parameterized<Name, TypeArgs>
423where
424 O: Output,
425 Name: Writable<O>,
426 TypeArgs: WritableSeq<O>,
427{
428 async fn write_to(&self, o: &mut O) -> Result<(), O::Error> {
429 self.name.write_to(o).await?;
430 TypeArgAccept::new(o)
431 .write_inside_diamond(&self.type_args)
432 .await
433 }
434}
435
436impl<O, TypeVar, Bounds> Writable<O> for BoundedTypeVar<TypeVar, Bounds>
437where
438 O: Output,
439 TypeVar: Writable<O>,
440 Bounds: WritableSeq<O>,
441{
442 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
443 self.0.write_to(output).await?;
444 self.1.for_each(&mut type_arg_bound_accept(output)).await
445 }
446}
447
448impl<O> Writable<O> for Lifetime<'_>
449where
450 O: Output,
451{
452 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
453 output.write("'").await?;
454 output.write(self.0).await
455 }
456}
457
458impl<O, Name, Type> Writable<O> for FunctionParam<Name, Type>
459where
460 O: Output,
461 Name: Writable<O>,
462 Type: Writable<O>,
463{
464 async fn write_to(&self, output: &mut O) -> Result<(), O::Error> {
465 self.0.write_to(output).await?;
466 output.write(": ").await?;
467 self.1.write_to(output).await
468 }
469}
470
471struct ModsAccept<'o, O>(&'o mut O);
476
477impl<'o, O: Output> SequenceAccept<O> for ModsAccept<'o, O> {
478 async fn accept<W>(&mut self, writable: &W) -> Result<(), O::Error>
479 where
480 W: Writable<O>,
481 {
482 writable.write_to(&mut self.0).await?;
483 self.0.write(" ").await
484 }
485}
486
487struct TypeArgAccept<'o, O> {
488 inner: SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>>,
489}
490
491impl<'o, O> TypeArgAccept<'o, O> {
492 fn new(output: &'o mut O) -> Self {
493 Self {
494 inner: SeparatedSeqAccept::new(output, Str("<"), Str(", ")),
495 }
496 }
497}
498
499impl<'o, O> TypeArgAccept<'o, O>
500where
501 O: Output,
502{
503 async fn write_inside_diamond<TypeArgs>(&mut self, type_args: &TypeArgs) -> Result<(), O::Error>
504 where
505 TypeArgs: WritableSeq<O>,
506 {
507 type_args.for_each(&mut self.inner).await?;
508 if self.inner.wrote_any {
509 self.inner.output.write(">").await?;
510 }
511 Ok(())
512 }
513}
514
515fn type_arg_bound_accept<'o, O>(
516 output: &'o mut O,
517) -> SeparatedSeqAccept<'o, O, Str<&'static str>, Str<&'static str>> {
518 SeparatedSeqAccept::new(output, Str(": "), Str(" + "))
519}