Skip to content

Commit

Permalink
Don't return TaffyResult when Taffy methods can't fail.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gibbz00 committed Jul 20, 2023
1 parent 6cf7960 commit 4a9ac5e
Show file tree
Hide file tree
Showing 15 changed files with 759 additions and 944 deletions.
18 changes: 7 additions & 11 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use taffy::prelude::*;

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

let child = taffy.new_leaf(Style {
size: Size { width: Dimension::Percent(0.5), height: Dimension::Auto },
..Default::default()
})?;
});

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

taffy.compute_layout(
node,
Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
)?;
taffy
.compute_layout(node, Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) });

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

println!("node: {:#?}", taffy.layout(node)?);
println!("child: {:#?}", taffy.layout(child)?);

Ok(())
println!("node: {:#?}", taffy.layout(node));
println!("child: {:#?}", taffy.layout(child));
}
14 changes: 6 additions & 8 deletions examples/flexbox_gap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ use taffy::prelude::*;
// Creates three 20px x 20px children, evenly spaced 10px apart from each other
// Thus the container is 80px x 20px.

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

let child_style = Style { size: Size { width: length(20.0), height: length(20.0) }, ..Default::default() };
let child0 = taffy.new_leaf(child_style.clone())?;
let child1 = taffy.new_leaf(child_style.clone())?;
let child2 = taffy.new_leaf(child_style.clone())?;
let child0 = taffy.new_leaf(child_style.clone());
let child1 = taffy.new_leaf(child_style.clone());
let child2 = taffy.new_leaf(child_style.clone());

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

// Compute layout and print result
taffy.compute_layout(root, Size::MAX_CONTENT)?;
taffy.compute_layout(root, Size::MAX_CONTENT);
taffy::util::print_tree(&taffy, root);

Ok(())
}
18 changes: 8 additions & 10 deletions examples/grid_holy_grail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn default<T: Default>() -> T {
}

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

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

// Define the child nodes
let header = taffy.new_leaf(Style { grid_row: line(1), grid_column: span(3), ..default() })?;
let left_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(1), ..default() })?;
let content_area = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(2), ..default() })?;
let right_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(3), ..default() })?;
let footer = taffy.new_leaf(Style { grid_row: line(3), grid_column: span(3), ..default() })?;
let header = taffy.new_leaf(Style { grid_row: line(1), grid_column: span(3), ..default() });
let left_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(1), ..default() });
let content_area = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(2), ..default() });
let right_sidebar = taffy.new_leaf(Style { grid_row: line(2), grid_column: line(3), ..default() });
let footer = taffy.new_leaf(Style { grid_row: line(3), grid_column: span(3), ..default() });

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

// Compute layout and print result
taffy.compute_layout(root, Size { width: length(800.0), height: length(600.0) })?;
taffy.compute_layout(root, Size { width: length(800.0), height: length(600.0) });
taffy::util::print_tree(&taffy, root);

Ok(())
}
28 changes: 12 additions & 16 deletions examples/nested.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use taffy::prelude::*;

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

// left
let child_t1 = taffy.new_leaf(Style {
size: Size { width: Dimension::Length(5.0), height: Dimension::Length(5.0) },
..Default::default()
})?;
});

let div1 = taffy.new_with_children(
Style {
Expand All @@ -16,13 +16,13 @@ fn main() -> Result<(), taffy::TaffyError> {
..Default::default()
},
&[child_t1],
)?;
);

// right
let child_t2 = taffy.new_leaf(Style {
size: Size { width: Dimension::Length(5.0), height: Dimension::Length(5.0) },
..Default::default()
})?;
});

let div2 = taffy.new_with_children(
Style {
Expand All @@ -31,25 +31,21 @@ fn main() -> Result<(), taffy::TaffyError> {
..Default::default()
},
&[child_t2],
)?;
);

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

taffy.compute_layout(
container,
Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
)?;
);

println!("node: {:#?}", taffy.layout(container)?);

println!("div1: {:#?}", taffy.layout(div1)?);
println!("div2: {:#?}", taffy.layout(div2)?);

println!("child1: {:#?}", taffy.layout(child_t1)?);
println!("child2: {:#?}", taffy.layout(child_t2)?);

