Skip to content

Commit 4a9ac5e

Browse files
committed
Don't return TaffyResult when Taffy methods can't fail.
Initial commit for: DioxusLabs#519. Affected methods: `new_leaf`, `compute_layout`, `new_leaf_with_measure`, `new_with_children`, `remove`, `set_measure`, `add_child`, `set_children`, `child_count`, `children`, `layout`, `set_style`, `style`, `mark_dirty`, `dirty`. Should be noted that many of the methods can still panic due to the frequent use of direct array indexing.
1 parent 6cf7960 commit 4a9ac5e

File tree

15 files changed

+759
-944
lines changed

15 files changed

+759
-944
lines changed

examples/basic.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use taffy::prelude::*;
22

3-
fn main() -> Result<(), taffy::TaffyError> {
3+
fn main() {
44
let mut taffy = Taffy::new();
55

66
let child = taffy.new_leaf(Style {
77
size: Size { width: Dimension::Percent(0.5), height: Dimension::Auto },
88
..Default::default()
9-
})?;
9+
});
1010

1111
let node = taffy.new_with_children(
1212
Style {
@@ -15,18 +15,14 @@ fn main() -> Result<(), taffy::TaffyError> {
1515
..Default::default()
1616
},
1717
&[child],
18-
)?;
18+
);
1919

20-
taffy.compute_layout(
21-
node,
22-
Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
23-
)?;
20+
taffy
21+
.compute_layout(node, Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) });
2422

2523
// or just use undefined for 100 x 100
2624
// taffy.compute_layout(node, Size::NONE)?;
2725

28-
println!("node: {:#?}", taffy.layout(node)?);
29-
println!("child: {:#?}", taffy.layout(child)?);
30-
31-
Ok(())
26+
println!("node: {:#?}", taffy.layout(node));
27+
println!("child: {:#?}", taffy.layout(child));
3228
}

examples/flexbox_gap.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@ use taffy::prelude::*;
33
// Creates three 20px x 20px children, evenly spaced 10px apart from each other
44
// Thus the container is 80px x 20px.
55

6-
fn main() -> Result<(), taffy::TaffyError> {
6+
fn main() {
77
let mut taffy = Taffy::new();
88

99
let child_style = Style { size: Size { width: length(20.0), height: length(20.0) }, ..Default::default() };
10-
let child0 = taffy.new_leaf(child_style.clone())?;
11-
let child1 = taffy.new_leaf(child_style.clone())?;
12-
let child2 = taffy.new_leaf(child_style.clone())?;
10+
let child0 = taffy.new_leaf(child_style.clone());
11+
let child1 = taffy.new_leaf(child_style.clone());
12+
let child2 = taffy.new_leaf(child_style.clone());
1313

1414
let root = taffy.new_with_children(
1515
Style { gap: Size { width: length(10.0), height: zero() }, ..Default::default() },
1616
&[child0, child1, child2],
17-
)?;
17+
);
1818

1919
// Compute layout and print result
20-
taffy.compute_layout(root, Size::MAX_CONTENT)?;
20+
taffy.compute_layout(root, Size::MAX_CONTENT);
2121
taffy::util::print_tree(&taffy, root);
22-
23-
Ok(())
2422
}

examples/grid_holy_grail.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn default<T: Default>() -> T {
1616
}
1717

1818
#[cfg(feature = "grid")]
19-
fn main() -> Result<(), taffy::TaffyError> {
19+
fn main() {
2020
use taffy::prelude::*;
2121

2222
let mut taffy = Taffy::new();
@@ -31,18 +31,16 @@ fn main() -> Result<(), taffy::TaffyError> {
3131
};
3232

3333
// Define the child nodes
34-
let header = taffy.new_leaf(Style { grid_row: line(1), grid_column: span(3), ..default() })?;
35-
let left_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(1), ..default() })?;
36-
let content_area = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(2), ..default() })?;
37-
let right_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(3), ..default() })?;
38-
let footer = taffy.new_leaf(Style { grid_row: line(3), grid_column: span(3), ..default() })?;
34+
let header = taffy.new_leaf(Style { grid_row: line(1), grid_column: span(3), ..default() });
35+
let left_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(1), ..default() });
36+
let content_area = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(2), ..default() });
37+
let right_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(3), ..default() });
38+
let footer = taffy.new_leaf(Style { grid_row: line(3), grid_column: span(3), ..default() });
3939

