gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 231Rust 8TOML 3CSS 1317 Definitions

⎇ 76722686b0999e997dbe9c280ff9ea8567e0e95c src/sys.rs Rust · 130 lines · 4.2 KB History Who touched what raw

Make CSS Grid feature a default feature (#297) 01babab32 · 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
16#[cfg(feature = "grid")]
17pub(crate) fn f32_max(a: f32, b: f32) -> f32 {
18    core::cmp::max_by(a, b, |a, b| a.total_cmp(b))
19}
20
21/// Returns the smallest of two f32 values
22#[cfg(feature = "grid")]
23pub(crate) fn f32_min(a: f32, b: f32) -> f32 {
24    core::cmp::min_by(a, b, |a, b| a.total_cmp(b))
25}
26
27/// For when `std` is enabled
28#[cfg(feature = "std")]
29mod std {
30    /// An allocation-backend agnostic [`Box`] type
31    pub(crate) type Box<A> = std::boxed::Box<A>;
32    /// An allocation-backend agnostic vector type
33    pub(crate) type Vec<A> = std::vec::Vec<A>;
34    /// A vector of child nodes
35    pub(crate) type ChildrenVec<A> = std::vec::Vec<A>;
36    /// A vector of grid tracks
37    #[cfg(feature = "grid")]
38    pub(crate) type GridTrackVec<A> = std::vec::Vec<A>;
39
40    /// Creates a new vector with the capacity for the specified number of items before it must be resized
41    #[must_use]
42    pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
43        Vec::with_capacity(capacity)
44    }
45
46    /// Rounds to the nearest whole number
47    #[must_use]
48    pub(crate) fn round(value: f32) -> f32 {
49        value.round()
50    }
51
52    /// Computes the absolute value
53    #[must_use]
54    pub(crate) fn abs(value: f32) -> f32 {
55        value.abs()
56    }
57}
58
59/// For when `alloc` but not `std` is enabled
60#[cfg(all(feature = "alloc", not(feature = "std")))]
61mod alloc {
62    extern crate alloc;
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
92/// For when neither `alloc` nor `std` is enabled
93#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
94mod core {
95    /// The maximum number of nodes in the tree
96    pub const MAX_NODE_COUNT: usize = 256;
97    /// The maximum number of children of any given node
98    pub const MAX_CHILD_COUNT: usize = 16;
99    /// The maximum number of children of any given node
100    pub const MAX_GRID_TRACKS: usize = 16;
101
102    /// An allocation-backend agnostic vector type
103    pub(crate) type Vec<A> = arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
104    /// A vector of child nodes, whose length cannot exceed [`MAX_CHILD_COUNT`]
105    pub(crate) type ChildrenVec<A> = arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
106    /// A vector of grid tracks
107    pub(crate) type GridTrackVec<A> = arrayvec::ArrayVec<A, MAX_GRID_TRACKS>;
108
109    /// Creates a new map with the capacity for the specified number of items before it must be resized
110    ///
111    /// This vector cannot be resized.
112    #[must_use]
113    pub(crate) fn new_vec_with_capacity<A, const CAP: usize>(_capacity: usize) -> arrayvec::ArrayVec<A, CAP> {
114        arrayvec::ArrayVec::new()
115    }
116
117    /// Rounds to the nearest whole number
118    #[inline]
119    #[must_use]
120    pub(crate) fn round(value: f32) -> f32 {
121        num_traits::float::FloatCore::round(value)
122    }
123
124    /// Computes the absolute value
125    #[inline]
126    #[must_use]
127    pub(crate) fn abs(value: f32) -> f32 {
128        num_traits::float::FloatCore::abs(value)
129    }
130}