gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1061HTML 997Markdown 12TOML 83120 Sutichaykuna

⎇ e56838be328f53ae5ef3d53ad297843ee8e992ba src/sys.rs Rust · 169 siq'ikuna · 5.3 KB Ñawpa willay Pi imata llamkarqan ch'uya

Fix unused improperly feature flagged items 92cd40289 · 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    #[cfg(feature = "grid")]
25    /// A vector of grid tracks
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    #[cfg(feature = "grid")]
71    /// A vector of grid tracks
72    pub(crate) type GridTrackVec<A> = alloc::vec::Vec<A>;
73
74    /// Creates a new vector with the capacity for the specified number of items before it must be resized
75    #[must_use]
76    pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
77        Vec::with_capacity(capacity)
78    }
79
80    /// Rounds to the nearest whole number
81    #[must_use]
82    pub(crate) fn round(value: f32) -> f32 {
83        num_traits::float::FloatCore::round(value)
84    }
85
86    /// Computes the absolute value
87    #[must_use]
88    pub(crate) fn abs(value: f32) -> f32 {
89        num_traits::float::FloatCore::abs(value)
90    }
91
92    /// Returns the largest of two f32 values
93    pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
94        match a.total_cmp(&b) {
95            Ordering::Greater | Ordering::Equal => a,
96            Ordering::Less => b,
97        }
98    }
99
100    /// Returns the smallest of two f32 values
101    #[cfg(feature = "grid")]
102    pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
103        match a.total_cmp(&b) {
104            Ordering::Less => a,
105            Ordering::Greater | Ordering::Equal => b,
106        }
107    }
108}
109
110/// For when neither `alloc` nor `std` is enabled
111#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
112mod core {
113    use core::cmp::Ordering;
114
115    /// The maximum number of nodes in the tree
116    pub const MAX_NODE_COUNT: usize = 256;
117    /// The maximum number of children of any given node
118    pub const MAX_CHILD_COUNT: usize = 16;
119    #[cfg(feature = "grid")]
120    /// The maximum number of children of any given node
121    pub const MAX_GRID_TRACKS: usize = 16;
122
123    /// An allocation-backend agnostic vector type
124    pub(crate) type Vec<A> = arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
125    /// A vector of child nodes, whose length cannot exceed [`MAX_CHILD_COUNT`]
126    pub(crate) type ChildrenVec<A> = arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
127    #[cfg(feature = "grid")]
128    /// A vector of grid tracks
129    pub(crate) type GridTrackVec<A> = arrayvec::ArrayVec<A, MAX_GRID_TRACKS>;
130
131    /// Creates a new map with the capacity for the specified number of items before it must be resized
132    ///
133    /// This vector cannot be resized.
134    #[must_use]
135    pub(crate) fn new_vec_with_capacity<A, const CAP: usize>(_capacity: usize) -> arrayvec::ArrayVec<A, CAP> {
136        arrayvec::ArrayVec::new()
137    }
138
139    /// Rounds to the nearest whole number
140    #[inline]
141    #[must_use]
142    pub(crate) fn round(value: f32) -> f32 {
143        num_traits::float::FloatCore::round(value)
144    }
145
146    /// Computes the absolute value
147    #[inline]
148    #[must_use]
149    pub(crate) fn abs(value: f32) -> f32 {
150        num_traits::float::FloatCore::abs(value)
151    }
152
153    /// Returns the largest of two f32 values
154    pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
155        match a.total_cmp(&b) {
156            Ordering::Greater | Ordering::Equal => a,
157            Ordering::Less => b,
158        }
159    }
160
161    /// Returns the smallest of two f32 values
162    #[cfg(feature = "grid")]
163    pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
164        match a.total_cmp(&b) {
165            Ordering::Less => a,
166            Ordering::Greater | Ordering::Equal => b,
167        }
168    }
169}