4040
// Create the container with the children
41-
let root = taffy.new_with_children(root_style, &[header, left_sidebar, content_area, right_sidebar, footer])?;
41+
let root = taffy.new_with_children(root_style, &[header, left_sidebar, content_area, right_sidebar, footer]);
4242

4343
// Compute layout and print result
44-
taffy.compute_layout(root, Size { width: length(800.0), height: length(600.0) })?;
44+
taffy.compute_layout(root, Size { width: length(800.0), height: length(600.0) });
4545
taffy::util::print_tree(&taffy, root);
46-
47-
Ok(())
4846
}

examples/nested.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use taffy::prelude::*;
22

3-
fn main() -> Result<(), taffy::TaffyError> {
3+
fn main() {
44
let mut taffy = Taffy::new();
55

66
// left
77
let child_t1 = taffy.new_leaf(Style {
88
size: Size { width: Dimension::Length(5.0), height: Dimension::Length(5.0) },
99
..Default::default()
10-
})?;
10+
});
1111

1212
let div1 = taffy.new_with_children(
1313
Style {
@@ -16,13 +16,13 @@ fn main() -> Result<(), taffy::TaffyError> {
1616
..Default::default()
1717
},
1818
&[child_t1],
19-
)?;
19+
);
2020

2121
// right
2222
let child_t2 = taffy.new_leaf(Style {
2323
size: Size { width: Dimension::Length(5.0), height: Dimension::Length(5.0) },
2424
..Default::default()
25-
})?;
25+
});
2626

2727
let div2 = taffy.new_with_children(
2828
Style {
@@ -31,25 +31,21 @@ fn main() -> Result<(), taffy::TaffyError> {
3131
..Default::default()
3232
},
3333
&[child_t2],
34-
)?;
34+
);
3535

3636
let container = taffy.new_with_children(
3737
Style { size: Size { width: Dimension::Percent(1.0), height: Dimension::Percent(1.0) }, ..Default::default() },
3838
&[div1, div2],
39-
)?;
39+
);
4040

4141
taffy.compute_layout(
4242
container,
4343
Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
44-
)?;
44+
);
4545

46-
println!("node: {:#?}", taffy.layout(container)?);
47-
48-
println!("div1: {:#?}", taffy.layout(div1)?);
49-
println!("div2: {:#?}", taffy.layout(div2)?);
50-
51-
println!("child1: {:#?}", taffy.layout(child_t1)?);
52-
println!("child2: {:#?}", taffy.layout(child_t2)?);
53-
54-
Ok(())
46+
println!("node: {:#?}", taffy.layout(container));
47+
println!("div1: {:#?}", taffy.layout(div1));
48+
println!("div2: {:#?}", taffy.layout(div2));
49+
println!("child1: {:#?}", taffy.layout(child_t1));
50+
println!("child2: {:#?}", taffy.layout(child_t2));
5551
}

