Skip to content

Commit

Permalink
Taking a few things by ref
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoISnoTarget committed Jul 12, 2024
1 parent acf450a commit dbd9343
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 159 deletions.
7 changes: 3 additions & 4 deletions src/compute/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ fn perform_final_layout_on_in_flow_children(
if item.position == Position::Absolute {
item.static_position.y = committed_y_offset;
} else {
let item_margin = item.margin.clone().map(|margin| margin.resolve_to_option(container_outer_width));
let item_margin = item.margin.map_ref(|margin| margin.resolve_to_option(container_outer_width));
let item_non_auto_margin = item_margin.map(|m| m.unwrap_or(0.0));
let item_non_auto_x_margin_sum = item_non_auto_margin.horizontal_axis_sum();
let known_dimensions = item
Expand Down Expand Up @@ -398,8 +398,7 @@ fn perform_final_layout_on_in_flow_children(
// Resolve item inset
let inset = item
.inset
.clone()
.zip_size(Size { width: container_inner_width, height: 0.0 }, |p, s| p.maybe_resolve(s));
.zip_size_ref(Size { width: container_inner_width, height: 0.0 }, |p, s| p.maybe_resolve(s));
let inset_offset = Point {
x: inset.left.or(inset.right.map(|x| -x)).unwrap_or(0.0),
y: inset.top.or(inset.bottom.map(|x| -x)).unwrap_or(0.0),
Expand Down Expand Up @@ -508,7 +507,7 @@ fn perform_absolute_layout_on_absolute_children(
}

let aspect_ratio = child_style.aspect_ratio;
let margin = child_style.margin.clone().map(|margin| margin.resolve_to_option(area_width));
let margin = child_style.margin.map_ref(|margin| margin.resolve_to_option(area_width));
let padding = child_style.padding.resolve_or_zero(Some(area_width));
let border = child_style.border.resolve_or_zero(Some(area_width));
let padding_border_sum = (padding + border).sum_axes();
Expand Down
108 changes: 54 additions & 54 deletions src/compute/flexbox.rs

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/compute/grid/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ pub(super) fn align_and_position_item(

let position = style.position;
let inset_horizontal =
style.inset.clone().horizontal_components().map(|size| size.resolve_to_option(grid_area_size.width));
style.inset.horizontal_components().map(|size| size.resolve_to_option(grid_area_size.width));
let inset_vertical =
style.inset.clone().vertical_components().map(|size| size.resolve_to_option(grid_area_size.height));
let padding = style.padding.clone().map(|p| p.resolve_or_zero(Some(grid_area_size.width)));
let border = style.border.clone().map(|p| p.resolve_or_zero(Some(grid_area_size.width)));
style.inset.vertical_components().map(|size| size.resolve_to_option(grid_area_size.height));
let padding = style.padding.map_ref(|p| p.resolve_or_zero(Some(grid_area_size.width)));
let border = style.border.map_ref(|p| p.resolve_or_zero(Some(grid_area_size.width)));
let padding_border_size = (padding + border).sum_axes();
let inherent_size = style.size.maybe_resolve(grid_area_size).maybe_apply_aspect_ratio(aspect_ratio);
let min_size = style
Expand Down Expand Up @@ -113,7 +113,7 @@ pub(super) fn align_and_position_item(

// Note: This is not a bug. It is part of the CSS spec that both horizontal and vertical margins
// resolve against the WIDTH of the grid area.
let margin = style.margin.clone().map(|margin| margin.resolve_to_option(grid_area_size.width));
let margin = style.margin.map_ref(|margin| margin.resolve_to_option(grid_area_size.width));

let grid_area_minus_item_margins_size = Size {
width: grid_area_size.width.maybe_sub(margin.left).maybe_sub(margin.right),
Expand Down Expand Up @@ -195,7 +195,7 @@ pub(super) fn align_and_position_item(
width,
position,
inset_horizontal,
margin.horizontal_components(),
margin.horizontal_components().map(|e|*e),
0.0,
);
let (y, y_margin) = align_item_within_area(
Expand All @@ -204,7 +204,7 @@ pub(super) fn align_and_position_item(
height,
position,
inset_vertical,
margin.vertical_components(),
margin.vertical_components().map(|e| *e),
baseline_shim,
);

Expand Down
32 changes: 16 additions & 16 deletions src/compute/grid/explicit_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ pub(crate) fn compute_explicit_grid_size_in_axis(
// - then the number of repetitions is the smallest possible positive integer that fulfills that minimum requirement
// Otherwise, the specified track list repeats only once.
let style_size = preferred_size.get_abs(axis);
let style_min_size = style.min_size.clone().get_abs(axis).into_option();
let style_max_size = style.max_size.clone().get_abs(axis).into_option();
let style_min_size = style.min_size.get_abs(axis).to_option();
let style_max_size = style.max_size.get_abs(axis).to_option();

let outer_container_size = style_size.maybe_min(style_max_size).or(style_max_size).or(style_min_size);
let inner_container_size = outer_container_size.map(|size| {
Expand All @@ -113,8 +113,8 @@ pub(crate) fn compute_explicit_grid_size_in_axis(
/// ...treating each track as its max track sizing function if that is definite or as its minimum track sizing function
/// otherwise, flooring the max track sizing function by the min track sizing function if both are definite
fn track_definite_value(sizing_function: &NonRepeatedTrackSizingFunction, parent_size: Option<f32>) -> f32 {
let max_size = sizing_function.max.clone().definite_value(parent_size);
let min_size = sizing_function.max.clone().definite_value(parent_size);
let max_size = sizing_function.max.definite_value(parent_size);
let min_size = sizing_function.max.definite_value(parent_size);
max_size.map(|max| max.maybe_min(min_size)).or(min_size).unwrap()
}

Expand All @@ -137,7 +137,7 @@ pub(crate) fn compute_explicit_grid_size_in_axis(
}
})
.sum();
let gap_size = style.gap.clone().get_abs(axis).resolve_or_zero(Some(inner_container_size));
let gap_size = style.gap.get_abs(axis).resolve_or_zero(Some(inner_container_size));

// Compute the amount of space that a single repetition of the repeated track list takes
let per_repetition_track_used_space: f32 = repetition_definition
Expand Down Expand Up @@ -194,17 +194,17 @@ pub(super) fn initialize_grid_tracks(
// and push the initial gutter
tracks.clear();
tracks.reserve((counts.len() * 2) + 1);
tracks.push(GridTrack::gutter(gap.clone()));
tracks.push(GridTrack::gutter(&gap));

// Create negative implicit tracks
if counts.negative_implicit > 0 {
if auto_tracks.is_empty() {
let iter = core::iter::repeat(NonRepeatedTrackSizingFunction::AUTO);
create_implicit_tracks(tracks, counts.negative_implicit, iter, gap.clone())
create_implicit_tracks(tracks, counts.negative_implicit, iter, &gap)
} else {
let offset = auto_tracks.len() - (counts.negative_implicit as usize % auto_tracks.len());
let iter = auto_tracks.iter().cloned().cycle().skip(offset);
create_implicit_tracks(tracks, counts.negative_implicit, iter, gap.clone())
create_implicit_tracks(tracks, counts.negative_implicit, iter, &gap)
}
}

Expand All @@ -222,7 +222,7 @@ pub(super) fn initialize_grid_tracks(
sizing_function.min_sizing_function(),
sizing_function.max_sizing_function(),
));
tracks.push(GridTrack::gutter(gap.clone()));
tracks.push(GridTrack::gutter(&gap));
current_track_index += 1;
}
TrackSizingFunction::Repeat(Count(count), repeated_tracks) => {
Expand All @@ -232,7 +232,7 @@ pub(super) fn initialize_grid_tracks(
sizing_function.min_sizing_function(),
sizing_function.max_sizing_function(),
));
tracks.push(GridTrack::gutter(gap.clone()));
tracks.push(GridTrack::gutter(&gap));
current_track_index += 1;
});
}
Expand All @@ -242,7 +242,7 @@ pub(super) fn initialize_grid_tracks(
for track_def in iter.take(auto_repeated_track_count) {
let mut track =
GridTrack::new(track_def.min_sizing_function(), track_def.max_sizing_function());
let mut gutter = GridTrack::gutter(gap.clone());
let mut gutter = GridTrack::gutter(&gap);

// Auto-fit tracks that don't contain should be collapsed.
if *repetition_kind == AutoFit && !track_has_items(current_track_index) {
Expand All @@ -263,10 +263,10 @@ pub(super) fn initialize_grid_tracks(
// Create positive implicit tracks
if auto_tracks.is_empty() {
let iter = core::iter::repeat(NonRepeatedTrackSizingFunction::AUTO);
create_implicit_tracks(tracks, counts.positive_implicit, iter, gap)
create_implicit_tracks(tracks, counts.positive_implicit, iter, &gap)
} else {
let iter = auto_tracks.iter().cloned().cycle();
create_implicit_tracks(tracks, counts.positive_implicit, iter, gap)
create_implicit_tracks(tracks, counts.positive_implicit, iter, &gap)
}

// Mark first and last grid lines as collapsed
Expand All @@ -279,12 +279,12 @@ fn create_implicit_tracks(
tracks: &mut Vec<GridTrack>,
count: u16,
mut auto_tracks_iter: impl Iterator<Item = NonRepeatedTrackSizingFunction>,
gap: LengthPercentage,
gap: &LengthPercentage,
) {
for _ in 0..count {
let track_def = auto_tracks_iter.next().unwrap();
tracks.push(GridTrack::new(track_def.min_sizing_function(), track_def.max_sizing_function()));
tracks.push(GridTrack::gutter(gap.clone()));
tracks.push(GridTrack::gutter(&gap));
}
}

Expand All @@ -301,7 +301,7 @@ mod test {
#[test]
fn explicit_grid_sizing_no_repeats() {
let grid_style = (600.0, 600.0, 2, 4).into_grid();
let preferred_size = grid_style.size.clone().map(|s| s.into_option());
let preferred_size = grid_style.size.map_ref(|s| s.to_option());
let width = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Horizontal);
let height = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Vertical);
assert_eq!(width, 2);
Expand Down
8 changes: 4 additions & 4 deletions src/compute/grid/types/grid_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ impl GridItem {
let spanned_tracks = &axis_tracks[self.track_range_excluding_lines(axis)];
let tracks_all_fixed = spanned_tracks
.iter()
.all(|track| track.max_track_sizing_function.clone().definite_limit(axis_parent_size).is_some());
.all(|track| track.max_track_sizing_function.definite_limit(axis_parent_size).is_some());
if tracks_all_fixed {
let limit: f32 = spanned_tracks
.iter()
.map(|track| track.max_track_sizing_function.clone().definite_limit(axis_parent_size).unwrap())
.map(|track| track.max_track_sizing_function.definite_limit(axis_parent_size).unwrap())
.sum();
Some(limit)
} else {
Expand All @@ -209,11 +209,11 @@ impl GridItem {
let spanned_tracks = &axis_tracks[self.track_range_excluding_lines(axis)];
let tracks_all_fixed = spanned_tracks
.iter()
.all(|track| track.max_track_sizing_function.clone().definite_value(axis_parent_size).is_some());
.all(|track| track.max_track_sizing_function.definite_value(axis_parent_size).is_some());
if tracks_all_fixed {
let limit: f32 = spanned_tracks
.iter()
.map(|track| track.max_track_sizing_function.clone().definite_value(axis_parent_size).unwrap())
.map(|track| track.max_track_sizing_function.definite_value(axis_parent_size).unwrap())
.sum();
Some(limit)
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/compute/grid/types/grid_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ impl GridTrack {
}

/// Create a new GridTrack representing a gutter
pub fn gutter(size: LengthPercentage) -> GridTrack {
pub fn gutter(size: &LengthPercentage) -> GridTrack {
Self::new_with_kind(
GridTrackKind::Gutter,
MinTrackSizingFunction::Fixed(size.clone()),
MaxTrackSizingFunction::Fixed(size),
MaxTrackSizingFunction::Fixed(size.clone()),
)
}

Expand All @@ -114,8 +114,8 @@ impl GridTrack {
#[inline(always)]
/// Returns true if the track is flexible (has a Flex MaxTrackSizingFunction), else false.
pub fn uses_percentage(&self) -> bool {
self.min_track_sizing_function.clone().uses_percentage()
|| self.max_track_sizing_function.clone().uses_percentage()
self.min_track_sizing_function.uses_percentage()
|| self.max_track_sizing_function.uses_percentage()
}

#[inline(always)]
Expand Down
66 changes: 53 additions & 13 deletions src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ impl AbsoluteAxis {
impl<T> Size<T> {
#[inline(always)]
/// Get either the width or height depending on the AbsoluteAxis passed in
pub fn get_abs(self, axis: AbsoluteAxis) -> T {
pub fn get_abs(&self, axis: AbsoluteAxis) -> &T {
match axis {
AbsoluteAxis::Horizontal => self.width,
AbsoluteAxis::Vertical => self.height,
AbsoluteAxis::Horizontal => &self.width,
AbsoluteAxis::Vertical => &self.height,
}
}
}
Expand Down Expand Up @@ -158,6 +158,25 @@ impl<T> Rect<T> {
}
}

/// Applies the function `f` to all four sides of the rect
///
/// When applied to the left and right sides, the width is used
/// as the second parameter of `f`.
/// When applied to the top or bottom sides, the height is used instead.
#[cfg(any(feature = "flexbox", feature = "block_layout"))]
pub(crate) fn zip_size_ref<R, F, U>(&self, size: Size<U>, f: F) -> Rect<R>
where
F: Fn(&T, U) -> R,
U: Copy,
{
Rect {
left: f(&self.left, size.width),
right: f(&self.right, size.width),
top: f(&self.top, size.height),
bottom: f(&self.bottom, size.height),
}
}

/// Applies the function `f` to the left, right, top, and bottom properties
///
/// This is used to transform a `Rect<T>` into a `Rect<R>`.
Expand All @@ -168,14 +187,24 @@ impl<T> Rect<T> {
Rect { left: f(self.left), right: f(self.right), top: f(self.top), bottom: f(self.bottom) }
}

/// Applies the function `f` to the left, right, top, and bottom properties
///
/// This is used to transform a `&Rect<T>` into a `Rect<R>`.
pub fn map_ref<R, F>(&self, f: F) -> Rect<R>
where
F: Fn(&T) -> R,
{
Rect { left: f(&self.left), right: f(&self.right), top: f(&self.top), bottom: f(&self.bottom) }
}

/// Returns a `Line<T>` representing the left and right properties of the Rect
pub fn horizontal_components(self) -> Line<T> {
Line { start: self.left, end: self.right }
pub fn horizontal_components(&self) -> Line<&T> {
Line { start: &self.left, end: &self.right }
}

/// Returns a `Line<T>` containing the top and bottom properties of the Rect
pub fn vertical_components(self) -> Line<T> {
Line { start: self.top, end: self.bottom }
pub fn vertical_components(&self) -> Line<&T> {
Line { start: &self.top, end: &self.bottom }
}
}

Expand Down Expand Up @@ -376,6 +405,17 @@ impl<T> Size<T> {
Size { width: f(self.width), height: f(self.height) }
}

/// Applies the function `f` to both the width and height
///
/// This is used to transform a `&Size<T>` into a `Size<R>`.
pub fn map_ref<R, F>(&self, f: F) -> Size<R>
where
F: Fn(&T) -> R,
{
Size { width: f(&self.width), height: f(&self.height) }
}


/// Applies the function `f` to the width
pub fn map_width<F>(self, f: F) -> Size<T>
where
Expand Down Expand Up @@ -485,23 +525,23 @@ impl<T> Size<T> {
///
/// Whether this is the width or height depends on the `direction` provided
#[cfg(feature = "flexbox")]
pub(crate) fn main(self, direction: FlexDirection) -> T {
pub(crate) fn main(&self, direction: FlexDirection) -> &T {
if direction.is_row() {
self.width
&self.width
} else {
self.height
&self.height
}
}

/// Gets the extent of the cross layout axis
///
/// Whether this is the width or height depends on the `direction` provided
#[cfg(feature = "flexbox")]
pub(crate) fn cross(self, direction: FlexDirection) -> T {
pub(crate) fn cross(&self, direction: FlexDirection) -> &T {
if direction.is_row() {
self.height
&self.height
} else {
self.width
&self.width
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/style/dimension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ impl LengthPercentageAuto {
/// - Some(resolved) using the provided context for Percent or Calc variants
/// - None for Auto variants
#[inline(always)]
pub fn resolve_to_option(self, context: f32) -> Option<f32> {
pub fn resolve_to_option(&self, context: f32) -> Option<f32> {
match self {
LengthPercentageAuto::Length(length) => Some(length),
LengthPercentageAuto::Length(length) => Some(*length),
LengthPercentageAuto::Percent(percent) => Some(context * percent),
#[cfg(feature = "calc")]
LengthPercentageAuto::Calc => self.get_calc().map(|calc| calc.resolve(Some(context))),
Expand Down Expand Up @@ -360,6 +360,13 @@ impl Dimension {
_ => None,
}
}
#[cfg(feature = "grid")]
pub fn to_option(&self) -> Option<f32> {
match self {
Dimension::Length(value) => Some(*value),
_ => None,
}
}
/// Returns true if value is Dimension::Auto
#[inline(always)]
pub fn is_auto(&self) -> bool {
Expand Down
Loading

0 comments on commit dbd9343

Please sign in to comment.