tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1077HTML 1008Markdown 12TOML 83209 Definiciones

Cleanup `new_with_children()` uses (#194)

* Cleanup `new_with_children()` uses

* Format

* Update scripts/gentest/src/main.rs

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>

832e2da45631009be13279a06c30f09962db0688 · por Andreas Weibye · (hace 4 años) · padre 7762c2490 · ver el árbol en este commit

Delta semántico · 14

benches/complex.rs

examples/basic.rs

  • cambió por dentro fn main

examples/nested.rs

  • cambió por dentro fn main

src/node.rs

tests/measure.rs

tests/relayout.rs

tests/root_constraints.rs

tests/simple_child.rs

9 archivos · +125 −187

diff --git a/benches/complex.rs b/benches/complex.rs
index a375b586e..2a27ce7d7 100644
--- a/benches/complex.rs
+++ b/benches/complex.rs
@@ -2,53 +2,41 @@ use criterion::{criterion_group, criterion_main, Criterion};
 
 fn build_deep_hierarchy(taffy: &mut taffy::node::Taffy) -> taffy::node::Node {
     let node111 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                size: taffy::geometry::Size {
-                    width: taffy::style::Dimension::Points(10.0),
-                    height: taffy::style::Dimension::Points(10.0),
-                },
-                ..Default::default()
+        .new_leaf(taffy::style::FlexboxLayout {
+            size: taffy::geometry::Size {
+                width: taffy::style::Dimension::Points(10.0),
+                height: taffy::style::Dimension::Points(10.0),
             },
-            &[],
-        )
+            ..Default::default()
+        })
         .unwrap();
     let node112 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                size: taffy::geometry::Size {
-                    width: taffy::style::Dimension::Points(10.0),
-                    height: taffy::style::Dimension::Points(10.0),
-                },
-                ..Default::default()
+        .new_leaf(taffy::style::FlexboxLayout {
+            size: taffy::geometry::Size {
+                width: taffy::style::Dimension::Points(10.0),
+                height: taffy::style::Dimension::Points(10.0),
             },
-            &[],
-        )
+            ..Default::default()
+        })
         .unwrap();
 
     let node121 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                size: taffy::geometry::Size {
-                    width: taffy::style::Dimension::Points(10.0),
-                    height: taffy::style::Dimension::Points(10.0),
-                },
-                ..Default::default()
+        .new_leaf(taffy::style::FlexboxLayout {
+            size: taffy::geometry::Size {
+                width: taffy::style::Dimension::Points(10.0),
+                height: taffy::style::Dimension::Points(10.0),
             },
-            &[],
-        )
+            ..Default::default()
+        })
         .unwrap();
     let node122 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                size: taffy::geometry::Size {
-                    width: taffy::style::Dimension::Points(10.0),
-                    height: taffy::style::Dimension::Points(10.0),
-                },
-                ..Default::default()
+        .new_leaf(taffy::style::FlexboxLayout {
+            size: taffy::geometry::Size {
+                width: taffy::style::Dimension::Points(10.0),
+                height: taffy::style::Dimension::Points(10.0),
             },
-            &[],
-        )
+            ..Default::default()
+        })
         .unwrap();
 
     let node11 =
@@ -59,53 +47,41 @@ fn build_deep_hierarchy(taffy: &mut taffy::node::Taffy) -> taffy::node::Node {
         taffy.new_with_children(taffy::style::FlexboxLayout { ..Default::default() }, &[node11, node12]).unwrap();
 
     let node211 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                size: taffy::geometry::Size {
-                    width: taffy::style::Dimension::Points(10.0),
-                    height: taffy::style::Dimension::Points(10.0),
-                },
-                ..Default::default()
+        .new_leaf(taffy::style::FlexboxLayout {
+            size: taffy::geometry::Size {
+                width: taffy::style::Dimension::Points(10.0),
+                height: taffy::style::Dimension::Points(10.0),
             },
-            &[],
-        )
+            ..Default::default()
+        })
         .unwrap();
     let node212 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                size: taffy::geometry::Size {
-                    width: taffy::style::Dimension::Points(10.0),
-                    height: taffy::style::Dimension::Points(10.0),
-                },
-                ..Default::default()
+        .new_leaf(taffy::style::FlexboxLayout {
+            size: taffy::geometry::Size {
+                width: taffy::style::Dimension::Points(10.0),
+                height: taffy::style::Dimension::Points(10.0),
             },
-            &[],
-        )
+            ..Default::default()
+        })
         .unwrap();
 
     let node221 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                size: taffy::geometry::Size {
-                    width: taffy::style::Dimension::Points(10.0),
-                    height: taffy::style::Dimension::Points(10.0),
-                },
-                ..Default::default()
+        .new_leaf(taffy::style::FlexboxLayout {
+            size: taffy::geometry::Size {
+                width: taffy::style::Dimension::Points(10.0),
+                height: taffy::style::Dimension::Points(10.0),
             },
-            &[],
-        )
+            ..Default::default()
+        })
         .unwrap();
     let node222 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                size: taffy::geometry::Size {
-                    width: taffy::style::Dimension::Points(10.0),
-                    height: taffy::style::Dimension::Points(10.0),
-                },
-                ..Default::default()
+        .new_leaf(taffy::style::FlexboxLayout {
+            size: taffy::geometry::Size {
+                width: taffy::style::Dimension::Points(10.0),
+                height: taffy::style::Dimension::Points(10.0),
             },
-            &[],
-        )
+            ..Default::default()
+        })
         .unwrap();
 
     let node21 =