src/compute/flexbox.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,12 +2120,12 @@ mod tests {
21202120
let mut tree = Taffy::with_capacity(16);
21212121

21222122
let style = Style::default();
2123-
let node_id = tree.new_leaf(style.clone()).unwrap();
2123+
let node_id = tree.new_leaf(style.clone());
21242124

21252125
let node_size = Size::NONE;
21262126
let parent_size = Size::NONE;
21272127

2128-
let constants = super::compute_constants(tree.style(node_id).unwrap(), node_size, parent_size);
2128+
let constants = super::compute_constants(tree.style(node_id), node_size, parent_size);
21292129

21302130
assert!(constants.dir == style.flex_direction);
21312131
assert!(constants.is_row == style.flex_direction.is_row());

src/compute/mod.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,29 +118,26 @@ mod tests {
118118

119119
let style: Style = Style { display: Display::Flex, size: Size::from_lengths(50.0, 50.0), ..Default::default() };
120120

121-
let grandchild_00 = taffy.new_leaf(style.clone()).unwrap();
122-
let grandchild_01 = taffy.new_leaf(style.clone()).unwrap();
123-
let child_00 = taffy.new_with_children(style.clone(), &[grandchild_00, grandchild_01]).unwrap();
121+
let grandchild_00 = taffy.new_leaf(style.clone());
122+
let grandchild_01 = taffy.new_leaf(style.clone());
123+
let child_00 = taffy.new_with_children(style.clone(), &[grandchild_00, grandchild_01]);
124124

125-
let grandchild_02 = taffy.new_leaf(style.clone()).unwrap();
126-
let child_01 = taffy.new_with_children(style.clone(), &[grandchild_02]).unwrap();
125+
let grandchild_02 = taffy.new_leaf(style.clone());
126+
let child_01 = taffy.new_with_children(style.clone(), &[grandchild_02]);
127127

128-
let root = taffy
129-
.new_with_children(
130-
Style { display: Display::None, size: Size::from_lengths(50.0, 50.0), ..Default::default() },
131-
&[child_00, child_01],
132-
)
133-
.unwrap();
128+
let root = taffy.new_with_children(
129+
Style { display: Display::None, size: Size::from_lengths(50.0, 50.0), ..Default::default() },
130+
&[child_00, child_01],
131+
);
134132

135-
perform_hidden_layout(&mut taffy, root.into());
133+
perform_hidden_layout(&mut taffy, root);
136134

137135
// Whatever size and display-mode the nodes had previously,
138136
// all layouts should resolve to ZERO due to the root's DISPLAY::NONE
139137
for (node, _) in taffy.nodes.iter().filter(|(node, _)| *node != root.into()) {
140-
if let Ok(layout) = taffy.layout(node.into()) {
141-
assert_eq!(layout.size, Size::zero());
142-
assert_eq!(layout.location, Point::zero());
143-
}
138+
let layout = taffy.layout(node.into());
139+
assert_eq!(layout.size, Size::zero());
140+
assert_eq!(layout.location, Point::zero());
144141
}
145142
}
146143
}

src/compute/taffy_tree.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::compute::{leaf, LayoutAlgorithm};
44
use crate::geometry::{Line, Point, Size};
55
use crate::style::{AvailableSpace, Display};
6-
use crate::tree::{Layout, LayoutTree, NodeId, RunMode, SizeBaselinesAndMargins, SizingMode, Taffy, TaffyError};
6+
use crate::tree::{Layout, LayoutTree, NodeId, RunMode, SizeBaselinesAndMargins, SizingMode, Taffy};
77
use crate::util::sys::round;
88

99
#[cfg(feature = "block_layout")]
@@ -34,11 +34,7 @@ fn debug_log_node(
3434
}
3535

3636
/// Updates the stored layout of the provided `node` and its children
37-
pub(crate) fn compute_layout(
38-
taffy: &mut Taffy,
39-
root: NodeId,
40-
available_space: Size<AvailableSpace>,
41-
) -> Result<(), TaffyError> {
37+
pub(crate) fn compute_layout(taffy: &mut Taffy, root: NodeId, available_space: Size<AvailableSpace>) {
4238
// Recursively compute node layout
4339
let size_and_baselines = perform_node_layout(
4440
taffy,
@@ -57,8 +53,6 @@ pub(crate) fn compute_layout(
5753
if taffy.config.use_rounding {
5854
round_layout(taffy, root, 0.0, 0.0);
5955
}
60-
61-
Ok(())
6256
}
6357

6458
/// Perform full layout on a node. Chooses which algorithm to use based on the `display` property.

0 commit comments

Comments
 (0)