tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 501HTML 254C 19JavaScript 81412 Definiciones

refactoring

703e96faff6338567ed71af137eb5affc22e942c · por Adam Nemecek · (hace 6 años) · padre 28e00474a · ver el árbol en este commit

Delta semántico · 29

bindings/js/src/lib.rs

  • cambió de firma fn new pub
  • cambió de firma fn new pub

src/algo.rs

src/forest.rs

src/geometry.rs

src/node.rs

src/result.rs

  • cambió por dentro def new

src/style.rs

7 archivos · +75 −66

diff --git a/bindings/js/src/lib.rs b/bindings/js/src/lib.rs
index ad6db312c..9fccbd707 100644
--- a/bindings/js/src/lib.rs
+++ b/bindings/js/src/lib.rs
@@ -396,8 +396,8 @@ pub struct Allocator {
 #[wasm_bindgen]
 impl Allocator {
     #[wasm_bindgen(constructor)]
-    pub fn new() -> Allocator {
-        Allocator { stretch: Rc::new(RefCell::new(stretch::node::Stretch::new())) }
+    pub fn new() -> Self {
+        Self { stretch: Rc::new(RefCell::new(stretch::node::Stretch::new())) }
     }
 }
 
@@ -414,8 +414,8 @@ pub struct Node {
 #[wasm_bindgen]
 impl Node {
     #[wasm_bindgen(constructor)]
-    pub fn new(allocator: &Allocator, style: &JsValue) -> Node {
-        Node {
+    pub fn new(allocator: &Allocator, style: &JsValue) -> Self {
+        Self {
             allocator: allocator.clone(),
             node: allocator.stretch.borrow_mut().new_node(parse_style(&style), &[]).unwrap(),
             style: style.clone(),
diff --git a/src/algo.rs b/src/algo.rs
index df9b834a4..18575ace0 100644
--- a/src/algo.rs
+++ b/src/algo.rs
@@ -65,7 +65,7 @@ impl Forest {
         let result = if has_root_min_max {
             let first_pass = self.compute_internal(
                 root,
-                Size { width: style.size.width.resolve(size.width), height: style.size.height.resolve(size.height) },
+                style.size.resolve(size),
                 size,
                 false,
             );
@@ -92,7 +92,7 @@ impl Forest {
         } else {
             self.compute_internal(
                 root,
-                Size { width: style.size.width.resolve(size.width), height: style.size.height.resolve(size.height) },
+                style.size.resolve(size),
                 size,
                 true,
             )
@@ -101,7 +101,7 @@ impl Forest {
         self.nodes[root].layout = result::Layout {
             order: 0,
             size: Size { width: result.size.width, height: result.size.height },
-            location: Point { x: 0.0, y: 0.0 },
+            location: Point::zero(),
         };
 
         Self::round_layout(&mut self.nodes, &self.children, root, 0.0, 0.0);
@@ -186,8 +186,8 @@ impl Forest {
             height: node_size.height - padding_border.vertical(),
         };
 
-        let mut container_size = Size { width: 0.0, height: 0.0 };
-        let mut inner_container_size = Size { width: 0.0, height: 0.0 };
+        let mut container_size = Size::zero();
+        let mut inner_container_size = Size::zero();
 
         // If this is a leaf node we can skip a lot of this function in some cases
         if self.children[node].is_empty() {
@@ -238,21 +238,9 @@ impl Forest {
             .filter(|(_, style)| style.display != Display::None)
             .map(|(child, child_style)| FlexItem {
                 node: *child,
-
-                size: Size {
-                    width: child_style.size.width.resolve(node_inner_size.width),
-                    height: child_style.size.height.resolve(node_inner_size.height),
-                },
-
-                min_size: Size {
-                    width: child_style.min_size.width.resolve(node_inner_size.width),
-                    height: child_style.min_size.height.resolve(node_inner_size.height),
-                },
-
-                max_size: Size {
-                    width: child_style.max_size.width.resolve(node_inner_size.width),
-                    height: child_style.max_size.height.resolve(node_inner_size.height),
-                },
+                size: child_style.size.resolve(node_inner_size),
+                min_size: child_style.min_size.resolve(node_inner_size),
+                max_size: child_style.max_size.resolve(node_inner_size),
 
                 position: child_style.position.map(|p| p.resolve(node_inner_size.width)),
                 margin: child_style.margin.map(|m| m.resolve(node_inner_size.width).or_else(0.0)),
@@ -264,10 +252,10 @@ impl Forest {
                 violation: 0.0,
                 frozen: false,
 
-                hypothetical_inner_size: Size { width: 0.0, height: 0.0 },
-                hypothetical_outer_size: Size { width: 0.0, height: 0.0 },
-                target_size: Size { width: 0.0, height: 0.0 },
-                outer_target_size: Size { width: 0.0, height: 0.0 },
+                hypothetical_inner_size: Size::zero(),
+                hypothetical_outer_size: Size::zero(),
+                target_size: Size::zero(),
+                outer_target_size: Size::zero(),
 
                 baseline: 0.0,
 
@@ -762,7 +750,7 @@ impl Forest {
                         &result::Layout {
                             order: self.children[node].iter().position(|n| *n == child.node).unwrap() as u32,
                             size: result.size,
-                            location: Point { x: 0.0, y: 0.0 },
+                            location: Point::zero(),
                         },
                     );
                 }
@@ -1326,7 +1314,7 @@ impl Forest {
 
         fn hidden_layout(nodes: &mut [NodeData], children: &[sys::ChildrenVec<NodeId>], node: NodeId, order: u32) {
             nodes[node].layout =
-                result::Layout { order, size: Size { width: 0.0, height: 0.0 }, location: Point { x: 0.0, y: 0.0 } };
+                result::Layout { order, size: Size::zero(), location: Point::zero() };
 
             for (order, child) in children[node].iter().enumerate() {
                 hidden_layout(nodes, children, *child, order as _);
diff --git a/src/forest.rs b/src/forest.rs
index f6d1d2779..cdd26ec1b 100644
--- a/src/forest.rs
+++ b/src/forest.rs
@@ -19,11 +19,11 @@ pub(crate) struct NodeData {
 
 impl NodeData {
     fn new_leaf(style: Style, measure: MeasureFunc) -> Self {
-        NodeData { style, measure: Some(measure), layout_cache: None, layout: Layout::new(), is_dirty: true }
+        Self { style, measure: Some(measure), layout_cache: None, layout: Layout::new(), is_dirty: true }
     }
 
     fn new(style: Style) -> Self {
-        NodeData { style, measure: None, layout_cache: None, layout: Layout::new(), is_dirty: true }
+        Self { style, measure: None, layout_cache: None, layout: Layout::new(), is_dirty: true }
     }
 }
 
@@ -35,7 +35,7 @@ pub(crate) struct Forest {
 
 impl Forest {
     pub fn with_capacity(capacity: usize) -> Self {
-        Forest {
+        Self {
             nodes: sys::new_vec_with_capacity(capacity),
             children: sys::new_vec_with_capacity(capacity),
             parents: sys::new_vec_with_capacity(capacity),
diff --git a/src/geometry.rs b/src/geometry.rs
index 7708b1830..c2e93142b 100644
--- a/src/geometry.rs
+++ b/src/geometry.rs
@@ -137,8 +137,29 @@ impl<T> Size<T> {
     }
 }
 
+impl Size<f32> {
+    pub fn zero() -> Self {
+        Self { width: 0.0, height: 0.0 }
+    }
+}
+
+impl Size<style::Dimension> {
+    pub(crate) fn resolve(&self, parent: Size<Number>) -> Size<Number> {
+        Size {
+            width: self.width.resolve(parent.width),
+            height: self.height.resolve(parent.height)
+        }
+    }
+}
+
 #[derive(Debug, Copy, Clone, PartialEq)]
 pub struct Point<T> {
     pub x: T,
     pub y: T,
 }
+
+impl Point<f32> {
+    pub fn zero() -> Self {
+        Self { x: 0.0, y: 0.0 }
+    }
+}
diff --git a/src/node.rs b/src/node.rs
index 2516d451f..4a423d747 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -35,7 +35,7 @@ pub struct Stretch {
 
 impl Default for Stretch {
     fn default() -> Self {
-        Stretch::with_capacity(16)
+        Self::with_capacity(16)
     }
 }
 
@@ -45,7 +45,7 @@ impl Stretch {
     }
 
     pub fn with_capacity(capacity: usize) -> Self {
-        Stretch {
+        Self {
             id: INSTANCE_ALLOCATOR.allocate(),
             nodes: id::Allocator::new(),
             nodes_to_ids: crate::sys::new_map_with_capacity(capacity),
diff --git a/src/result.rs b/src/result.rs
index 7f69e8a5f..5ea88aef0 100644
--- a/src/result.rs
+++ b/src/result.rs
@@ -11,7 +11,7 @@ pub struct Layout {
 
 impl Layout {
     pub(crate) fn new() -> Self {
-        Layout { order: 0, size: Size { width: 0.0, height: 0.0 }, location: Point { x: 0.0, y: 0.0 } }
+        Self { order: 0, size: Size::zero(), location: Point::zero() }
     }
 }
 
diff --git a/src/style.rs b/src/style.rs
index 784346087..212c1e1c4 100644
--- a/src/style.rs
+++ b/src/style.rs
@@ -14,8 +14,8 @@ pub enum AlignItems {
 }
 
 impl Default for AlignItems {
-    fn default() -> AlignItems {
-        AlignItems::Stretch
+    fn default() -> Self {
+        Self::Stretch
     }
 }
 
@@ -33,8 +33,8 @@ pub enum AlignSelf {
 }
 
 impl Default for AlignSelf {
-    fn default() -> AlignSelf {
-        AlignSelf::Auto
+    fn default() -> Self {
+        Self::Auto
     }
 }
 
@@ -52,8 +52,8 @@ pub enum AlignContent {
 }
 
 impl Default for AlignContent {
-    fn default() -> AlignContent {
-        AlignContent::Stretch
+    fn default() -> Self {
+        Self::Stretch
     }
 }
 
@@ -70,8 +70,8 @@ pub enum Direction {
 }
 
 impl Default for Direction {
-    fn default() -> Direction {
-        Direction::Inherit
+    fn default() -> Self {
+        Self::Inherit
     }
 }
 
@@ -87,8 +87,8 @@ pub enum Display {
 }
 
 impl Default for Display {
-    fn default() -> Display {
-        Display::Flex
+    fn default() -> Self {
+        Self::Flex
     }
 }
 
@@ -104,8 +104,8 @@ pub enum FlexDirection {
 }
 
 impl Default for FlexDirection {
-    fn default() -> FlexDirection {
-        FlexDirection::Row
+    fn default() -> Self {
+        Self::Row
     }
 }
 
@@ -137,8 +137,8 @@ pub enum JustifyContent {
 }
 
 impl Default for JustifyContent {
-    fn default() -> JustifyContent {
-        JustifyContent::FlexStart
+    fn default() -> Self {
+        Self::FlexStart
     }
 }
 
@@ -153,8 +153,8 @@ pub enum Overflow {
 }
 
 impl Default for Overflow {
-    fn default() -> Overflow {
-        Overflow::Visible
+    fn default() -> Self {
+        Self::Visible
     }
 }
 
@@ -168,8 +168,8 @@ pub enum PositionType {
 }
 
 impl Default for PositionType {
-    fn default() -> PositionType {
-        PositionType::Relative
+    fn default() -> Self {
+        Self::Relative
     }
 }
 
@@ -184,8 +184,8 @@ pub enum FlexWrap {
 }
 
 impl Default for FlexWrap {
-    fn default() -> FlexWrap {
-        FlexWrap::NoWrap
+    fn default() -> Self {
+        Self::NoWrap
     }
 }
 
@@ -201,16 +201,16 @@ pub enum Dimension {
 }
 
 impl Default for Dimension {
-    fn default() -> Dimension {
-        Dimension::Undefined
+    fn default() -> Self {
+        Self::Undefined
     }
 }
 
 impl Dimension {
-    pub(crate) fn resolve(self, parent_width: Number) -> Number {
+    pub(crate) fn resolve(self, parent_dim: Number) -> Number {
         match self {
             Dimension::Points(points) => Number::Defined(points),
-            Dimension::Percent(percent) => parent_width * percent,
+            Dimension::Percent(percent) => parent_dim * percent,
             _ => Number::Undefined,
         }
     }
@@ -225,14 +225,14 @@ impl Dimension {
 }
 
 impl Default for Rect<Dimension> {
-    fn default() -> Rect<Dimension> {
-        Rect { start: Default::default(), end: Default::default(), top: Default::default(), bottom: Default::default() }
+    fn default() -> Self {
+        Self { start: Default::default(), end: Default::default(), top: Default::default(), bottom: Default::default() }
     }
 }
 
 impl Default for Size<Dimension> {
-    fn default() -> Size<Dimension> {
-        Size { width: Dimension::Auto, height: Dimension::Auto }
+    fn default() -> Self {
+        Self { width: Dimension::Auto, height: Dimension::Auto }
     }
 }
 
@@ -266,8 +266,8 @@ pub struct Style {
 }
 
 impl Default for Style {
-    fn default() -> Style {
-        Style {
+    fn default() -> Self {
+        Self {
             display: Default::default(),
             position_type: Default::default(),
             direction: Default::default(),