Skip to content

Commit

Permalink
Use tagged pointer to represent size/length values (DioxusLabs#769)
Browse files Browse the repository at this point in the history
* Move AvailableSpace to it's own module

Signed-off-by: Nico Burns <[email protected]>

* Implement CompactLength representation

* Update tests

Signed-off-by: Nico Burns <[email protected]>

* Bump MSRV to 1.83

Signed-off-by: Nico Burns <[email protected]>

---------

Signed-off-by: Nico Burns <[email protected]>
  • Loading branch information
nicoburns authored Jan 6, 2025
1 parent 9978ebc commit afa6437
Show file tree
Hide file tree
Showing 967 changed files with 13,868 additions and 17,545 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ jobs:
# We only run `cargo build` (not `cargo test`) so as to avoid requiring dev-dependencies to build with the MSRV
# version. Building is likely sufficient as runtime errors varying between rust versions is very unlikely.
build-features-msrv:
name: "MSRV Build [Rust 1.65]"
name: "MSRV Build [Rust 1.83]"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.65
toolchain: 1.83
- run: cargo build

build-features-debug:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = [
"Nico Burns <[email protected]>",
]
edition = "2021"
rust-version = "1.65"
rust-version = "1.83"
include = ["src/**/*", "examples/**/*", "Cargo.toml", "README.md"]
description = "A flexible UI layout library "
repository = "https://github.com/DioxusLabs/taffy"
Expand Down
6 changes: 3 additions & 3 deletions benches/benches/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use taffy_benchmarks::yoga_helpers::YogaTreeBuilder;

fn random_dimension(rng: &mut impl Rng) -> Dimension {
match rng.gen_range(0.0..=1.0) {
rand if rand < 0.2 => Dimension::Auto,
rand if rand < 0.8 => Dimension::Length(rng.gen_range(0.0..500.0)),
_ => Dimension::Percent(rng.gen_range(0.0..1.0)),
rand if rand < 0.2 => auto(),
rand if rand < 0.8 => length(rng.gen_range(0.0..500.0)),
_ => percent(rng.gen_range(0.0..1.0)),
}
}

Expand Down
11 changes: 7 additions & 4 deletions benches/src/yoga_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,13 @@ fn set_node_children(tree: &mut yg::YogaTree, node_id: yg::DefaultKey, children:
}

fn into_yg_units(dim: impl Into<tf::Dimension>) -> yg::StyleUnit {
match dim.into() {
tf::Dimension::Auto => yg::StyleUnit::Auto,
tf::Dimension::Length(val) => yg::StyleUnit::Point(yg::OrderedFloat(val)),
tf::Dimension::Percent(val) => yg::StyleUnit::Percent(yg::OrderedFloat(val)),
let dim: tf::Dimension = dim.into();
let val = dim.into_raw().value();
match dim.into_raw().tag() {
tf::CompactLength::AUTO_TAG => yg::StyleUnit::Auto,
tf::CompactLength::LENGTH_TAG => yg::StyleUnit::Point(yg::OrderedFloat(val)),
tf::CompactLength::PERCENT_TAG => yg::StyleUnit::Percent(yg::OrderedFloat(val)),
_ => unreachable!(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ fn main() -> Result<(), taffy::TaffyError> {
let mut taffy: TaffyTree<()> = TaffyTree::new();

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

let node = taffy.new_with_children(
Style {
size: Size { width: Dimension::Length(100.0), height: Dimension::Length(100.0) },
size: Size { width: Dimension::from_length(100.0), height: Dimension::from_length(100.0) },
justify_content: Some(JustifyContent::Center),
..Default::default()
},
Expand Down
13 changes: 8 additions & 5 deletions examples/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ fn main() -> Result<(), taffy::TaffyError> {

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

let div1 = taffy.new_with_children(
Style {
size: Size { width: Dimension::Percent(0.5), height: Dimension::Percent(1.0) },
size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
// justify_content: JustifyContent::Center,
..Default::default()
},
Expand All @@ -20,21 +20,24 @@ fn main() -> Result<(), taffy::TaffyError> {

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

let div2 = taffy.new_with_children(
Style {
size: Size { width: Dimension::Percent(0.5), height: Dimension::Percent(1.0) },
size: Size { width: Dimension::from_percent(0.5), height: Dimension::from_percent(1.0) },
// justify_content: JustifyContent::Center,
..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() },
Style {
size: Size { width: Dimension::from_percent(1.0), height: Dimension::from_percent(1.0) },
..Default::default()
},
&[div1, div2],
)?;

Expand Down
16 changes: 8 additions & 8 deletions scripts/gentest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,11 @@ fn generate_length_percentage(dimen: &serde_json::Map<String, Value>) -> TokenSt
Value::String(ref unit) => match unit.as_ref() {
"px" => {
let value = value();
quote!(taffy::style::LengthPercentage::Length(#value))
quote!(length(#value))
}
"percent" => {
let value = value();
quote!(taffy::style::LengthPercentage::Percent(#value))
quote!(percent(#value))
}
_ => unreachable!(),
},
Expand All @@ -737,14 +737,14 @@ fn generate_length_percentage_auto(dimen: &serde_json::Map<String, Value>) -> To

match unit {
Value::String(ref unit) => match unit.as_ref() {
"auto" => quote!(taffy::style::LengthPercentageAuto::Auto),
"auto" => quote!(taffy::style::LengthPercentageAuto::AUTO),
"px" => {
let value = value();
quote!(taffy::style::LengthPercentageAuto::Length(#value))
quote!(length(#value))
}
"percent" => {
let value = value();
quote!(taffy::style::LengthPercentageAuto::Percent(#value))
quote!(percent(#value))
}
_ => unreachable!(),
},
Expand All @@ -758,14 +758,14 @@ fn generate_dimension(dimen: &serde_json::Map<String, Value>) -> TokenStream {

match unit {
Value::String(ref unit) => match unit.as_ref() {
"auto" => quote!(taffy::style::Dimension::Auto),
"auto" => quote!(taffy::style::Dimension::AUTO),
"px" => {
let value = value();
quote!(taffy::style::Dimension::Length(#value))
quote!(taffy::style::Dimension::from_length(#value))
}
"percent" => {
let value = value();
quote!(taffy::style::Dimension::Percent(#value))
quote!(taffy::style::Dimension::from_percent(#value))
}
_ => unreachable!(),
},
Expand Down
8 changes: 4 additions & 4 deletions src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use crate::compute::common::alignment::compute_alignment_offset;
use crate::geometry::{Line, Point, Rect, Size};
use crate::style::{
AlignContent, AlignItems, AlignSelf, AvailableSpace, Dimension, FlexWrap, JustifyContent, LengthPercentageAuto,
Overflow, Position,
AlignContent, AlignItems, AlignSelf, AvailableSpace, FlexWrap, JustifyContent, LengthPercentageAuto, Overflow,
Position,
};
use crate::style::{CoreStyle, FlexDirection, FlexboxContainerStyle, FlexboxItemStyle};
use crate::style_helpers::{TaffyMaxContent, TaffyMinContent};
Expand Down Expand Up @@ -527,7 +527,7 @@ fn generate_anonymous_flex_items(

inset: child_style.inset().zip_size(constants.node_inner_size, |p, s| p.maybe_resolve(s)),
margin: child_style.margin().resolve_or_zero(constants.node_inner_size.width),
margin_is_auto: child_style.margin().map(|m| m == LengthPercentageAuto::Auto),
margin_is_auto: child_style.margin().map(LengthPercentageAuto::is_auto),
padding: child_style.padding().resolve_or_zero(constants.node_inner_size.width),
border: child_style.border().resolve_or_zero(constants.node_inner_size.width),
align_self: child_style.align_self().unwrap_or(constants.align_items),
Expand Down Expand Up @@ -1578,7 +1578,7 @@ fn determine_used_cross_size(
if child.align_self == AlignSelf::Stretch
&& !child.margin_is_auto.cross_start(constants.dir)
&& !child.margin_is_auto.cross_end(constants.dir)
&& child_style.size().cross(constants.dir) == Dimension::Auto
&& child_style.size().cross(constants.dir).is_auto()
{
// For some reason this particular usage of max_width is an exception to the rule that max_width's transfer
// using the aspect_ratio (if set). Both Chrome and Firefox agree on this. And reading the spec, it seems like
Expand Down
50 changes: 27 additions & 23 deletions src/compute/grid/explicit_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,16 +555,20 @@ mod test {

#[test]
fn test_initialize_grid_tracks() {
let px0 = LengthPercentage::Length(0.0);
let px20 = LengthPercentage::Length(20.0);
let px100 = LengthPercentage::Length(100.0);
let minpx0 = MinTrackSizingFunction::from_length(0.0);
let minpx20 = MinTrackSizingFunction::from_length(20.0);
let minpx100 = MinTrackSizingFunction::from_length(100.0);

let maxpx0 = MaxTrackSizingFunction::from_length(0.0);
let maxpx20 = MaxTrackSizingFunction::from_length(20.0);
let maxpx100 = MaxTrackSizingFunction::from_length(100.0);

// Setup test
let track_template = vec![length(100.0), minmax(length(100.0), fr(2.0)), fr(1.0)];
let track_counts =
TrackCounts { negative_implicit: 3, explicit: track_template.len() as u16, positive_implicit: 3 };
let auto_tracks = vec![auto(), length(100.0)];
let gap = px20;
let gap = LengthPercentage::from_length(20.0);

// Call function
let mut tracks = Vec::new();
Expand All @@ -573,28 +577,28 @@ mod test {
// Assertions
let expected = vec![
// Gutter
(GridTrackKind::Gutter, MinTrackSizingFunction::Fixed(px0), MaxTrackSizingFunction::Fixed(px0)),
(GridTrackKind::Gutter, minpx0, maxpx0),
// Negative implicit tracks
(GridTrackKind::Track, MinTrackSizingFunction::Fixed(px100), MaxTrackSizingFunction::Fixed(px100)),
(GridTrackKind::Gutter, MinTrackSizingFunction::Fixed(px20), MaxTrackSizingFunction::Fixed(px20)),
(GridTrackKind::Track, MinTrackSizingFunction::Auto, MaxTrackSizingFunction::Auto),
(GridTrackKind::Gutter, MinTrackSizingFunction::Fixed(px20), MaxTrackSizingFunction::Fixed(px20)),
(GridTrackKind::Track, MinTrackSizingFunction::Fixed(px100), MaxTrackSizingFunction::Fixed(px100)),
(GridTrackKind::Gutter, MinTrackSizingFunction::Fixed(px20), MaxTrackSizingFunction::Fixed(px20)),
(GridTrackKind::Track, minpx100, maxpx100),
(GridTrackKind::Gutter, minpx20, maxpx20),
(GridTrackKind::Track, auto(), auto()),
(GridTrackKind::Gutter, minpx20, maxpx20),
(GridTrackKind::Track, minpx100, maxpx100),
(GridTrackKind::Gutter, minpx20, maxpx20),
// Explicit tracks
(GridTrackKind::Track, MinTrackSizingFunction::Fixed(px100), MaxTrackSizingFunction::Fixed(px100)),
(GridTrackKind::Gutter, MinTrackSizingFunction::Fixed(px20), MaxTrackSizingFunction::Fixed(px20)),
(GridTrackKind::Track, MinTrackSizingFunction::Fixed(px100), MaxTrackSizingFunction::Fraction(2.0)), // Note: separate min-max functions
(GridTrackKind::Gutter, MinTrackSizingFunction::Fixed(px20), MaxTrackSizingFunction::Fixed(px20)),
(GridTrackKind::Track, MinTrackSizingFunction::Auto, MaxTrackSizingFunction::Fraction(1.0)), // Note: min sizing function of flex sizing functions is auto
(GridTrackKind::Gutter, MinTrackSizingFunction::Fixed(px20), MaxTrackSizingFunction::Fixed(px20)),
(GridTrackKind::Track, minpx100, maxpx100),
(GridTrackKind::Gutter, minpx20, maxpx20),
(GridTrackKind::Track, minpx100, MaxTrackSizingFunction::from_fr(2.0)), // Note: separate min-max functions
(GridTrackKind::Gutter, minpx20, maxpx20),
(GridTrackKind::Track, auto(), MaxTrackSizingFunction::from_fr(1.0)), // Note: min sizing function of flex sizing functions is AUTO
(GridTrackKind::Gutter, minpx20, maxpx20),
// Positive implicit tracks
(GridTrackKind::Track, MinTrackSizingFunction::Auto, MaxTrackSizingFunction::Auto),
(GridTrackKind::Gutter, MinTrackSizingFunction::Fixed(px20), MaxTrackSizingFunction::Fixed(px20)),
(GridTrackKind::Track, MinTrackSizingFunction::Fixed(px100), MaxTrackSizingFunction::Fixed(px100)),
(GridTrackKind::Gutter, MinTrackSizingFunction::Fixed(px20), MaxTrackSizingFunction::Fixed(px20)),
(GridTrackKind::Track, MinTrackSizingFunction::Auto, MaxTrackSizingFunction::Auto),
(GridTrackKind::Gutter, MinTrackSizingFunction::Fixed(px0), MaxTrackSizingFunction::Fixed(px0)),
(GridTrackKind::Track, auto(), auto()),
(GridTrackKind::Gutter, minpx20, maxpx20),
(GridTrackKind::Track, minpx100, maxpx100),
(GridTrackKind::Gutter, minpx20, maxpx20),
(GridTrackKind::Track, auto(), auto()),
(GridTrackKind::Gutter, minpx0, maxpx0),
];

assert_eq!(tracks.len(), expected.len(), "Number of tracks doesn't match");
Expand Down
Loading

0 comments on commit afa6437

Please sign in to comment.