gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1086HTML 1018Markdown 12TOML 84406 Sutichaykuna

⎇ 6c62bd8db839bde826e6edb3ff07168ec4183be3 src/sys.rs Rust · 129 siq'ikuna · 4.1 KB Ñawpa willay Pi imata llamkarqan ch'uya

Fix building with grid feature disabled 6c62bd8db · 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/// Returns the largest of two f32 values
16pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
17    core::cmp::max_by(a, b, |a, b| a.total_cmp(b))
18}
19
20/// Returns the smallest of two f32 values
21#[cfg(feature = "grid")]
22pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
23    core::cmp::min_by(a, b, |a, b| a.total_cmp(b))
24}
25
26/// For when `std` is enabled
27#[cfg(feature = "std")]
28mod std {
29    /// An allocation-backend agnostic [`Box`] type
30    pub(crate) type Box<A> = std::boxed::Box<A>;
31    /// An allocation-backend agnostic vector type
32    pub(crate) type Vec<A> = std::vec::Vec<A>;
33    /// A vector of child nodes
34    pub(crate) type ChildrenVec<A> = std::vec::Vec<A>;
35    /// A vector of grid tracks
36    #[cfg(feature = "grid")]
37    pub(crate) type GridTrackVec<A> = std::vec::Vec<A>;
38
39    /// Creates a new vector with the capacity for the specified number of items before it must be resized
40    #[must_use]
41    pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
42        Vec::with_capacity(capacity)
43    }
44
45    /// Rounds to the nearest whole number
46    #[must_use]
47    pub(crate) fn round(value: f32) -> f32 {
48        value.round()
49    }
50
51    /// Computes the absolute value
52    #[must_use]
53    pub(crate) fn abs(value: f32) -> f32 {
54        value.abs()
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
63    /// An allocation-backend agnostic `Box` type
64    pub(crate) type Box<A> = alloc::boxed::Box<A>;
65    /// An allocation-backend agnostic vector type
66    pub(crate) type Vec<A> = alloc::vec::Vec<A>;
67    /// A vector of child nodes
68    pub(crate) type ChildrenVec<A> = alloc::vec::Vec<A>;
69    /// A vector of grid tracks
70    pub(crate) type GridTrackVec<A> = alloc::vec::Vec<A>;
71
72    /// Creates a new vector with the capacity for the specified number of items before it must be resized
73    #[must_use]
74    pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
75        Vec::with_capacity(capacity)
76    }
77
78    /// Rounds to the nearest whole number
79    #[must_use]
80    pub(crate) fn round(value: f32) -> f32 {
81        num_traits::float::FloatCore::round(value)
82    }
83
84    /// Computes the absolute value
85    #[must_use]
86    pub(crate) fn abs(value: f32) -> f32 {
87        num_traits::float::FloatCore::abs(value)
88    }
89}
90
91/// For when neither `alloc` nor `std` is enabled
92#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
93mod core {
94    /// The maximum number of nodes in the tree
95    pub const MAX_NODE_COUNT: usize = 256;
96    /// The maximum number of children of any given node
97    pub const MAX_CHILD_COUNT: usize = 16;
98    /// The maximum number of children of any given node
99    pub const MAX_GRID_TRACKS: usize = 16;
100
101    /// An allocation-backend agnostic vector type
102    pub(crate) type Vec<A> = arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
103    /// A vector of child nodes, whose length cannot exceed [`MAX_CHILD_COUNT`]
104    pub(crate) type ChildrenVec<A> = arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
105    /// A vector of grid tracks
106    pub(crate) type GridTrackVec<A> = arrayvec::ArrayVec<A, MAX_GRID_TRACKS>;
107
108    /// Creates a new map with the capacity for the specified number of items before it must be resized
109    ///
110    /// This vector cannot be resized.
111    #[must_use]
112    pub(crate) fn new_vec_with_capacity<A, const CAP: usize>(_capacity: usize) -> arrayvec::ArrayVec<A, CAP> {
113        arrayvec::ArrayVec::new()
114    }
115
116    /// Rounds to the nearest whole number
117    #[inline]
118    #[must_use]
119    pub(crate) fn round(value: f32) -> f32 {
120        num_traits::float::FloatCore::round(value)
121    }
122
123    /// Computes the absolute value
124    #[inline]
125    #[must_use]
126    pub(crate) fn abs(value: f32) -> f32 {
127        num_traits::float::FloatCore::abs(value)
128    }
129}