tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Sutichaykuna

Make alloc and std compatible features (#86)

* Clean up feature definition

* Fix alloc imports

* Import style

* Make everything pub again

* Add release notes

* Import style

* Disambiguate namespace

7d9382ab959f495fb127584399448d7d7e7456de · ruwaq Alice Cecile · (4 wata ñawpaq) · tayta 52ef801c9 · kay commitpi sach'ata qhaway

Semántico sayk'uy · 19

src/sys.rs

3 qillqakuna · +43 −38

diff --git a/Cargo.toml b/Cargo.toml
index 646c8f3d2..f3019a9e3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,20 +17,14 @@ hash32-derive = "0.1"
 heapless = "0.7"
 num-traits = { version = "0.2", default-features = false }
 typenum = "1"
-
-[dependencies.hashbrown]
-version = "0.12"
-optional = true
-
-[dependencies.serde]
-version = "1.0"
-features = ["serde_derive"]
-optional = true
+hashbrown = {version = "0.12", optional = true}
+serde = {version = "1.0", optional = true, features = ["serde_derive"]}
 
 [features]
 default = ["std"]
 alloc = ["hashbrown"]
 std = ["num-traits/std"]
+serde = ["dep:serde"]
 
 [dev-dependencies]
 criterion = "0.3"
diff --git a/RELEASES.md b/RELEASES.md
index 161f02818..a093f9a7e 100644
--- a/RELEASES.md
+++ b/RELEASES.md
@@ -6,7 +6,11 @@
 
 - renamed crate from `stretch2` to `sprawl`
 - updated to the latest version of all dependencies to reduce upstream pain caused by duplicate dependencies
-- Renamed `sprawl::node::Strech` -> `sprawl::node::Sprawl`
+- Renamed `stretch::node::Strech` -> `sprawl::node::Sprawl`
+
+### Fixed
+
+- fixed feature strategy for `alloc` and `std`: these can now be compiled together, with `std`'s types taking priority
 
 ### Removed
 
diff --git a/src/sys.rs b/src/sys.rs
index ef8d5b5e5..47921a38e 100644
--- a/src/sys.rs
+++ b/src/sys.rs
@@ -1,14 +1,26 @@
+// When std is enabled, prefer those types
+#[cfg(feature = "std")]
+pub use self::std::*;
+
+// When alloc but not std is enabled, use those types
+#[cfg(all(feature = "alloc", not(feature = "std")))]
+pub use self::alloc::*;
+
+// When neither alloc or std is enabled, use a heapless fallback
+#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
+pub use self::core::*;
+
 #[cfg(feature = "std")]
 mod std {
-    pub type Box<A> = ::std::boxed::Box<A>;
-    pub type Map<K, V> = ::std::collections::HashMap<K, V>;
-    pub type Vec<A> = ::std::vec::Vec<A>;
-    pub type ChildrenVec<A> = ::std::vec::Vec<A>;
-    pub type ParentsVec<A> = ::std::vec::Vec<A>;
+    pub type Box<A> = std::boxed::Box<A>;
+    pub type Map<K, V> = std::collections::HashMap<K, V>;
+    pub type Vec<A> = std::vec::Vec<A>;
+    pub type ChildrenVec<A> = std::vec::Vec<A>;
+    pub type ParentsVec<A> = std::vec::Vec<A>;
 
     pub fn new_map_with_capacity<K, V>(capacity: usize) -> Map<K, V>
     where
-        K: Eq + ::std::hash::Hash,
+        K: Eq + std::hash::Hash,
     {
         Map::with_capacity(capacity)
     }
@@ -28,13 +40,15 @@ mod std {
     }
 }
 
-#[cfg(feature = "alloc")]
+#[cfg(all(feature = "alloc", not(feature = "std")))]
 mod alloc {
-    pub type Box<A> = ::alloc::boxed::Box<A>;
-    pub type Map<K, V> = ::hashbrown::HashMap<K, V>;
-    pub type Vec<A> = ::alloc::vec::Vec<A>;
-    pub type ChildrenVec<A> = ::alloc::vec::Vec<A>;
-    pub type ParentsVec<A> = ::alloc::vec::Vec<A>;
+    extern crate alloc;
+
+    pub type Box<A> = alloc::boxed::Box<A>;
+    pub type Map<K, V> = hashbrown::HashMap<K, V>;
+    pub type Vec<A> = alloc::vec::Vec<A>;
+    pub type ChildrenVec<A> = alloc::vec::Vec<A>;
+    pub type ParentsVec<A> = alloc::vec::Vec<A>;
 
     pub fn new_map_with_capacity<K, V>(capacity: usize) -> Map<K, V> {
         Map::with_capacity(capacity)
@@ -57,14 +71,14 @@ mod alloc {
 
 #[cfg(all(not(feature = "alloc"), not(feature = "std")))]
 mod core {
-    const MAX_NODE_COUNT: usize = 256;
-    const MAX_CHILD_COUNT: usize = 16;
-    const MAX_PARENTS_COUNT: usize = 1;
+    pub const MAX_NODE_COUNT: usize = 256;
+    pub const MAX_CHILD_COUNT: usize = 16;
+    pub const MAX_PARENTS_COUNT: usize = 1;
 
-    pub type Map<K, V> = ::heapless::FnvIndexMap<K, V, MAX_NODE_COUNT>;
-    pub type Vec<A> = ::arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
-    pub type ChildrenVec<A> = ::arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
-    pub type ParentsVec<A> = ::arrayvec::ArrayVec<A, MAX_PARENTS_COUNT>;
+    pub type Map<K, V> = heapless::FnvIndexMap<K, V, MAX_NODE_COUNT>;
+    pub type Vec<A> = arrayvec::ArrayVec<A, MAX_NODE_COUNT>;
+    pub type ChildrenVec<A> = arrayvec::ArrayVec<A, MAX_CHILD_COUNT>;
+    pub type ParentsVec<A> = arrayvec::ArrayVec<A, MAX_PARENTS_COUNT>;
 
     pub fn new_map_with_capacity<K, V>(_capacity: usize) -> Map<K, V>
     where
@@ -73,8 +87,8 @@ mod core {
         Map::new()
     }
 
-    pub fn new_vec_with_capacity<A, const CAP: usize>(_capacity: usize) -> ::arrayvec::ArrayVec<A, CAP> {
-        ::arrayvec::ArrayVec::new()
+    pub fn new_vec_with_capacity<A, const CAP: usize>(_capacity: usize) -> arrayvec::ArrayVec<A, CAP> {
+        arrayvec::ArrayVec::new()
     }
 
     #[inline]
@@ -87,10 +101,3 @@ mod core {
         num_traits::float::FloatCore::abs(value)
     }
 }
-
-#[cfg(feature = "alloc")]
-pub use self::alloc::*;
-#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
-pub use self::core::*;
-#[cfg(feature = "std")]
-pub use self::std::*;