diff options
author | René Kijewski <rene.kijewski@fu-berlin.de> | 2023-03-27 05:10:14 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2023-03-31 13:02:03 +0200 |
commit | 41b4181a2001f5b6297a870e2875dc72e09b1989 (patch) | |
tree | c5fb413f247559b6d9ea4d1570d5063ead438c1a | |
parent | 795554e84ad923ae249d22c6145b194678ff7d89 (diff) | |
download | askama-41b4181a2001f5b6297a870e2875dc72e09b1989.tar.gz askama-41b4181a2001f5b6297a870e2875dc72e09b1989.tar.bz2 askama-41b4181a2001f5b6297a870e2875dc72e09b1989.zip |
Use `try_reserve()` instead of `with_capacity()`
`String::with_capacity()` panics if the requested memory could not be
allocated. `Template::render()` is fallible method, and the fact that it
can panic is not documented.
This commit uses `String::try_reserve()` instead, so even for an
exceedingly large `SIZE_HINT` the method should not panic. In the
generated code `write!()` calls will fail instead with
`Err(std::fmt::Error)`.
I do not test if `try_reserve()` returned an error, because the
rendering might succeed anyway, if less bytes are written than estimated.
Diffstat (limited to '')
-rw-r--r-- | askama/src/lib.rs | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/askama/src/lib.rs b/askama/src/lib.rs index 906c79a..8806490 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -82,7 +82,8 @@ pub use crate::error::{Error, Result}; pub trait Template: fmt::Display { /// Helper method which allocates a new `String` and renders into it fn render(&self) -> Result<String> { - let mut buf = String::with_capacity(Self::SIZE_HINT); + let mut buf = String::new(); + let _ = buf.try_reserve(Self::SIZE_HINT); self.render_into(&mut buf)?; Ok(buf) } |