summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-10-01 12:47:59 +0200
committerGitHub <noreply@github.com>2024-10-01 10:47:59 +0000
commitd03af848eb510a9ce0ad84db6714d27982c68717 (patch)
treec1bcf635f795282e12225a6ebd8381450c0782b5
parent63e6150ca1cd793d5c2a994e892d862b45bdaa42 (diff)
Prevent double allocation due to `Content::sequence` (#5084)
-rw-r--r--crates/typst/src/foundations/content.rs18
1 files changed, 8 insertions, 10 deletions
diff --git a/crates/typst/src/foundations/content.rs b/crates/typst/src/foundations/content.rs
index 9e26b179..0103ff9b 100644
--- a/crates/typst/src/foundations/content.rs
+++ b/crates/typst/src/foundations/content.rs
@@ -245,16 +245,14 @@ impl Content {
/// Create a new sequence element from multiples elements.
pub fn sequence(iter: impl IntoIterator<Item = Self>) -> Self {
- let mut iter = iter.into_iter();
- let Some(first) = iter.next() else { return Self::empty() };
- let Some(second) = iter.next() else { return first };
- SequenceElem::new(
- std::iter::once(first)
- .chain(std::iter::once(second))
- .chain(iter)
- .collect(),
- )
- .into()
+ let vec: Vec<_> = iter.into_iter().collect();
+ if vec.is_empty() {
+ Self::empty()
+ } else if vec.len() == 1 {
+ vec.into_iter().next().unwrap()
+ } else {
+ SequenceElem::new(vec).into()
+ }
}
/// Whether the contained element is of type `T`.