tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1077HTML 1008Markdown 12TOML 83209 Definitions

⎇ 397e3158fc99c64fab0f651260f8663207645943 src/sys.rs Rust · 166 lines · 5.2 KB History Who touched what raw

Implement core/alloc compatible versions of f32_max and f32_min 397e3158f · Nico Burns ·

1//! Allocator-flexible data types
2
3// When std is enabled, prefer those types
4#[cfg(feature = "std")]
5pub(crate) use self::std::*;
6
7// When alloc but not std is enabled, use those types
8#[cfg(all(feature = "alloc", not(feature = "std")))]
9pub(crate) use self::alloc::*;
10
11// When neither alloc or std is enabled, use a heapless fallback
12#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
13pub(crate) use self::core::*;
14
15/// For when `std` is enabled
16#[cfg(feature = "std")]
17mod std {
18    /// An allocation-backend agnostic [`Box`] type
19    pub(crate) type Box<A> = std::boxed::Box<A>;
20    /// An allocation-backend agnostic vector type
21    pub(crate) type Vec<A> = std::vec::Vec<A>;
22    /// A vector of child nodes
23    pub(crate) type ChildrenVec<A> = std::vec::Vec<A>;
24    /// A vector of grid tracks
25    #[cfg(feature = "grid")]
26    pub(crate) type GridTrackVec<A> = std::vec::Vec<A>;
27
28    /// Creates a new vector with the capacity for the specified number of items before it must be resized
29    #[must_use]
30    pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
31        Vec::with_capacity(capacity)
32    }
33
34    /// Rounds to the nearest whole number
35    #[must_use]
36    pub(crate) fn round(value: f32) -> f32 {
37        value.round()
38    }
39
40    /// Computes the absolute value
41    #[must_use]
42    pub(crate) fn abs(value: f32) -> f32 {
43        value.abs()
44    }
45
46    /// Returns the largest of two f32 values
47    pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
48        std::cmp::max_by(a, b, |a, b| a.total_cmp(&b))
49    }
50
51    /// Returns the smallest of two f32 values
52    #[cfg(feature = "grid")]
53    pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
54        std::cmp::min_by(a, b, |a, b| a.total_cmp(&b))
55    }
56}
57
58/// For when `alloc` but not `std` is enabled
59#[cfg(all(feature = "alloc", not(feature = "std")))]
60mod alloc {
61    extern crate alloc;
62    use core::cmp::Ordering;
63
64    /// An allocation-backend agnostic `Box` type
65    pub(crate) type Box<A> = alloc::boxed::Box<A>;
66    /// An allocation-backend agnostic vector type
67    pub(crate) type Vec<A> = alloc::vec::Vec<A>;
68    /// A vector of child nodes
69    pub(crate) type ChildrenVec<A> = alloc::vec::Vec<A>;
70    /// A vector of grid tracks
71    pub(crate) type GridTrackVec<A> = alloc::vec::Vec<A>;
72
73    /// Creates a new vector with the capacity for the specified number of items before it must be resized
74    #[must_use]
75    pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
76        Vec::with_capacity(capacity)
77    }
78
79    /// Rounds to the nearest whole number
80    #[must_use]
81    pub(crate) fn round(value: f32) -> f32 {
82        num_traits::float::FloatCore::round(value)
83    }
84
85    /// Computes the absolute value
86    #[must_use]
87    pub(crate) fn abs(value: f32) -> f32 {
88        num_traits::float::FloatCore::abs(value)
89    }
90
91    /// Returns the largest of two f32 values
92    pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
93        match a.total_cmp(&b) {
94            Ordering::Greater | Ordering::Equal => a,
95            Ordering::Less => b,
96        }
97    }
98
99    /// Returns the smallest of two f32 values
100    #[cfg(feature = "grid")]
101    pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
102        match a.total_cmp(&b) {
103            Ordering::Less => a,
104            Ordering::Greater | Ordering::Equal => b,
105        }
106    }
107}
108
109/// For when neither `alloc` nor `std` is enabled
110#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
111mod core {
112    use core::cmp::Ordering;
113
114    /// The maximum number of nodes in the tree
115    pub const MAX_NODE_COUNT: usize = 256;
116    /// The maximum number of children of any given node
117    pub const MAX_CHILD_COUNT: usize = 16;
118    /// The maximum number of children of any given node
119    pub const MAX_GRID_TRACKS: usize = 16;
120
121    /// An allocation-backend agnostic vector type
122    pub(crate) type Vec<A> = arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
123    /// A vector of child nodes, whose length cannot exceed [`MAX_CHILD_COUNT`]
124    pub(crate) type ChildrenVec<A> = arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
125    /// A vector of grid tracks
126    pub(crate) type GridTrackVec<A> = arrayvec::ArrayVec<A, MAX_GRID_TRACKS>;
127
128    /// Creates a new map with the capacity for the specified number of items before it must be resized
129    ///
130    /// This vector cannot be resized.
131    #[must_use]
132    pub(crate) fn new_vec_with_capacity<A, const CAP: usize>(_capacity: usize) -> arrayvec::ArrayVec<A, CAP> {
133        arrayvec::ArrayVec::new()
134    }
135
136    /// Rounds to the nearest whole number
137    #[inline]
138    #[must_use]
139    pub(crate) fn round(value: f32) -> f32 {
140        num_traits::float::FloatCore::round(value)
141    }
142
143    /// Computes the absolute value
144    #[inline]
145    #[must_use]
146    pub(crate) fn abs(value: f32) -> f32 {
147        num_traits::float::FloatCore::abs(value)
148    }
149
150    /// Returns the largest of two f32 values
151    pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
152        match a.total_cmp(&b) {
153            Ordering::Greater | Ordering::Equal => a,
154            Ordering::Less => b,
155        }
156    }
157
158    /// Returns the smallest of two f32 values
159    #[cfg(feature = "grid")]
160    pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
161        match a.total_cmp(&b) {
162            Ordering::Less => a,
163            Ordering::Greater | Ordering::Equal => b,
164        }
165    }
166}