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 take self by value if we don't have to #498

Closed
Closed
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compute/grid/types/coordinates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl OriginZeroLine {
}

/// The minimum number of positive implicit track there must be if a grid item end at this line.
pub(crate) fn implied_positive_implicit_tracks(self, explicit_track_count: u16) -> u16 {
pub(crate) fn implied_positive_implicit_tracks(&self, explicit_track_count: u16) -> u16 {
if self.0 > explicit_track_count as i16 {
self.0 as u16 - explicit_track_count
} else {
Expand Down
94 changes: 48 additions & 46 deletions src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ impl AbsoluteAxis {
}
}

impl<T> Size<T> {
impl<T: Clone> 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.clone(),
AbsoluteAxis::Vertical => self.height.clone(),
}
}
}

impl<T: Add> Rect<T> {
impl<T: Add + Copy> Rect<T> {
#[inline(always)]
/// Get either the width or height depending on the AbsoluteAxis passed in
pub fn grid_axis_sum(self, axis: AbsoluteAxis) -> <T as Add>::Output {
pub fn grid_axis_sum(&self, axis: AbsoluteAxis) -> <T as Add>::Output {
match axis {
AbsoluteAxis::Horizontal => self.left + self.right,
AbsoluteAxis::Vertical => self.top + self.bottom,
Expand Down Expand Up @@ -127,14 +127,14 @@ impl<U, T: Add<U>> Add<Rect<U>> for Rect<T> {
}
}

impl<T> Rect<T> {
impl<T: Copy> 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(feature = "flexbox")]
pub(crate) fn zip_size<R, F, U>(self, size: Size<U>, f: F) -> Rect<R>
pub(crate) fn zip_size<R, F, U>(&self, size: Size<U>, f: F) -> Rect<R>
where
F: Fn(T, U) -> R,
U: Copy,
Expand All @@ -150,20 +150,20 @@ impl<T> Rect<T> {
/// 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<R, F>(self, f: F) -> Rect<R>
pub fn map<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> {
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> {
pub fn vertical_components(&self) -> Line<T> {
Line { start: self.top, end: self.bottom }
}
}
Expand Down Expand Up @@ -297,11 +297,11 @@ pub struct Line<T> {
pub end: T,
}

impl<T> Line<T> {
impl<T: Copy> Line<T> {
/// Applies the function `f` to both the width and height
///
/// This is used to transform a `Line<T>` into a `Line<R>`.
pub fn map<R, F>(self, f: F) -> Line<R>
pub fn map<R, F>(&self, f: F) -> Line<R>
where
F: Fn(T) -> R,
{
Expand Down Expand Up @@ -351,30 +351,44 @@ impl<U, T: Sub<U>> Sub<Size<U>> for Size<T> {
}
}

// Note: we allow dead_code here as we want to provide a complete API of helpers that is symetrical in all axes,
impl<T: Clone> Size<T> {
/// 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 {
if direction.is_row() {
self.height.clone()
} else {
self.width.clone()
}
}
}

// Note: we allow dead_code here as we want to provide a complete API of helpers that is symmetrical in all axes,
// but sometimes we only currently have a use for the helper in a single axis
#[allow(dead_code)]
impl<T> Size<T> {
impl<T: Copy> Size<T> {
/// 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<R, F>(self, f: F) -> Size<R>
pub fn map<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>
pub fn map_width<F>(&self, f: F) -> Size<T>
where
F: Fn(T) -> T,
{
Size { width: f(self.width), height: self.height }
}

/// Applies the function `f` to the height
pub fn map_height<F>(self, f: F) -> Size<T>
pub fn map_height<F>(&self, f: F) -> Size<T>
where
F: Fn(T) -> T,
{
Expand All @@ -383,7 +397,7 @@ impl<T> Size<T> {

/// Applies the function `f` to both the width and height
/// of this value and another passed value
pub fn zip_map<Other, Ret, Func>(self, other: Size<Other>, f: Func) -> Size<Ret>
pub fn zip_map<Other, Ret, Func>(&self, other: Size<Other>, f: Func) -> Size<Ret>
where
Func: Fn(T, Other) -> Ret,
{
Expand Down Expand Up @@ -474,30 +488,18 @@ 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
} else {
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 {
if direction.is_row() {
self.height
} else {
self.width
}
}

/// Gets the extent of the specified layout axis
/// Whether this is the width or height depends on the `GridAxis` provided
#[cfg(feature = "grid")]
pub(crate) fn get(self, axis: AbstractAxis) -> T {
pub(crate) fn get(&self, axis: AbstractAxis) -> T {
match axis {
AbstractAxis::Inline => self.width,
AbstractAxis::Block => self.height,
Expand All @@ -520,7 +522,7 @@ impl Size<f32> {
pub const ZERO: Size<f32> = Self { width: 0.0, height: 0.0 };

/// Applies f32_max to each component separately
pub fn f32_max(self, rhs: Size<f32>) -> Size<f32> {
pub fn f32_max(&self, rhs: Size<f32>) -> Size<f32> {
Size { width: f32_max(self.width, rhs.width), height: f32_max(self.height, rhs.height) }
}
}
Expand All @@ -540,26 +542,26 @@ impl Size<Option<f32>> {
/// - If height is `Some` but width is `None`, then width is computed from height and aspect_ratio
///
/// If aspect_ratio is `None` then this function simply returns self.
pub fn maybe_apply_aspect_ratio(self, aspect_ratio: Option<f32>) -> Size<Option<f32>> {
pub fn maybe_apply_aspect_ratio(&self, aspect_ratio: Option<f32>) -> Size<Option<f32>> {
match aspect_ratio {
Some(ratio) => match (self.width, self.height) {
(Some(width), None) => Size { width: Some(width), height: Some(width / ratio) },
(None, Some(height)) => Size { width: Some(height * ratio), height: Some(height) },
_ => self,
_ => *self,
},
None => self,
None => *self,
}
}
}

impl<T> Size<Option<T>> {
impl<T: Copy> Size<Option<T>> {
/// Performs Option::unwrap_or on each component separately
pub fn unwrap_or(self, alt: Size<T>) -> Size<T> {
pub fn unwrap_or(&self, alt: Size<T>) -> Size<T> {
Size { width: self.width.unwrap_or(alt.width), height: self.height.unwrap_or(alt.height) }
}

/// Performs Option::or on each component separately
pub fn or(self, alt: Size<Option<T>>) -> Size<Option<T>> {
pub fn or(&self, alt: Size<Option<T>>) -> Size<Option<T>> {
Size { width: self.width.or(alt.width), height: self.height.or(alt.height) }
}

Expand Down Expand Up @@ -615,11 +617,11 @@ impl<U, T: Add<U>> Add<Point<U>> for Point<T> {
}
}

impl<T> Point<T> {
impl<T: Copy> Point<T> {
/// Applies the function `f` to both the x and y
///
/// This is used to transform a `Point<T>` into a `Point<R>`.
pub fn map<R, F>(self, f: F) -> Point<R>
pub fn map<R, F>(&self, f: F) -> Point<R>
where
F: Fn(T) -> R,
{
Expand All @@ -629,15 +631,15 @@ impl<T> Point<T> {
/// Gets the extent of the specified layout axis
/// Whether this is the width or height depends on the `GridAxis` provided
#[cfg(feature = "grid")]
pub fn get(self, axis: AbstractAxis) -> T {
pub fn get(&self, axis: AbstractAxis) -> T {
match axis {
AbstractAxis::Inline => self.x,
AbstractAxis::Block => self.y,
}
}

/// Swap x and y components
pub fn transpose(self) -> Point<T> {
pub fn transpose(&self) -> Point<T> {
Point { x: self.y, y: self.x }
}

Expand All @@ -655,7 +657,7 @@ impl<T> Point<T> {
///
/// Whether this is the x or y 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.x
} else {
Expand All @@ -667,7 +669,7 @@ impl<T> Point<T> {
///
/// Whether this is the x or y 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.y
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/style/dimension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ impl LengthPercentageAuto {
/// - Some(resolved) using the provided context for Percent 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 {
Self::Length(length) => Some(length),
Self::Length(length) => Some(*length),
Self::Percent(percent) => Some(context * percent),
Self::Auto => None,
}
Expand Down Expand Up @@ -204,8 +204,8 @@ impl FromLength for AvailableSpace {

impl AvailableSpace {
/// Returns true for definite values, else false
pub fn is_definite(self) -> bool {
matches!(self, AvailableSpace::Definite(_))
pub fn is_definite(&self) -> bool {
matches!(*self, AvailableSpace::Definite(_))
}

/// Convert to Option
Expand Down
12 changes: 6 additions & 6 deletions src/style/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ impl Default for FlexDirection {
impl FlexDirection {
#[inline]
/// Is the direction [`FlexDirection::Row`] or [`FlexDirection::RowReverse`]?
pub(crate) fn is_row(self) -> bool {
matches!(self, Self::Row | Self::RowReverse)
pub(crate) fn is_row(&self) -> bool {
matches!(*self, Self::Row | Self::RowReverse)
}

#[inline]
/// Is the direction [`FlexDirection::Column`] or [`FlexDirection::ColumnReverse`]?
pub(crate) fn is_column(self) -> bool {
matches!(self, Self::Column | Self::ColumnReverse)
pub(crate) fn is_column(&self) -> bool {
matches!(*self, Self::Column | Self::ColumnReverse)
}

#[inline]
/// Is the direction [`FlexDirection::RowReverse`] or [`FlexDirection::ColumnReverse`]?
pub(crate) fn is_reverse(self) -> bool {
matches!(self, Self::RowReverse | Self::ColumnReverse)
pub(crate) fn is_reverse(&self) -> bool {
matches!(*self, Self::RowReverse | Self::ColumnReverse)
}
}

Expand Down
Loading