tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 583HTML 275Markdown 8TOML 41586 Definitions

⎇ 90ca73fcec69bf4a1b2fdcb21125cf19df393d8a src/sys.rs Rust · 149 lines · 5.0 KB History Who touched what raw

Stop leaking internal types in public interface (#137) b1f23f1d5 · Alice Cecile ·

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 map type
21    pub(crate) type Map<K, V> = std::collections::HashMap<K, V>;
22    /// An allocation-backend agnostic vector type
23    pub(crate) type Vec<A> = std::vec::Vec<A>;
24    /// A vector of child nodes
25    pub(crate) type ChildrenVec<A> = std::vec::Vec<A>;
26    /// A vector of parent nodes
27    pub(crate) type ParentsVec<A> = std::vec::Vec<A>;
28
29    /// Creates a new map with the capacity for the specified number of items before it must be resized
30    #[must_use]
31    pub(crate) fn new_map_with_capacity<K, V>(capacity: usize) -> Map<K, V>
32    where
33        K: Eq + std::hash::Hash,
34    {
35        Map::with_capacity(capacity)
36    }
37
38    /// Creates a new vector with the capacity for the specified number of items before it must be resized    
39    #[must_use]
40    pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
41        Vec::with_capacity(capacity)
42    }
43
44    /// Rounds to the nearest whole number
45    #[must_use]
46    pub(crate) fn round(value: f32) -> f32 {
47        value.round()
48    }
49
50    /// Computes the absolute value
51    #[must_use]
52    pub(crate) fn abs(value: f32) -> f32 {
53        value.abs()
54    }
55}
56
57/// For when `alloc` but not `std` is enabled
58#[cfg(all(feature = "alloc", not(feature = "std")))]
59mod alloc {
60    extern crate alloc;
61
62    /// An allocation-backend agnostic `Box` type
63    pub(crate) type Box<A> = alloc::boxed::Box<A>;
64    /// An allocation-backend agnostic map type
65    pub(crate) type Map<K, V> = hashbrown::HashMap<K, V>;
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 parent nodes
71    pub(crate) type ParentsVec<A> = alloc::vec::Vec<A>;
72
73    /// Creates a new map with the capacity for the specified number of items before it must be resized
74    #[must_use]
75    pub(crate) fn new_map_with_capacity<K, V>(capacity: usize) -> Map<K, V> {
76        Map::with_capacity(capacity)
77    }
78
79    /// Creates a new vector with the capacity for the specified number of items before it must be resized
80    #[must_use]
81    pub(crate) fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
82        Vec::with_capacity(capacity)
83    }
84
85    /// Rounds to the nearest whole number
86    #[must_use]
87    pub(crate) fn round(value: f32) -> f32 {
88        num_traits::float::FloatCore::round(value)
89    }
90
91    /// Computes the absolute value
92    #[must_use]
93    pub(crate) fn abs(value: f32) -> f32 {
94        num_traits::float::FloatCore::abs(value)
95    }
96}
97
98/// For when neither `alloc` nor `std` is enabled
99#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
100mod core {
101    /// The maximum number of nodes in the forest
102    pub const MAX_NODE_COUNT: usize = 256;
103    /// The maximum number of children of any given node
104    pub const MAX_CHILD_COUNT: usize = 16;
105    /// The maximum number of parents for each child
106    pub const MAX_PARENTS_COUNT: usize = 1;
107
108    /// An allocation-backend agnostic map type
109    pub(crate) type Map<K, V> = crate::indexmap::FnvIndexMap<K, V, MAX_NODE_COUNT>;
110    /// An allocation-backend agnostic vector type
111    pub(crate) type Vec<A> = arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
112    /// A vector of child nodes, whose length cannot exceed [`MAX_CHILD_COUNT`]
113    pub(crate) type ChildrenVec<A> = arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
114    /// A vector of parent nodes, whose length cannot exceed [`MAX_PARENTS_COUNT`]
115    pub(crate) type ParentsVec<A> = arrayvec::ArrayVec<A, MAX_PARENTS_COUNT>;
116
117    /// Creates a new map with the capacity for the specified number of items
118    ///
119    /// This map cannot be resized.
120    #[must_use]
121    pub(crate) fn new_map_with_capacity<K, V>(_capacity: usize) -> Map<K, V>
122    where
123        K: Eq + ::hash32::Hash,
124    {
125        Map::new()
126    }
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}