Add `const` annotations to appropriate functions (#892)
Signed-off-by: WaterWhisperer <waterwhisperer24@qq.com>
3cc24a253541875779d524c4c86b9d1bc8660ddf · by WaterWhisperer · (11 months ago) · parent 32db50d96 · browse the tree at this commit
Semantic delta · 35
src/compute/grid/types/grid_track.rs
src/compute/grid/types/grid_track_counts.rs
src/style/available_space.rs
diff --git a/src/compute/grid/types/grid_track.rs b/src/compute/grid/types/grid_track.rs
index bfd5a82a6..af23c40c7 100644
--- a/src/compute/grid/types/grid_track.rs
+++ b/src/compute/grid/types/grid_track.rs
@@ -61,7 +61,7 @@ pub(in super::super) struct GridTrack {
impl GridTrack {
/// GridTrack constructor with all configuration parameters for the other constructors exposed
- fn new_with_kind(
+ const fn new_with_kind(
kind: GridTrackKind,
min_track_sizing_function: MinTrackSizingFunction,
max_track_sizing_function: MaxTrackSizingFunction,
@@ -83,7 +83,7 @@ impl GridTrack {
}
/// Create new GridTrack representing an actual track (not a gutter)
- pub fn new(
+ pub const fn new(
min_track_sizing_function: MinTrackSizingFunction,
max_track_sizing_function: MaxTrackSizingFunction,
) -> GridTrack {
diff --git a/src/compute/grid/types/grid_track_counts.rs b/src/compute/grid/types/grid_track_counts.rs
index 435724b4b..079533de0 100644
--- a/src/compute/grid/types/grid_track_counts.rs
+++ b/src/compute/grid/types/grid_track_counts.rs
@@ -49,22 +49,22 @@ pub(crate) struct TrackCounts {
impl TrackCounts {
/// Create a TrackCounts instance from raw track count numbers
- pub fn from_raw(negative_implicit: u16, explicit: u16, positive_implicit: u16) -> Self {
+ pub const fn from_raw(negative_implicit: u16, explicit: u16, positive_implicit: u16) -> Self {
Self { negative_implicit, explicit, positive_implicit }
}
/// Count the total number of tracks in the axis
- pub fn len(&self) -> usize {
+ pub const fn len(&self) -> usize {
(self.negative_implicit + self.explicit + self.positive_implicit) as usize
}
/// The OriginZeroLine representing the start of the implicit grid
- pub fn implicit_start_line(&self) -> OriginZeroLine {
+ pub const fn implicit_start_line(&self) -> OriginZeroLine {
OriginZeroLine(-(self.negative_implicit as i16))
}
/// The OriginZeroLine representing the end of the implicit grid
- pub fn implicit_end_line(&self) -> OriginZeroLine {
+ pub const fn implicit_end_line(&self) -> OriginZeroLine {
OriginZeroLine((self.explicit + self.positive_implicit) as i16)
}
}
@@ -74,13 +74,13 @@ impl TrackCounts {
impl TrackCounts {
/// Converts a grid line in OriginZero coordinates into the track immediately
/// following that grid line as an index into the CellOccupancyMatrix.
- pub fn oz_line_to_next_track(&self, index: OriginZeroLine) -> i16 {
+ pub const fn oz_line_to_next_track(&self, index: OriginZeroLine) -> i16 {
index.0 + (self.negative_implicit as i16)
}
/// Converts start and end grid lines in OriginZero coordinates into a range of tracks
/// as indexes into the CellOccupancyMatrix
- pub fn oz_line_range_to_track_range(&self, input: Line<OriginZeroLine>) -> Range<i16> {
+ pub const fn oz_line_range_to_track_range(&self, input: Line<OriginZeroLine>) -> Range<i16> {
let start = self.oz_line_to_next_track(input.start);
let end = self.oz_line_to_next_track(input.end); // Don't subtract 1 as output range is exclusive
start..end
@@ -88,13 +88,13 @@ impl TrackCounts {
/// Converts a track as an index into the CellOccupancyMatrix into the grid line immediately
/// preceding that track in OriginZero coordinates.
- pub fn track_to_prev_oz_line(&self, index: u16) -> OriginZeroLine {
+ pub const fn track_to_prev_oz_line(&self, index: u16) -> OriginZeroLine {
OriginZeroLine((index as i16) - (self.negative_implicit as i16))
}
/// Converts a range of tracks as indexes into the CellOccupancyMatrix into
/// start and end grid lines in OriginZero coordinates
- pub fn track_range_to_oz_line_range(&self, input: Range<i16>) -> Line<OriginZeroLine> {
+ pub const fn track_range_to_oz_line_range(&self, input: Range<i16>) -> Line<OriginZeroLine> {
let start = self.track_to_prev_oz_line(input.start as u16);
let end = self.track_to_prev_oz_line(input.end as u16); // Don't add 1 as input range is exclusive
Line { start, end }
diff --git a/src/geometry.rs b/src/geometry.rs
index 8cabefcf2..fc4f7a899 100644
--- a/src/geometry.rs
+++ b/src/geometry.rs
@@ -63,7 +63,7 @@ pub enum AbstractAxis {
impl AbstractAxis {
/// Returns the other variant of the enum
#[inline]
- pub fn other(&self) -> AbstractAxis {
+ pub const fn other(&self) -> AbstractAxis {
match *self {
AbstractAxis::Inline => AbstractAxis::Block,
AbstractAxis::Block => AbstractAxis::Inline,
@@ -73,7 +73,7 @@ impl AbstractAxis {
/// Convert an `AbstractAxis` into an `AbsoluteAxis` naively assuming that the Inline axis is Horizontal
/// This is currently always true, but will change if Taffy ever implements the `writing_mode` property
#[inline]
- pub fn as_abs_naive(&self) -> AbsoluteAxis {
+ pub const fn as_abs_naive(&self) -> AbsoluteAxis {
match self {
AbstractAxis::Inline => AbsoluteAxis::Horizontal,
AbstractAxis::Block => AbsoluteAxis::Vertical,
@@ -94,7 +94,7 @@ pub(crate) struct InBothAbsAxis<T> {
impl<T: Copy> InBothAbsAxis<T> {
#[cfg(feature = "grid")]
/// Get the contained item based on the AbsoluteAxis passed
- pub fn get(&self, axis: AbsoluteAxis) -> T {
+ pub const fn get(&self, axis: AbsoluteAxis) -> T {
match axis {
AbsoluteAxis::Horizontal => self.horizontal,
AbsoluteAxis::Vertical => self.vertical,
@@ -248,7 +248,7 @@ where
{
/// The `start` or `top` value of the [`Rect`], from the perspective of the main layout axis
#[cfg(feature = "flexbox")]
- pub(crate) fn main_start(&self, direction: FlexDirection) -> T {
+ pub(crate) const fn main_start(&self, direction: FlexDirection) -> T {
if direction.is_row() {
self.left
} else {
@@ -258,7 +258,7 @@ where
/// The `end` or `bottom` value of the [`Rect`], from the perspective of the main layout axis
#[cfg(feature = "flexbox")]
- pub(crate) fn main_end(&self, direction: FlexDirection) -> T {
+ pub(crate) const fn main_end(&self, direction: FlexDirection) -> T {
if direction.is_row() {
self.right
} else {
@@ -268,7 +268,7 @@ where
/// The `start` or `top` value of the [`Rect`], from the perspective of the cross layout axis
#[cfg(feature = "flexbox")]
- pub(crate) fn cross_start(&self, direction: FlexDirection) -> T {
+ pub(crate) const fn cross_start(&self, direction: FlexDirection) -> T {
if direction.is_row() {
self.top
} else {
@@ -278,7 +278,7 @@ where
/// The `end` or `bottom` value of the [`Rect`], from the perspective of the main layout axis
#[cfg(feature = "flexbox")]
- pub(crate) fn cross_end(&self, direction: FlexDirection) -> T {
+ pub(crate) const fn cross_end(&self, direction: FlexDirection) -> T {
if direction.is_row() {
self.bottom
} else {
@@ -562,7 +562,7 @@ impl Size<Option<f32>> {
/// Creates a new [`Size<Option<f32>>`] with either the width or height set based on the provided `direction`
#[cfg(feature = "flexbox")]
- pub fn from_cross(direction: FlexDirection, value: Option<f32>) -> Self {
+ pub const fn from_cross(direction: FlexDirection, value: Option<f32>) -> Self {
let mut new = Self::NONE;
if direction.is_row() {
new.height = value
diff --git a/src/style/available_space.rs b/src/style/available_space.rs
index 4cfcecafb..844037e0a 100644
--- a/src/style/available_space.rs
+++ b/src/style/available_space.rs
@@ -34,13 +34,13 @@ impl FromLength for AvailableSpace {
impl AvailableSpace {
/// Returns true for definite values, else false
- pub fn is_definite(self) -> bool {
+ pub const fn is_definite(self) -> bool {
matches!(self, AvailableSpace::Definite(_))
}
/// Convert to Option
/// Definite values become Some(value). Constraints become None.
- pub fn into_option(self) -> Option<f32> {
+ pub const fn into_option(self) -> Option<f32> {
match self {
AvailableSpace::Definite(value) => Some(value),
_ => None,
diff --git a/src/style/dimension.rs b/src/style/dimension.rs
index ad270e48c..b3b2f8797 100644
--- a/src/style/dimension.rs
+++ b/src/style/dimension.rs
@@ -52,12 +52,12 @@ impl LengthPercentage {
/// # Safety
/// CompactLength must represent a valid variant for LengthPercentage
#[allow(unsafe_code)]
- pub unsafe fn from_raw(val: CompactLength) -> Self {
+ pub const unsafe fn from_raw(val: CompactLength) -> Self {
Self(val)
}
/// Get the underlying `CompactLength` representation of the value
- pub fn into_raw(self) -> CompactLength {
+ pub const fn into_raw(self) -> CompactLength {
self.0
}
}
@@ -143,12 +143,12 @@ impl LengthPercentageAuto {
/// # Safety
/// CompactLength must represent a valid variant for LengthPercentageAuto
#[allow(unsafe_code)]
- pub unsafe fn from_raw(val: CompactLength) -> Self {
+ pub const unsafe fn from_raw(val: CompactLength) -> Self {
Self(val)
}
/// Get the underlying `CompactLength` representation of the value
- pub fn into_raw(self) -> CompactLength {
+ pub const fn into_raw(self) -> CompactLength {
self.0
}
@@ -261,12 +261,12 @@ impl Dimension {
/// # Safety
/// CompactLength must represent a valid variant for LengthPercentageAuto
#[allow(unsafe_code)]
- pub unsafe fn from_raw(val: CompactLength) -> Self {
+ pub const unsafe fn from_raw(val: CompactLength) -> Self {
Self(val)
}
/// Get the underlying `CompactLength` representation of the value
- pub fn into_raw(self) -> CompactLength {
+ pub const fn into_raw(self) -> CompactLength {
self.0
}
diff --git a/src/style/flex.rs b/src/style/flex.rs
index adf73516a..92b6e04b8 100644
--- a/src/style/flex.rs
+++ b/src/style/flex.rs
@@ -121,25 +121,25 @@ pub enum FlexDirection {
impl FlexDirection {
#[inline]
/// Is the direction [`FlexDirection::Row`] or [`FlexDirection::RowReverse`]?
- pub(crate) fn is_row(self) -> bool {
+ pub(crate) const 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 {
+ pub(crate) const 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 {
+ pub(crate) const fn is_reverse(self) -> bool {
matches!(self, Self::RowReverse | Self::ColumnReverse)
}
#[inline]
/// The `AbsoluteAxis` that corresponds to the main axis
- pub(crate) fn main_axis(self) -> AbsoluteAxis {
+ pub(crate) const fn main_axis(self) -> AbsoluteAxis {
match self {
Self::Row | Self::RowReverse => AbsoluteAxis::Horizontal,
Self::Column | Self::ColumnReverse => AbsoluteAxis::Vertical,
@@ -148,7 +148,7 @@ impl FlexDirection {
#[inline]
/// The `AbsoluteAxis` that corresponds to the cross axis
- pub(crate) fn cross_axis(self) -> AbsoluteAxis {
+ pub(crate) const fn cross_axis(self) -> AbsoluteAxis {
match self {
Self::Row | Self::RowReverse => AbsoluteAxis::Vertical,
Self::Column | Self::ColumnReverse => AbsoluteAxis::Horizontal,
diff --git a/src/style/grid.rs b/src/style/grid.rs
index c3d558335..d7c30939d 100644
--- a/src/style/grid.rs
+++ b/src/style/grid.rs
@@ -290,7 +290,7 @@ pub enum GridAutoFlow {
impl GridAutoFlow {
/// Whether grid auto placement uses the sparse placement algorithm or the dense placement algorithm
/// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow#values>
- pub fn is_dense(&self) -> bool {
+ pub const fn is_dense(&self) -> bool {
match self {
Self::Row | Self::Column => false,
Self::RowDense | Self::ColumnDense => true,
@@ -299,7 +299,7 @@ impl GridAutoFlow {
/// Whether grid auto placement fills areas row-wise or column-wise
/// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow#values>
- pub fn primary_axis(&self) -> AbsoluteAxis {
+ pub const fn primary_axis(&self) -> AbsoluteAxis {
match self {
Self::Row | Self::RowDense => AbsoluteAxis::Horizontal,
Self::Column | Self::ColumnDense => AbsoluteAxis::Vertical,
@@ -431,7 +431,7 @@ impl NonNamedGridPlacement {
impl<T: GridCoordinate> Line<GenericGridPlacement<T>> {
/// Resolves the span for an indefinite placement (a placement that does not consist of two `Track`s).
/// Panics if called on a definite placement
- pub fn indefinite_span(&self) -> u16 {
+ pub const fn indefinite_span(&self) -> u16 {
use GenericGridPlacement as GP;
match (self.start, self.end) {
(GP::Line(_), GP::Auto) => 1,
@@ -489,7 +489,7 @@ impl Line<OriginZeroGridPlacement> {
#[inline]
/// Whether the track position is definite in this axis (or the item will need auto placement)
/// The track position is definite if least one of the start and end positions is a track index
- pub fn is_definite(&self) -> bool {
+ pub const fn is_definite(&self) -> bool {
matches!((self.start, self.end), (GenericGridPlacement::Line(_), _) | (_, GenericGridPlacement::Line(_)))
}