Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't return TaffyResult when Taffy methods can't fail. #520

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Be sure that your Chrome vesion matches the downloaded `chromedriver` version!

Once you have chromedriver installed and available in `PATH` you can re-generate all tests by running `cargo gentest`. You should not manually update the tests in `tests/generated`. Instead, fix the script in `scripts/gentest/` and re-generate them. This can happen after a refactor. It can be helpful to commit the updated tests in a dedicated commit so that they can be easier to ignore during review.

`cargo gentest` generates log events that can be viewed by setting the [`RUST_LOG`](https://docs.rs/env_logger/0.10.0/env_logger/#enabling-logging) env variable. E.g. `export RUST_LOG="info"`.

To add a new test case add another HTML file to `/test_fixtures` following the current tests as a template for new tests.

**Note: test fixtures (HTML files in the `text_fixtures` directory) that begin with an "x" are considered disabled, and the test generation script will not generate a test for them (and as the test generation script overwrites the entire directory when generating tests, this means that prefixing an existing test with an "x" and then running the test generation script will delete that test)**
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let header_node = taffy
size: Size { width: length(800.0), height: length(100.0) },
..Default::default()
},
).unwrap();
);

let body_node = taffy
.new_leaf(
Expand All @@ -40,7 +40,7 @@ let body_node = taffy
flex_grow: 1.0,
..Default::default()
},
).unwrap();
);

let root_node = taffy
.new_with_children(
Expand All @@ -51,18 +51,18 @@ let root_node = taffy
},
&[header_node, body_node],
)
.unwrap();
;

// Call compute_layout on the root of your tree to run the layout algorithm
taffy.compute_layout(root_node, Size::MAX_CONTENT).unwrap();
taffy.compute_layout(root_node, Size::MAX_CONTENT);

// Inspect the computed layout using taffy.layout
assert_eq!(taffy.layout(root_node).unwrap().size.width, 800.0);
assert_eq!(taffy.layout(root_node).unwrap().size.height, 600.0);
assert_eq!(taffy.layout(header_node).unwrap().size.width, 800.0);
assert_eq!(taffy.layout(header_node).unwrap().size.height, 100.0);
assert_eq!(taffy.layout(body_node).unwrap().size.width, 800.0);
assert_eq!(taffy.layout(body_node).unwrap().size.height, 500.0); // This value was not set explicitly, but was computed by Taffy
assert_eq!(taffy.layout(root_node).size.width, 800.0);
assert_eq!(taffy.layout(root_node).size.height, 600.0);
assert_eq!(taffy.layout(header_node).size.width, 800.0);
assert_eq!(taffy.layout(header_node).size.height, 100.0);
assert_eq!(taffy.layout(body_node).size.width, 800.0);
assert_eq!(taffy.layout(body_node).size.height, 500.0); // This value was not set explicitly, but was computed by Taffy

