tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Sutichaykuna

⎇ 7d9382ab959f495fb127584399448d7d7e7456de src/sys.rs Rust · 103 siq'ikuna · 2.8 KB Ñawpa willay Pi imata llamkarqan ch'uya

Make alloc and std compatible features (#86) 7d9382ab9 · Alice Cecile ·

1// When std is enabled, prefer those types
2#[cfg(feature = "std")]
3pub use self::std::*;
4
5// When alloc but not std is enabled, use those types
6#[cfg(all(feature = "alloc", not(feature = "std")))]
7pub use self::alloc::*;
8
9// When neither alloc or std is enabled, use a heapless fallback
10#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
11pub use self::core::*;
12
13#[cfg(feature = "std")]
14mod std {
15    pub type Box<A> = std::boxed::Box<A>;
16    pub type Map<K, V> = std::collections::HashMap<K, V>;
17    pub type Vec<A> = std::vec::Vec<A>;
18    pub type ChildrenVec<A> = std::vec::Vec<A>;
19    pub type ParentsVec<A> = std::vec::Vec<A>;
20
21    pub fn new_map_with_capacity<K, V>(capacity: usize) -> Map<K, V>
22    where
23        K: Eq + std::hash::Hash,
24    {
25        Map::with_capacity(capacity)
26    }
27
28    pub fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
29        Vec::with_capacity(capacity)
30    }
31
32    #[inline]
33    pub fn round(value: f32) -> f32 {
34        value.round()
35    }
36
37    #[inline]
38    pub fn abs(value: f32) -> f32 {
39        value.abs()
40    }
41}
42
43#[cfg(all(feature = "alloc", not(feature = "std")))]
44mod alloc {
45    extern crate alloc;
46
47    pub type Box<A> = alloc::boxed::Box<A>;
48    pub type Map<K, V> = hashbrown::HashMap<K, V>;
49    pub type Vec<A> = alloc::vec::Vec<A>;
50    pub type ChildrenVec<A> = alloc::vec::Vec<A>;
51    pub type ParentsVec<A> = alloc::vec::Vec<A>;
52
53    pub fn new_map_with_capacity<K, V>(capacity: usize) -> Map<K, V> {
54        Map::with_capacity(capacity)
55    }
56
57    pub fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
58        Vec::with_capacity(capacity)
59    }
60
61    #[inline]
62    pub fn round(value: f32) -> f32 {
63        num_traits::float::FloatCore::round(value)
64    }
65
66    #[inline]
67    pub fn abs(value: f32) -> f32 {
68        num_traits::float::FloatCore::abs(value)
69    }
70}
71
72#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
73mod core {
74    pub const MAX_NODE_COUNT: usize = 256;
75    pub const MAX_CHILD_COUNT: usize = 16;
76    pub const MAX_PARENTS_COUNT: usize = 1;
77
78    pub type Map<K, V> = heapless::FnvIndexMap<K, V, MAX_NODE_COUNT>;
79    pub type Vec<A> = arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
80    pub type ChildrenVec<A> = arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
81    pub type ParentsVec<A> = arrayvec::ArrayVec<A, MAX_PARENTS_COUNT>;
82
83    pub fn new_map_with_capacity<K, V>(_capacity: usize) -> Map<K, V>
84    where
85        K: Eq + ::hash32::Hash,
86    {
87        Map::new()
88    }
89
90    pub fn new_vec_with_capacity<A, const CAP: usize>(_capacity: usize) -> arrayvec::ArrayVec<A, CAP> {
91        arrayvec::ArrayVec::new()
92    }
93
94    #[inline]
95    pub fn round(value: f32) -> f32 {
96        num_traits::float::FloatCore::round(value)
97    }
98
99    #[inline]
100    pub fn abs(value: f32) -> f32 {
101        num_traits::float::FloatCore::abs(value)
102    }
103}