Skip to content

Commit 5d38d64

Browse files
Merge pull request #726 from GuillaumeGomez/recursive-template
Fix compiler error with recursive types on recursive templates by using `dyn` instead of plain generic bounds
2 parents 46cd48b + 122a9bb commit 5d38d64

14 files changed

Lines changed: 146 additions & 200 deletions

File tree

askama/src/filters/alloc.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ impl<S: fmt::Display> fmt::Display for Lower<S> {
136136

137137
impl<S: FastWritable> FastWritable for Lower<S> {
138138
#[inline]
139-
fn write_into<W: fmt::Write + ?Sized>(
139+
fn write_into(
140140
&self,
141-
dest: &mut W,
141+
dest: &mut dyn fmt::Write,
142142
values: &dyn crate::Values,
143143
) -> crate::Result<()> {
144144
let mut buffer = String::new();
@@ -223,9 +223,9 @@ impl<S: fmt::Display> fmt::Display for Upper<S> {
223223

224224
impl<S: FastWritable> FastWritable for Upper<S> {
225225
#[inline]
226-
fn write_into<W: fmt::Write + ?Sized>(
226+
fn write_into(
227227
&self,
228-
dest: &mut W,
228+
dest: &mut dyn fmt::Write,
229229
values: &dyn crate::Values,
230230
) -> crate::Result<()> {
231231
let mut buffer = String::new();
@@ -306,9 +306,9 @@ impl<S: fmt::Display> fmt::Display for Trim<S> {
306306

307307
impl<S: FastWritable> FastWritable for Trim<S> {
308308
#[inline]
309-
fn write_into<W: fmt::Write + ?Sized>(
309+
fn write_into(
310310
&self,
311-
dest: &mut W,
311+
dest: &mut dyn fmt::Write,
312312
values: &dyn crate::Values,
313313
) -> crate::Result<()> {
314314
let mut collector = TrimCollector(String::new());
@@ -374,9 +374,9 @@ impl<S: fmt::Display> fmt::Display for Capitalize<S> {
374374

375375
impl<S: FastWritable> FastWritable for Capitalize<S> {
376376
#[inline]
377-
fn write_into<W: fmt::Write + ?Sized>(
377+
fn write_into(
378378
&self,
379-
dest: &mut W,
379+
dest: &mut dyn fmt::Write,
380380
values: &dyn crate::Values,
381381
) -> crate::Result<()> {
382382
let mut buffer = String::new();
@@ -437,9 +437,9 @@ impl<S: fmt::Display> fmt::Display for Title<S> {
437437

438438
impl<S: FastWritable> FastWritable for Title<S> {
439439
#[inline]
440-
fn write_into<W: fmt::Write + ?Sized>(
440+
fn write_into(
441441
&self,
442-
dest: &mut W,
442+
dest: &mut dyn fmt::Write,
443443
values: &dyn crate::Values,
444444
) -> crate::Result<()> {
445445
let mut buffer = String::new();

askama/src/filters/core.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ impl<S: fmt::Display> fmt::Display for TruncateFilter<S> {
5151

5252
impl<S: FastWritable> FastWritable for TruncateFilter<S> {
5353
#[inline]
54-
fn write_into<W: fmt::Write + ?Sized>(
55-
&self,
56-
dest: &mut W,
57-
values: &dyn Values,
58-
) -> crate::Result<()> {
54+
fn write_into(&self, dest: &mut dyn fmt::Write, values: &dyn Values) -> crate::Result<()> {
5955
self.source
6056
.write_into(&mut TruncateWriter::new(dest, self.remaining), values)
6157
}
@@ -448,11 +444,7 @@ impl<L: fmt::Display, R: fmt::Display> fmt::Display for Either<L, R> {
448444

449445
impl<L: FastWritable, R: FastWritable> FastWritable for Either<L, R> {
450446
#[inline]
451-
fn write_into<W: fmt::Write + ?Sized>(
452-
&self,
453-
dest: &mut W,
454-
values: &dyn Values,
455-
) -> crate::Result<()> {
447+
fn write_into(&self, dest: &mut dyn fmt::Write, values: &dyn Values) -> crate::Result<()> {
456448
match self {
457449
Either::Left(value) => value.write_into(dest, values),
458450
Either::Right(value) => value.write_into(dest, values),
@@ -573,11 +565,7 @@ impl<S: fmt::Display> fmt::Display for Wordcount<S> {
573565

574566
impl<S: FastWritable> FastWritable for Wordcount<S> {
575567
#[inline]
576-
fn write_into<W: fmt::Write + ?Sized>(
577-
&self,
578-
_: &mut W,
579-
values: &dyn crate::Values,
580-
) -> crate::Result<()> {
568+
fn write_into(&self, _: &mut dyn fmt::Write, values: &dyn crate::Values) -> crate::Result<()> {
581569
let mut inner = self.count.get();
582570
self.source
583571
.write_into(&mut WordCountWriter(&mut inner), values)?;
@@ -716,9 +704,9 @@ impl<S: fmt::Display> fmt::Display for NewlineCounting<S> {
716704
}
717705

718706
impl<S: FastWritable> FastWritable for NewlineCounting<S> {
719-
fn write_into<W: fmt::Write + ?Sized>(
707+
fn write_into(
720708
&self,
721-
dest: &mut W,
709+
dest: &mut dyn fmt::Write,
722710
values: &dyn crate::Values,
723711
) -> crate::Result<()> {
724712
self.run(dest, |f| self.source.write_into(f, values))
@@ -792,9 +780,9 @@ struct LinebreaksbrFormatter<'a, W: ?Sized>(&'a mut W);
792780

793781
impl<S: FastWritable> FastWritable for Linebreaksbr<S> {
794782
#[inline]
795-
fn write_into<W: fmt::Write + ?Sized>(
783+
fn write_into(
796784
&self,
797-
dest: &mut W,
785+
dest: &mut dyn fmt::Write,
798786
values: &dyn crate::Values,
799787
) -> crate::Result<()> {
800788
self.0.write_into(&mut LinebreaksbrFormatter(dest), values)

askama/src/filters/escape.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ impl<T: fmt::Display, E: Escaper> fmt::Display for EscapeDisplay<T, E> {
8181

8282
impl<T: FastWritable, E: Escaper> FastWritable for EscapeDisplay<T, E> {
8383
#[inline]
84-
fn write_into<W: fmt::Write + ?Sized>(
85-
&self,
86-
dest: &mut W,
87-
values: &dyn Values,
88-
) -> crate::Result<()> {
84+
fn write_into(&self, dest: &mut dyn fmt::Write, values: &dyn Values) -> crate::Result<()> {
8985
self.0.write_into(&mut EscapeWriter(dest, self.1), values)
9086
}
9187
}
@@ -311,11 +307,7 @@ const _: () = {
311307
// This is the fallback. The filter is not the last element of the filter chain.
312308
impl<T: FastWritable> FastWritable for MaybeSafe<T> {
313309
#[inline]
314-
fn write_into<W: fmt::Write + ?Sized>(
315-
&self,
316-
dest: &mut W,
317-
values: &dyn Values,
318-
) -> crate::Result<()> {
310+
fn write_into(&self, dest: &mut dyn fmt::Write, values: &dyn Values) -> crate::Result<()> {
319311
let inner = match self {
320312
MaybeSafe::Safe(inner) => inner,
321313
MaybeSafe::NeedsEscaping(inner) => inner,
@@ -350,11 +342,7 @@ const _: () = {
350342
}
351343

352344
impl<T: FastWritable + ?Sized, E: Escaper> FastWritable for Wrapped<'_, T, E> {
353-
fn write_into<W: fmt::Write + ?Sized>(
354-
&self,
355-
dest: &mut W,
356-
values: &dyn Values,
357-
) -> crate::Result<()> {
345+
fn write_into(&self, dest: &mut dyn fmt::Write, values: &dyn Values) -> crate::Result<()> {
358346
match *self {
359347
Wrapped::Safe(t) => t.write_into(dest, values),
360348
Wrapped::NeedsEscaping(t, e) => EscapeDisplay(t, e).write_into(dest, values),
@@ -426,11 +414,7 @@ const _: () = {
426414
// This is the fallback. The filter is not the last element of the filter chain.
427415
impl<T: FastWritable> FastWritable for Safe<T> {
428416
#[inline]
429-
fn write_into<W: fmt::Write + ?Sized>(
430-
&self,
431-
dest: &mut W,
432-
values: &dyn Values,
433-
) -> crate::Result<()> {
417+
fn write_into(&self, dest: &mut dyn fmt::Write, values: &dyn Values) -> crate::Result<()> {
434418
self.0.write_into(dest, values)
435419
}
436420
}
@@ -512,11 +496,7 @@ pub struct Writable<'a, S: ?Sized>(pub &'a S);
512496
/// Used internally by askama to select the appropriate [`write!()`] mechanism
513497
pub trait WriteWritable {
514498
/// Used internally by askama to select the appropriate [`write!()`] mechanism
515-
fn askama_write<W: fmt::Write + ?Sized>(
516-
&self,
517-
dest: &mut W,
518-
values: &dyn Values,
519-
) -> crate::Result<()>;
499+
fn askama_write(&self, dest: &mut dyn fmt::Write, values: &dyn Values) -> crate::Result<()>;
520500
}
521501

522502
#[test]

askama/src/filters/humansize.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ impl fmt::Display for FilesizeFormatFilter {
4242
}
4343

4444
impl FastWritable for FilesizeFormatFilter {
45-
fn write_into<W: fmt::Write + ?Sized>(
46-
&self,
47-
dest: &mut W,
48-
values: &dyn Values,
49-
) -> crate::Result<()> {
45+
fn write_into(&self, dest: &mut dyn fmt::Write, values: &dyn Values) -> crate::Result<()> {
5046
if self.bytes < 1000 {
5147
(self.bytes as u32).write_into(dest, values)?;
5248
return Ok(dest.write_str(" B")?);

askama/src/filters/indent.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ impl<S: fmt::Display, I: AsIndent> fmt::Display for Indent<S, I> {
8888
}
8989

9090
impl<S: FastWritable, I: AsIndent> FastWritable for Indent<S, I> {
91-
fn write_into<W: fmt::Write + ?Sized>(
91+
fn write_into(
9292
&self,
93-
dest: &mut W,
93+
dest: &mut dyn fmt::Write,
9494
values: &dyn crate::Values,
9595
) -> crate::Result<()> {
9696
let indent = self.indent.as_indent();

askama/src/filters/json.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ struct ToJsonPretty<S, I> {
102102

103103
impl<S: Serialize> FastWritable for ToJson<S> {
104104
#[inline]
105-
fn write_into<W: fmt::Write + ?Sized>(&self, f: &mut W, _: &dyn Values) -> crate::Result<()> {
105+
fn write_into(&self, f: &mut dyn fmt::Write, _: &dyn Values) -> crate::Result<()> {
106106
serialize(f, &self.value, CompactFormatter)
107107
}
108108
}
@@ -116,7 +116,7 @@ impl<S: Serialize> fmt::Display for ToJson<S> {
116116

117117
impl<S: Serialize, I: AsIndent> FastWritable for ToJsonPretty<S, I> {
118118
#[inline]
119-
fn write_into<W: fmt::Write + ?Sized>(&self, f: &mut W, _: &dyn Values) -> crate::Result<()> {
119+
fn write_into(&self, f: &mut dyn fmt::Write, _: &dyn Values) -> crate::Result<()> {
120120
serialize(
121121
f,
122122
&self.value,

askama/src/filters/urlencode.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ impl<T: fmt::Display> fmt::Display for UrlencodeFilter<T> {
111111

112112
impl<T: FastWritable> FastWritable for UrlencodeFilter<T> {
113113
#[inline]
114-
fn write_into<W: fmt::Write + ?Sized>(
115-
&self,
116-
f: &mut W,
117-
values: &dyn Values,
118-
) -> crate::Result<()> {
114+
fn write_into(&self, f: &mut dyn fmt::Write, values: &dyn Values) -> crate::Result<()> {
119115
self.0.write_into(&mut UrlencodeWriter(f, self.1), values)
120116
}
121117
}

askama/src/helpers.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl fmt::Display for Empty {
243243

244244
impl FastWritable for Empty {
245245
#[inline]
246-
fn write_into<W: fmt::Write + ?Sized>(&self, _: &mut W, _: &dyn Values) -> crate::Result<()> {
246+
fn write_into(&self, _: &mut dyn fmt::Write, _: &dyn Values) -> crate::Result<()> {
247247
Ok(())
248248
}
249249
}
@@ -277,20 +277,16 @@ impl<L: fmt::Display, R: fmt::Display> fmt::Display for Concat<L, R> {
277277

278278
impl<L: FastWritable, R: FastWritable> FastWritable for Concat<L, R> {
279279
#[inline]
280-
fn write_into<W: fmt::Write + ?Sized>(
281-
&self,
282-
dest: &mut W,
283-
values: &dyn Values,
284-
) -> crate::Result<()> {
280+
fn write_into(&self, dest: &mut dyn fmt::Write, values: &dyn Values) -> crate::Result<()> {
285281
self.0.write_into(dest, values)?;
286282
self.1.write_into(dest, values)
287283
}
288284
}
289285

290286
pub trait EnumVariantTemplate {
291-
fn render_into_with_values<W: fmt::Write + ?Sized>(
287+
fn render_into_with_values(
292288
&self,
293-
writer: &mut W,
289+
writer: &mut dyn fmt::Write,
294290
values: &dyn crate::Values,
295291
) -> crate::Result<()>;
296292
}

0 commit comments

Comments
 (0)