```

Expand Down
25 changes: 24 additions & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,31 @@

### Breaking

#### Fewer unwraps.

Infallible `Taffy` methods that have previously returned `TaffyResult` will simply return the `Ok` value only.
This change affects the following methods:

- `Taffy::add_child`
- `Taffy::child_count`
- `Taffy::children`
- `Taffy::compute_layout`
- `Taffy::dirty`
- `Taffy::layout`
- `Taffy::mark_dirty`
- `Taffy::new_leaf_with_measure`
- `Taffy::new_with_children`
- `Taffy::remove`
- `Taffy::set_children`
- `Taffy::set_measure`
- `Taffy::set_style`
- `Taffy::style`
- `Taffy::new_leaf`

#### Replace `Points` with `Length`

Many APIs have been renamed to replace `points` or `Points` with `length` or `Length`.
This new name better describes one-dimentional measure of space in some unspecified unit
This new name better describes one-dimensional measure of space in some unspecified unit
which is often unrelated to the PostScript point or the CSS `pt` unit.

This also removes a misleading similarity with the 2D `Point`,
Expand Down
24 changes: 12 additions & 12 deletions benches/benches/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use yoga_helpers::yg;

/// Build a random leaf node
fn build_random_leaf(taffy: &mut Taffy, rng: &mut ChaCha8Rng) -> NodeId {
taffy.new_with_children(Style::random(rng), &[]).unwrap()
taffy.new_with_children(Style::random(rng), &[])
}

/// A tree with many children that have shallow depth
Expand All @@ -29,13 +29,13 @@ fn build_taffy_flat_hierarchy(total_node_count: u32) -> (Taffy, NodeId) {
let sub_children_count = rng.gen_range(1..=4);
let sub_children: Vec<NodeId> =
(0..sub_children_count).map(|_| build_random_leaf(&mut taffy, &mut rng)).collect();
let node = taffy.new_with_children(Style::random(&mut rng), &sub_children).unwrap();
let node = taffy.new_with_children(Style::random(&mut rng), &sub_children);

children.push(node);
node_count += 1 + sub_children_count;
}

let root = taffy.new_with_children(Style::DEFAULT, children.as_slice()).unwrap();
let root = taffy.new_with_children(Style::DEFAULT, children.as_slice());
(taffy, root)
}

Expand Down Expand Up @@ -68,11 +68,11 @@ fn build_taffy_deep_hierarchy(node_count: u32, branching_factor: u32) -> (Taffy,
let mut build_leaf_node = |taffy: &mut Taffy| build_random_leaf(taffy, &mut rng);
let mut rng = ChaCha8Rng::seed_from_u64(12345);
let mut build_flex_node =
|taffy: &mut Taffy, children: Vec<NodeId>| taffy.new_with_children(Style::random(&mut rng), &children).unwrap();
|taffy: &mut Taffy, children: Vec<NodeId>| taffy.new_with_children(Style::random(&mut rng), &children);

let mut taffy = Taffy::new();
let tree = build_deep_tree(&mut taffy, node_count, branching_factor, &mut build_leaf_node, &mut build_flex_node);
let root = taffy.new_with_children(Style::DEFAULT, &tree).unwrap();
let root = taffy.new_with_children(Style::DEFAULT, &tree);
(taffy, root)
}

Expand Down Expand Up @@ -101,13 +101,13 @@ fn build_taffy_huge_nested_hierarchy(node_count: u32, branching_factor: u32) ->
flex_grow: 1.0,
..Default::default()
};
let mut build_leaf_node = |taffy: &mut Taffy| taffy.new_leaf(style.clone()).unwrap();
let mut build_leaf_node = |taffy: &mut Taffy| taffy.new_leaf(style.clone());
let mut build_flex_node =
|taffy: &mut Taffy, children: Vec<NodeId>| taffy.new_with_children(style.clone(), &children).unwrap();
|taffy: &mut Taffy, children: Vec<NodeId>| taffy.new_with_children(style.clone(), &children);

let mut taffy = Taffy::new();
let tree = build_deep_tree(&mut taffy, node_count, branching_factor, &mut build_leaf_node, &mut build_flex_node);
let root = taffy.new_with_children(Style::DEFAULT, &tree).unwrap();
let root = taffy.new_with_children(Style::DEFAULT, &tree);
(taffy, root)
}

Expand Down Expand Up @@ -159,7 +159,7 @@ fn taffy_benchmarks(c: &mut Criterion) {
group.bench_with_input(BenchmarkId::new("Taffy", node_count), node_count, |b, &node_count| {
b.iter_batched(
|| build_taffy_huge_nested_hierarchy(node_count, 10),
|(mut taffy, root)| taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(),
|(mut taffy, root)| taffy.compute_layout(root, Size::MAX_CONTENT),
criterion::BatchSize::SmallInput,
)
});
Expand All @@ -186,7 +186,7 @@ fn taffy_benchmarks(c: &mut Criterion) {
group.bench_with_input(benchmark_id, node_count, |b, &node_count| {
b.iter_batched(
|| build_taffy_flat_hierarchy(node_count),
|(mut taffy, root)| taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(),
|(mut taffy, root)| taffy.compute_layout(root, Size::MAX_CONTENT),
criterion::BatchSize::SmallInput,
)
});
Expand All @@ -211,7 +211,7 @@ fn taffy_benchmarks(c: &mut Criterion) {
group.bench_with_input(BenchmarkId::new(format!("Taffy {label}"), node_count), node_count, |b, &node_count| {
b.iter_batched(
|| build_taffy_deep_hierarchy(node_count, 2),
|(mut taffy, root)| taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(),
|(mut taffy, root)| taffy.compute_layout(root, Size::MAX_CONTENT),
criterion::BatchSize::SmallInput,
)
});
Expand All @@ -234,7 +234,7 @@ fn taffy_benchmarks(c: &mut Criterion) {
group.bench_with_input(BenchmarkId::new("Taffy", node_count), node_count, |b, &node_count| {
b.iter_batched(
|| build_taffy_deep_hierarchy(node_count, 2),
|(mut taffy, root)| taffy.compute_layout(root, Size::MAX_CONTENT).unwrap(),
|(mut taffy, root)| taffy.compute_layout(root, Size::MAX_CONTENT),
criterion::BatchSize::SmallInput,
)
});
Expand Down
15 changes: 7 additions & 8 deletions benches/benches/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use taffy::style::Style;

/// Build a random leaf node
fn build_random_leaf(taffy: &mut Taffy, _rng: &mut ChaCha8Rng) -> NodeId {
taffy.new_with_children(Style { size: length(20.0), ..Default::default() }, &[]).unwrap()
taffy.new_with_children(Style { size: length(20.0), ..Default::default() }, &[])
}

fn random_grid_track<R: Rng>(rng: &mut R) -> TrackSizingFunction {
Expand Down Expand Up @@ -54,8 +54,7 @@ fn build_grid_flat_hierarchy(col_count: usize, row_count: usize) -> (Taffy, Node
let children: Vec<_> =
iter::from_fn(|| Some(build_random_leaf(&mut taffy, &mut rng))).take(col_count * row_count).collect();

let root = taffy.new_with_children(style, children.as_slice()).unwrap();
(taffy, root)
let root = taffy.new_with_children(style, children.as_slice())(taffy, root);
}

/// A helper function to recursively construct a deep tree
Expand Down Expand Up @@ -91,12 +90,12 @@ fn build_taffy_deep_grid_hierarchy(levels: usize, track_count: usize) -> (Taffy,
let mut build_leaf_node = |taffy: &mut Taffy| build_random_leaf(taffy, &mut rng);
let mut rng = ChaCha8Rng::seed_from_u64(12345);
let mut build_flex_node = |taffy: &mut Taffy, children: Vec<NodeId>| {
taffy.new_with_children(random_nxn_grid_style(&mut rng, track_count), &children).unwrap()
taffy.new_with_children(random_nxn_grid_style(&mut rng, track_count), &children)
};

let mut taffy = Taffy::new();
let tree = build_deep_grid_tree(&mut taffy, levels, track_count, &mut build_leaf_node, &mut build_flex_node);
let root = taffy.new_with_children(Style::DEFAULT, &tree).unwrap();
let root = taffy.new_with_children(Style::DEFAULT, &tree);
(taffy, root)
}

Expand All @@ -110,7 +109,7 @@ fn taffy_benchmarks(c: &mut Criterion) {
|b, &track_count| {
b.iter_batched(
|| build_grid_flat_hierarchy(track_count, track_count),
|(mut taffy, root)| taffy.compute_layout(root, length(12000.0)).unwrap(),
|(mut taffy, root)| taffy.compute_layout(root, length(12000.0)),
criterion::BatchSize::SmallInput,
)
},
Expand All @@ -128,7 +127,7 @@ fn taffy_benchmarks(c: &mut Criterion) {
|b, &(levels, tracks)| {
b.iter_batched(
|| build_taffy_deep_grid_hierarchy(levels, tracks),
|(mut taffy, root)| taffy.compute_layout(root, length(12000.0)).unwrap(),
|(mut taffy, root)| taffy.compute_layout(root, length(12000.0)),
criterion::BatchSize::SmallInput,
)
},
Expand All @@ -142,7 +141,7 @@ fn taffy_benchmarks(c: &mut Criterion) {
group.bench_with_input(BenchmarkId::new("1x1", levels), levels, |b, &levels| {
b.iter_batched(
|| build_taffy_deep_grid_hierarchy(levels, 1),
|(mut taffy, root)| taffy.compute_layout(root, max_content()).unwrap(),
|(mut taffy, root)| taffy.compute_layout(root, max_content()),
criterion::BatchSize::SmallInput,
)
});
Expand Down
6 changes: 3 additions & 3 deletions benches/benches/tree_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use yoga_helpers::yg;

/// Build a random leaf node
fn build_random_leaf(taffy: &mut Taffy) -> NodeId {
taffy.new_with_children(Style::DEFAULT, &[]).unwrap()
taffy.new_with_children(Style::DEFAULT, &[])
}

/// A tree with many children that have shallow depth
Expand All @@ -27,13 +27,13 @@ fn build_taffy_flat_hierarchy(total_node_count: u32, use_with_capacity: bool) ->
while node_count < total_node_count {
let sub_children_count = rng.gen_range(1..=4);
let sub_children: Vec<NodeId> = (0..sub_children_count).map(|_| build_random_leaf(&mut taffy)).collect();
let node = taffy.new_with_children(Style::DEFAULT, &sub_children).unwrap();
let node = taffy.new_with_children(Style::DEFAULT, &sub_children);

children.push(node);
node_count += 1 + sub_children_count;
}

let root = taffy.new_with_children(Style::DEFAULT, children.as_slice()).unwrap();
let root = taffy.new_with_children(Style::DEFAULT, children.as_slice());
(taffy, root)
}

Expand Down
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(())
}
Loading