tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 583HTML 275Markdown 8TOML 41586 Sutichaykuna

Fix bounds check in `CellOccupancyMatrix::last_of_type` (#890)

* Fix bounds check in CellOccupancyMatrix::last_of_type.

This can operate in either axis, but previously the bounds check was
hardcoded to check against the number of rows.

* Fix clippy::derivable_impls Default lints

Signed-off-by: Nico Burns <nico@nicoburns.com>

---------

Signed-off-by: Nico Burns <nico@nicoburns.com>

4ba82792b936af0e25f195b3c796c97835d16bfb · ruwaq Nico Burns · (11 killa ñawpaq) · tayta f99b57fba · kay commitpi sach'ata qhaway

Semántico sayk'uy · 13

src/compute/grid/types/cell_occupancy.rs

src/style/flex.rs

src/style/grid.rs

src/style/mod.rs

4 qillqakuna · +24 −49

diff --git a/src/compute/grid/types/cell_occupancy.rs b/src/compute/grid/types/cell_occupancy.rs
index 1903c2fa9..9fddca053 100644
--- a/src/compute/grid/types/cell_occupancy.rs
+++ b/src/compute/grid/types/cell_occupancy.rs
@@ -243,17 +243,22 @@ impl CellOccupancyMatrix {
         let track_counts = self.track_counts(track_type.other_axis());
         let track_computed_index = track_counts.oz_line_to_next_track(start_at);
 
-        // Index out of boudnds: no track to search
-        if track_computed_index < 0 || track_computed_index >= self.inner.rows() as i16 {
-            return None;
-        }
-
         let maybe_index = match track_type {
             AbsoluteAxis::Horizontal => {
-                self.inner.iter_row(track_computed_index as usize).rposition(|item| *item == kind)
+                if track_computed_index < 0 || track_computed_index >= self.inner.rows() as i16 {
+                    // Index out of bounds: no tracks to search
+                    None
+                } else {
+                    self.inner.iter_row(track_computed_index as usize).rposition(|item| *item == kind)
+                }
             }
             AbsoluteAxis::Vertical => {
-                self.inner.iter_col(track_computed_index as usize).rposition(|item| *item == kind)
+                if track_computed_index < 0 || track_computed_index >= self.inner.cols() as i16 {
+                    // Index out of bounds: no tracks to search
+                    None
+                } else {
+                    self.inner.iter_col(track_computed_index as usize).rposition(|item| *item == kind)
+                }
             }
         };
 
diff --git a/src/style/flex.rs b/src/style/flex.rs
index 1bbcb8f14..adf73516a 100644
--- a/src/style/flex.rs
+++ b/src/style/flex.rs
@@ -73,10 +73,11 @@ use crate::geometry::AbsoluteAxis;
 /// Defaults to [`FlexWrap::NoWrap`]
 ///
 /// [Specification](https://www.w3.org/TR/css-flexbox-1/#flex-wrap-property)
-#[derive(Copy, Clone, PartialEq, Eq, Debug)]
+#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 pub enum FlexWrap {
     /// Items will not wrap and stay on a single line
+    #[default]
     NoWrap,
     /// Items will wrap according to this item's [`FlexDirection`]
     Wrap,
@@ -84,12 +85,6 @@ pub enum FlexWrap {
     WrapReverse,
 }
 
-impl Default for FlexWrap {
-    fn default() -> Self {
-        Self::NoWrap
-    }
-}
-
 /// The direction of the flexbox layout main axis.
 ///
 /// There are always two perpendicular layout axes: main (or primary) and cross (or secondary).
@@ -101,12 +96,13 @@ impl Default for FlexWrap {
 /// The default behavior is [`FlexDirection::Row`].
 ///
 /// [Specification](https://www.w3.org/TR/css-flexbox-1/#flex-direction-property)
-#[derive(Copy, Clone, PartialEq, Eq, Debug)]
+#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 pub enum FlexDirection {
     /// Defines +x as the main axis
     ///
     /// Items will be added from left to right in a row.
+    #[default]
     Row,
     /// Defines +y as the main axis
     ///
@@ -122,12 +118,6 @@ pub enum FlexDirection {
     ColumnReverse,
 }
 
-impl Default for FlexDirection {
-    fn default() -> Self {
-        Self::Row
-    }
-}
-
 impl FlexDirection {
     #[inline]
     /// Is the direction [`FlexDirection::Row`] or [`FlexDirection::RowReverse`]?
diff --git a/src/style/grid.rs b/src/style/grid.rs
index 9f949d61d..c3d558335 100644
--- a/src/style/grid.rs
+++ b/src/style/grid.rs
@@ -273,10 +273,11 @@ pub trait GridItemStyle: CoreStyle {
 /// Defaults to [`GridAutoFlow::Row`]
 ///
 /// [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow)
-#[derive(Copy, Clone, PartialEq, Eq, Debug)]
+#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 pub enum GridAutoFlow {
     /// Items are placed by filling each row in turn, adding new rows as necessary
+    #[default]
     Row,
     /// Items are placed by filling each column in turn, adding new columns as necessary.
     Column,
@@ -286,12 +287,6 @@ pub enum GridAutoFlow {
     ColumnDense,
 }
 
-impl Default for GridAutoFlow {
-    fn default() -> Self {
-        Self::Row
-    }
-}
-
 impl GridAutoFlow {
     /// Whether grid auto placement uses the sparse placement algorithm or the dense placement algorithm
     /// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow#values>
@@ -343,10 +338,11 @@ pub(crate) type NonNamedGridPlacement = GenericGridPlacement<GridLine>;
 /// Defaults to `GridPlacement::Auto`
 ///
 /// [Specification](https://www.w3.org/TR/css3-grid-layout/#typedef-grid-row-start-grid-line)
-#[derive(Clone, PartialEq, Debug)]
+#[derive(Clone, PartialEq, Debug, Default)]
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 pub enum GridPlacement<S: CheapCloneStr = DefaultCheapStr> {
     /// Place item according to the auto-placement algorithm, and the parent's grid_auto_flow property
+    #[default]
     Auto,
     /// Place item at specified line (column or row) index
     Line(GridLine),
@@ -384,12 +380,6 @@ impl<S: CheapCloneStr> TaffyGridSpan for Line<GridPlacement<S>> {
     }
 }
 
-impl<S: CheapCloneStr> Default for GridPlacement<S> {
-    fn default() -> Self {
-        Self::Auto
-    }
-}
-
 impl<S: CheapCloneStr> GridPlacement<S> {
     /// Apply a mapping function if the [`GridPlacement`] is a `Line`. Otherwise return `self` unmodified.
     pub fn into_origin_zero_placement_ignoring_named(&self, explicit_track_count: u16) -> OriginZeroGridPlacement {
diff --git a/src/style/mod.rs b/src/style/mod.rs
index e44d06b74..ebc97776a 100644
--- a/src/style/mod.rs
+++ b/src/style/mod.rs
@@ -251,11 +251,12 @@ impl Default for BoxGenerationMode {
 /// which can be unintuitive.
 ///
 /// [`Position::Relative`] is the default value, in contrast to the default behavior in CSS.
-#[derive(Copy, Clone, PartialEq, Eq, Debug)]
+#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 pub enum Position {
     /// The offset is computed relative to the final position given by the layout algorithm.
     /// Offsets do not affect the position of any other items; they are effectively a correction factor applied at the end.
+    #[default]
     Relative,
     /// The offset is computed relative to this item's closest positioned ancestor, if any.
     /// Otherwise, it is placed relative to the origin.
@@ -265,12 +266,6 @@ pub enum Position {
     Absolute,
 }
 
-impl Default for Position {
-    fn default() -> Self {
-        Self::Relative
-    }
-}
-
 /// Specifies whether size styles for this node are assigned to the node's "content box" or "border box"
 ///
 /// - The "content box" is the node's inner size excluding padding, border and margin
@@ -284,21 +279,16 @@ impl Default for Position {
 ///   - `flex_basis`
 ///
 /// See <https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing>
-#[derive(Copy, Clone, PartialEq, Eq, Debug)]
+#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 pub enum BoxSizing {
     /// Size styles such size, min_size, max_size specify the box's "border box" (the size excluding margin but including padding/border)
+    #[default]
     BorderBox,
     /// Size styles such size, min_size, max_size specify the box's "content box" (the size excluding padding/border/margin)
     ContentBox,
 }
 
-impl Default for BoxSizing {
-    fn default() -> Self {
-        Self::BorderBox
-    }
-}
-
 /// How children overflowing their container should affect layout
 ///
 /// In CSS the primary effect of this property is to control whether contents of a parent container that overflow that container should