Skip to content

Commit

Permalink
Fully remove num-traits
Browse files Browse the repository at this point in the history
Signed-off-by: Nico Burns <[email protected]>
  • Loading branch information
nicoburns committed Dec 15, 2024
1 parent 016b93a commit 75f9e7f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ license = "MIT"
[dependencies]
arrayvec = { version = "0.7", default-features = false }
document-features = { version = "0.2.7", optional = true }
num-traits = { version = "0.2", default-features = false }
serde = { version = "1.0", default-features = false, optional = true, features = [
"serde_derive",
] }
Expand Down Expand Up @@ -66,7 +65,7 @@ taffy_tree = ["dep:slotmap"]
## Add [`serde`] derives to Style structs
serde = ["dep:serde"]
## Allow Taffy to depend on the [`Rust Standard Library`](std)
std = ["num-traits/std", "grid?/std", "serde?/std", "slotmap?/std"]
std = ["grid?/std", "serde?/std", "slotmap?/std"]
## Allow Taffy to depend on the alloc library
alloc = ["serde?/alloc"]
## Internal feature for debugging
Expand Down
9 changes: 3 additions & 6 deletions src/compute/grid/explicit_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ use super::types::{GridTrack, TrackCounts};
use crate::geometry::{AbsoluteAxis, Size};
use crate::style::{GridTrackRepetition, LengthPercentage, NonRepeatedTrackSizingFunction, TrackSizingFunction};
use crate::style_helpers::TaffyAuto;
use crate::util::sys::Vec;
use crate::util::sys::{ceil, floor, Vec};
use crate::util::MaybeMath;
use crate::util::ResolveOrZero;
use crate::{GridContainerStyle, MaybeResolve};

#[cfg(not(feature = "std"))]
use num_traits::float::FloatCore;

/// Compute the number of rows and columns in the explicit grid
pub(crate) fn compute_explicit_grid_size_in_axis(
style: &impl GridContainerStyle,
Expand Down Expand Up @@ -161,9 +158,9 @@ pub(crate) fn compute_explicit_grid_size_in_axis(
//
// In all cases we add the additional repetition that was already accounted for in the special-case computation above
if size_is_maximum {
(num_repetition_that_fit.floor() as u16) + 1
(floor(num_repetition_that_fit) as u16) + 1
} else {
(num_repetition_that_fit.ceil() as u16) + 1
(ceil(num_repetition_that_fit) as u16) + 1
}
}
}
Expand Down
51 changes: 48 additions & 3 deletions src/util/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ mod std {
value.round()
}

/// Rounds up to the nearest whole number
#[must_use]
#[inline(always)]
pub(crate) fn ceil(value: f32) -> f32 {
value.ceil()
}

/// Rounds down to the nearest whole number
#[must_use]
#[inline(always)]
pub(crate) fn floor(value: f32) -> f32 {
value.floor()
}

/// Computes the absolute value
#[must_use]
#[inline(always)]
Expand Down Expand Up @@ -79,6 +93,12 @@ mod alloc {
/// Rounds to the nearest whole number
pub(crate) use super::polyfill::round;

/// Rounds up to the nearest whole number
pub(crate) use super::polyfill::ceil;

/// Rounds down to the nearest whole number
pub(crate) use super::polyfill::floor;

/// Computes the absolute value
pub(crate) use super::polyfill::abs;

Expand Down Expand Up @@ -145,9 +165,10 @@ mod core {

/// Implementations of float functions for no_std and alloc builds
/// Copied from `num-traits` crate
#[cfg(not(feature = "std"))]
mod polyfill {
#[must_use]
#[inline(always)]
#[cfg(not(feature = "std"))]
fn fract(value: f32) -> f32 {
if value == 0.0 {
0.0
Expand All @@ -158,7 +179,6 @@ mod polyfill {

#[must_use]
#[inline(always)]
#[cfg(not(feature = "std"))]
pub(crate) fn round(value: f32) -> f32 {
let f = fract(value);
if f.is_nan() || f == 0.0 {
Expand All @@ -176,10 +196,35 @@ mod polyfill {
}
}

#[must_use]
#[inline(always)]
pub(crate) fn floor(value: f32) -> f32 {
let f = fract(value);
if f.is_nan() || f == 0.0 {
value
} else if value < 0.0 {
value - f - 1.0
} else {
value - f
}
}

#[must_use]
#[inline(always)]
pub(crate) fn ceil(value: f32) -> f32 {
let f = fract(value);
if f.is_nan() || f == 0.0 {
value
} else if value > 0.0 {
value - f + 1.0
} else {
value - f
}
}

/// Computes the absolute value
#[must_use]
#[inline(always)]
#[cfg(not(feature = "std"))]
pub(crate) fn abs(value: f32) -> f32 {
if value.is_sign_positive() {
return value;
Expand Down

0 comments on commit 75f9e7f

Please sign in to comment.