From 6181ae557fdf27cdf63052c6db5d21a7d31a0010 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Mon, 16 Nov 2026 23:26:35 -0500 Subject: [PATCH] feat(lottie): flatten borrowed gradient stops without temporary buffers --- crates/lottie/src/gradient.rs | 14 +++++++++++++- crates/lottie/src/lib.rs | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/crates/lottie/src/gradient.rs b/crates/lottie/src/gradient.rs index 0c9a9608f..a3dff7314 100644 --- a/crates/lottie/src/gradient.rs +++ b/crates/lottie/src/gradient.rs @@ -4,6 +4,18 @@ use serde_json::json; use crate::{Json, nums, round6}; +/// Raw gradient wire values from borrowed or mapped stops, without intermediate allocation. +/// +/// Yields color groups (`position, r, g, b`) then every opacity pair (`position, alpha`). +/// Input order and all values, including non-finite values and opaque alpha, are preserved. +/// Sanitization, rounding, and input/output size budgets remain the caller's responsibility. +pub fn flat_gradient_values( + colors: impl IntoIterator, + opacities: impl IntoIterator, +) -> impl Iterator { + colors.into_iter().flat_map(|(p, [r, g, b])| [p, r, g, b]).chain(opacities.into_iter().flat_map(|(p, a)| [p, a])) +} + /// A gradient: colour stops and opacity stops, each at a position in 0..=1. #[derive(Debug, Clone, Default, PartialEq)] pub struct Gradient { @@ -37,7 +49,7 @@ impl Gradient { /// stops remain present so an opaque frame can interpolate to a translucent one. /// Non-finite values are preserved; callers must sanitize them before JSON output. pub fn flat_values(&self) -> impl Iterator + '_ { - self.colors.iter().flat_map(|(p, [r, g, b])| [*p, *r, *g, *b]).chain(self.opacities.iter().flat_map(|(p, a)| [*p, *a])) + flat_gradient_values(self.colors.iter().copied(), self.opacities.iter().copied()) } /// The flat list, rounded to six decimals. The alpha pairs are left out when every stop is diff --git a/crates/lottie/src/lib.rs b/crates/lottie/src/lib.rs index 16c1c6e07..578939d6b 100644 --- a/crates/lottie/src/lib.rs +++ b/crates/lottie/src/lib.rs @@ -30,7 +30,7 @@ mod prop; mod value; pub mod zip; -pub use gradient::Gradient; +pub use gradient::{Gradient, flat_gradient_values}; pub use model::{Animated, Animation, Layer, LayerContent, Limits, Shape, ShapeKind}; pub use path::Bezier; pub use prop::{Ease, Key, KeyOut, Prop, PropValue, Tangents, is_animated, read_prop, tangent, write_keys, write_prop};