diff --git a/examples/basic.rs b/examples/basic.rs
index ba1f7f29b..ae6835068 100644
--- a/examples/basic.rs
+++ b/examples/basic.rs
@@ -3,10 +3,10 @@ use taffy::prelude::*;
 fn main() -> Result<(), taffy::error::InvalidNode> {
     let mut taffy = Taffy::new();
 
-    let child = taffy.new_with_children(
-        FlexboxLayout { size: Size { width: Dimension::Percent(0.5), height: Dimension::Auto }, ..Default::default() },
-        &[],
-    )?;
+    let child = taffy.new_leaf(FlexboxLayout {
+        size: Size { width: Dimension::Percent(0.5), height: Dimension::Auto },
+        ..Default::default()
+    })?;
 
     let node = taffy.new_with_children(
         FlexboxLayout {
diff --git a/examples/nested.rs b/examples/nested.rs
index 2abf98213..1915a7bca 100644
--- a/examples/nested.rs
+++ b/examples/nested.rs
@@ -4,13 +4,10 @@ fn main() -> Result<(), taffy::error::InvalidNode> {
     let mut taffy = Taffy::new();
 
     // left
-    let child_t1 = taffy.new_with_children(
-        FlexboxLayout {
-            size: Size { width: Dimension::Points(5.0), height: Dimension::Points(5.0) },
-            ..Default::default()
-        },
-        &[],
-    )?;
+    let child_t1 = taffy.new_leaf(FlexboxLayout {
+        size: Size { width: Dimension::Points(5.0), height: Dimension::Points(5.0) },
+        ..Default::default()
+    })?;
 
     let div1 = taffy.new_with_children(
         FlexboxLayout {
@@ -22,13 +19,10 @@ fn main() -> Result<(), taffy::error::InvalidNode> {
     )?;
 
     // right
-    let child_t2 = taffy.new_with_children(
-        FlexboxLayout {
-            size: Size { width: Dimension::Points(5.0), height: Dimension::Points(5.0) },
-            ..Default::default()
-        },
-        &[],
-    )?;
+    let child_t2 = taffy.new_leaf(FlexboxLayout {
+        size: Size { width: Dimension::Points(5.0), height: Dimension::Points(5.0) },
+        ..Default::default()
+    })?;
 
     let div2 = taffy.new_with_children(
         FlexboxLayout {
diff --git a/scripts/gentest/src/main.rs b/scripts/gentest/src/main.rs
index ba68cc12a..0f3fbba48 100644
--- a/scripts/gentest/src/main.rs
+++ b/scripts/gentest/src/main.rs
@@ -425,6 +425,7 @@ fn generate_node(ident: &str, node: &json::JsonValue) -> TokenStream {
             ..Default::default()
         },
         #children
+        // TODO: Only add children if they exist
     ).unwrap();)
 }
 
diff --git a/src/node.rs b/src/node.rs
index 1df0178b2..e68fd3393 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -476,7 +476,7 @@ mod tests {
     fn remove_node_should_remove() {
         let mut taffy = Taffy::new();
 
-        let node = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
+        let node = taffy.new_leaf(FlexboxLayout::default()).unwrap();
 
         // node should exist
         assert!(taffy.find_node(node).is_ok());
diff --git a/tests/measure.rs b/tests/measure.rs
index 963087b9d..9100d4e4d 100644
--- a/tests/measure.rs
+++ b/tests/measure.rs
@@ -118,16 +118,13 @@ mod measure {
     fn measure_child_with_flex_grow() {
         let mut taffy = taffy::node::Taffy::new();
         let child0 = taffy
-            .new_with_children(
-                taffy::style::FlexboxLayout {
-                    size: taffy::geometry::Size {
-                        width: taffy::style::Dimension::Points(50.0),
-                        height: taffy::style::Dimension::Points(50.0),
-                    },
-                    ..Default::default()
+            .new_leaf(taffy::style::FlexboxLayout {
+                size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(50.0),
+                    height: taffy::style::Dimension::Points(50.0),
                 },
-                &[],
-            )
+                ..Default::default()
+            })
             .unwrap();
 
         let child1 = taffy
@@ -160,17 +157,14 @@ mod measure {
     fn measure_child_with_flex_shrink() {
         let mut taffy = taffy::node::Taffy::new();
         let child0 = taffy
-            .new_with_children(
-                taffy::style::FlexboxLayout {
-                    size: taffy::geometry::Size {
-                        width: taffy::style::Dimension::Points(50.0),
-                        height: taffy::style::Dimension::Points(50.0),
-                    },
-                    flex_shrink: 0.0,
-                    ..Default::default()
+            .new_leaf(taffy::style::FlexboxLayout {
+                size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(50.0),
+                    height: taffy::style::Dimension::Points(50.0),
                 },
-                &[],
-            )
+                flex_shrink: 0.0,
+                ..Default::default()
+            })
             .unwrap();
 
         let child1 = taffy
@@ -203,16 +197,13 @@ mod measure {
     fn remeasure_child_after_growing() {
         let mut taffy = taffy::node::Taffy::new();
         let child0 = taffy
-            .new_with_children(
-                taffy::style::FlexboxLayout {
-                    size: taffy::geometry::Size {
-                        width: taffy::style::Dimension::Points(50.0),
-                        height: taffy::style::Dimension::Points(50.0),
-                    },
-                    ..Default::default()
+            .new_leaf(taffy::style::FlexboxLayout {
+                size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(50.0),
+                    height: taffy::style::Dimension::Points(50.0),
                 },
-                &[],
-            )
+                ..Default::default()
+            })
             .unwrap();
 
         let child1 = taffy
@@ -248,17 +239,14 @@ mod measure {
         let mut taffy = taffy::node::Taffy::new();
 
         let child0 = taffy
-            .new_with_children(
-                taffy::style::FlexboxLayout {
-                    size: taffy::geometry::Size {
-                        width: taffy::style::Dimension::Points(50.0),
-                        height: taffy::style::Dimension::Points(50.0),
-                    },
-                    flex_shrink: 0.0,
-                    ..Default::default()
+            .new_leaf(taffy::style::FlexboxLayout {
+                size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(50.0),
+                    height: taffy::style::Dimension::Points(50.0),
                 },
-                &[],
-            )
+                flex_shrink: 0.0,
+                ..Default::default()
+            })
             .unwrap();
 
         let child1 = taffy
@@ -373,14 +361,11 @@ mod measure {
     fn flex_basis_overrides_measure() {
         let mut taffy = taffy::node::Taffy::new();
         let child0 = taffy
-            .new_with_children(
-                taffy::style::FlexboxLayout {
-                    flex_basis: taffy::style::Dimension::Points(50.0),
-                    flex_grow: 1.0,
-                    ..Default::default()
-                },
-                &[],
-            )
+            .new_leaf(taffy::style::FlexboxLayout {
+                flex_basis: taffy::style::Dimension::Points(50.0),
+                flex_grow: 1.0,
+                ..Default::default()
+            })
             .unwrap();
 
         let child1 = taffy
diff --git a/tests/relayout.rs b/tests/relayout.rs
index 6b4df7767..2345ea187 100644
--- a/tests/relayout.rs
+++ b/tests/relayout.rs
@@ -4,13 +4,10 @@ use taffy::style::Dimension;
 fn relayout() {
     let mut taffy = taffy::Taffy::new();
     let node1 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                size: taffy::geometry::Size { width: Dimension::Points(8f32), height: Dimension::Points(80f32) },
-                ..Default::default()
-            },
-            &[],
-        )
+        .new_leaf(taffy::style::FlexboxLayout {
+            size: taffy::geometry::Size { width: Dimension::Points(8f32), height: Dimension::Points(80f32) },
+            ..Default::default()
+        })
         .unwrap();
     let node0 = taffy
         .new_with_children(
diff --git a/tests/root_constraints.rs b/tests/root_constraints.rs
index bc2ee1f86..f31467a0c 100644
--- a/tests/root_constraints.rs
+++ b/tests/root_constraints.rs
@@ -5,16 +5,13 @@ mod root_constraints {
     fn root_with_percentage_size() {
         let mut taffy = taffy::node::Taffy::new();
         let node = taffy
-            .new_with_children(
-                taffy::style::FlexboxLayout {
-                    size: taffy::geometry::Size {
-                        width: taffy::style::Dimension::Percent(1.0),
-                        height: taffy::style::Dimension::Percent(1.0),
-                    },
-                    ..Default::default()
+            .new_leaf(taffy::style::FlexboxLayout {
+                size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Percent(1.0),
+                    height: taffy::style::Dimension::Percent(1.0),
                 },
-                &[],
-            )
+                ..Default::default()
+            })
             .unwrap();
 
         taffy.compute_layout(node, taffy::geometry::Size { width: Some(100.0), height: Some(200.0) }).unwrap();
@@ -27,7 +24,7 @@ mod root_constraints {
     #[test]
     fn root_with_no_size() {
         let mut taffy = taffy::node::Taffy::new();
-        let node = taffy.new_with_children(taffy::style::FlexboxLayout { ..Default::default() }, &[]).unwrap();
+        let node = taffy.new_leaf(taffy::style::FlexboxLayout { ..Default::default() }).unwrap();
 
         taffy.compute_layout(node, taffy::geometry::Size { width: Some(100.0), height: Some(100.0) }).unwrap();
         let layout = taffy.layout(node).unwrap();
@@ -40,16 +37,13 @@ mod root_constraints {
     fn root_with_larger_size() {
         let mut taffy = taffy::node::Taffy::new();
         let node = taffy
-            .new_with_children(
-                taffy::style::FlexboxLayout {
-                    size: taffy::geometry::Size {
-                        width: taffy::style::Dimension::Points(200.0),
-                        height: taffy::style::Dimension::Points(200.0),
-                    },
-                    ..Default::default()
+            .new_leaf(taffy::style::FlexboxLayout {
+                size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(200.0),
+                    height: taffy::style::Dimension::Points(200.0),
                 },
-                &[],
-            )
+                ..Default::default()
+            })
             .unwrap();
 
         taffy.compute_layout(node, taffy::geometry::Size { width: Some(100.0), height: Some(100.0) }).unwrap();
diff --git a/tests/simple_child.rs b/tests/simple_child.rs
index 61a4f9fb9..002fa0f5a 100644
--- a/tests/simple_child.rs
+++ b/tests/simple_child.rs
@@ -4,14 +4,11 @@ use taffy::{geometry::Point, style::Dimension};
 fn simple_child() {
     let mut taffy = taffy::Taffy::new();
     let node0_0 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                align_self: taffy::prelude::AlignSelf::Center,
-                size: taffy::geometry::Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
-                ..Default::default()
-            },
-            &[],
-        )
+        .new_leaf(taffy::style::FlexboxLayout {
+            align_self: taffy::prelude::AlignSelf::Center,
+            size: taffy::geometry::Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
+            ..Default::default()
+        })
         .unwrap();
     let node0 = taffy
         .new_with_children(
@@ -23,24 +20,18 @@ fn simple_child() {
         )
         .unwrap();
     let node1_0 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                align_self: taffy::prelude::AlignSelf::Center,
-                size: taffy::geometry::Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
-                ..Default::default()
-            },
-            &[],
-        )
+        .new_leaf(taffy::style::FlexboxLayout {
+            align_self: taffy::prelude::AlignSelf::Center,
+            size: taffy::geometry::Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
+            ..Default::default()
+        })
         .unwrap();
     let node1_1 = taffy
-        .new_with_children(
-            taffy::style::FlexboxLayout {
-                align_self: taffy::prelude::AlignSelf::Center,
-                size: taffy::geometry::Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
-                ..Default::default()
-            },
-            &[],
-        )
+        .new_leaf(taffy::style::FlexboxLayout {
+            align_self: taffy::prelude::AlignSelf::Center,
+            size: taffy::geometry::Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
+            ..Default::default()
+        })
         .unwrap();
     let node1 = taffy
         .new_with_children(