Ok(())
println!("node: {:#?}", taffy.layout(container));
println!("div1: {:#?}", taffy.layout(div1));
println!("div2: {:#?}", taffy.layout(div2));
println!("child1: {:#?}", taffy.layout(child_t1));
println!("child2: {:#?}", taffy.layout(child_t2));
}
4 changes: 2 additions & 2 deletions src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2120,12 +2120,12 @@ mod tests {
let mut tree = Taffy::with_capacity(16);

let style = Style::default();
let node_id = tree.new_leaf(style.clone()).unwrap();
let node_id = tree.new_leaf(style.clone());

let node_size = Size::NONE;
let parent_size = Size::NONE;

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

assert!(constants.dir == style.flex_direction);
assert!(constants.is_row == style.flex_direction.is_row());
Expand Down
29 changes: 13 additions & 16 deletions src/compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,29 +118,26 @@ mod tests {

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

let grandchild_00 = taffy.new_leaf(style.clone()).unwrap();
let grandchild_01 = taffy.new_leaf(style.clone()).unwrap();
let child_00 = taffy.new_with_children(style.clone(), &[grandchild_00, grandchild_01]).unwrap();
let grandchild_00 = taffy.new_leaf(style.clone());
let grandchild_01 = taffy.new_leaf(style.clone());
let child_00 = taffy.new_with_children(style.clone(), &[grandchild_00, grandchild_01]);

let grandchild_02 = taffy.new_leaf(style.clone()).unwrap();
let child_01 = taffy.new_with_children(style.clone(), &[grandchild_02]).unwrap();
let grandchild_02 = taffy.new_leaf(style.clone());
let child_01 = taffy.new_with_children(style.clone(), &[grandchild_02]);

let root = taffy
.new_with_children(
Style { display: Display::None, size: Size::from_lengths(50.0, 50.0), ..Default::default() },
&[child_00, child_01],
)
.unwrap();
let root = taffy.new_with_children(
Style { display: Display::None, size: Size::from_lengths(50.0, 50.0), ..Default::default() },
&[child_00, child_01],
);

perform_hidden_layout(&mut taffy, root.into());
perform_hidden_layout(&mut taffy, root);

// Whatever size and display-mode the nodes had previously,
// all layouts should resolve to ZERO due to the root's DISPLAY::NONE
for (node, _) in taffy.nodes.iter().filter(|(node, _)| *node != root.into()) {
if let Ok(layout) = taffy.layout(node.into()) {
assert_eq!(layout.size, Size::zero());
assert_eq!(layout.location, Point::zero());
}
let layout = taffy.layout(node.into());
assert_eq!(layout.size, Size::zero());
assert_eq!(layout.location, Point::zero());
}
}
}
10 changes: 2 additions & 8 deletions src/compute/taffy_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::compute::{leaf, LayoutAlgorithm};
use crate::geometry::{Line, Point, Size};
use crate::style::{AvailableSpace, Display};
use crate::tree::{Layout, LayoutTree, NodeId, RunMode, SizeBaselinesAndMargins, SizingMode, Taffy, TaffyError};
use crate::tree::{Layout, LayoutTree, NodeId, RunMode, SizeBaselinesAndMargins, SizingMode, Taffy};
use crate::util::sys::round;

#[cfg(feature = "block_layout")]
Expand Down Expand Up @@ -34,11 +34,7 @@ fn debug_log_node(
}

/// Updates the stored layout of the provided `node` and its children
pub(crate) fn compute_layout(
taffy: &mut Taffy,
root: NodeId,
available_space: Size<AvailableSpace>,
) -> Result<(), TaffyError> {
pub(crate) fn compute_layout(taffy: &mut Taffy, root: NodeId, available_space: Size<AvailableSpace>) {
// Recursively compute node layout
let size_and_baselines = perform_node_layout(
taffy,
Expand All @@ -57,8 +53,6 @@ pub(crate) fn compute_layout(
if taffy.config.use_rounding {
round_layout(taffy, root, 0.0, 0.0);
}

Ok(())
}

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

0 comments on commit 4a9ac5e

Please sign in to comment.