Break out a sys module that encapsulates target-specific APIs
This module should contain any types or code that is different depending
on whether we're compiling for std, alloc or just core.
ffb470aade51878952bec921cf5b5405a838fa4c · por David Flemström · (hace 6 años) · padre 4acf6e960 · ver el árbol en este commit
diff --git a/Cargo.toml b/Cargo.toml
index 271404b3e..f130df418 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,9 +16,17 @@ circle-ci = { repository = "vislyhq/stretch", branch = "master" }
maintenance = { status = "experimental" }
[dependencies]
-hashbrown = "0.6.3"
+arrayvec = "0.5.1"
+hash32 = "0.1.1"
+hash32-derive = "0.1.0"
+heapless = "0.5.1"
lazy_static = "1.4"
spin = "0.5.2"
+typenum = "1.11.2"
+
+[dependencies.hashbrown]
+version = "0.6.3"
+optional = true
[dependencies.serde]
version = "1.0.102"
@@ -27,6 +35,7 @@ optional = true
[features]
default = ["std"]
+alloc = ["hashbrown"]
std = []
serde_camel_case = ["serde"]
serde_kebab_case = ["serde"]
diff --git a/src/lib.rs b/src/lib.rs
index 2e1284112..226ce569e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,6 +22,7 @@ mod algo;
mod forest;
mod id;
+mod sys;
pub use crate::node::Stretch;
use core::any::Any;
diff --git a/src/sys.rs b/src/sys.rs
new file mode 100644
index 000000000..40156fb1b
--- /dev/null
+++ b/src/sys.rs
@@ -0,0 +1,69 @@
+#[cfg(feature = "std")]
+mod std {
+ 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,
+ {
+ Map::with_capacity(capacity)
+ }
+
+ pub fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
+ Vec::with_capacity(capacity)
+ }
+}
+
+#[cfg(feature = "alloc")]
+mod alloc {
+ 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)
+ }
+
+ pub fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
+ Vec::with_capacity(capacity)
+ }
+}
+
+#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
+mod core {
+ use typenum::marker_traits::Unsigned;
+
+ type MaxNodeCount = heapless::consts::U256;
+ type MaxChildCount = heapless::consts::U16;
+ type MaxParentsCount = heapless::consts::U1;
+
+ pub type Map<K, V> = ::heapless::FnvIndexMap<K, V, MaxNodeCount>;
+ pub type Vec<A> = ::arrayvec::ArrayVec<[A; MaxNodeCount::USIZE]>;
+ pub type ChildrenVec<A> = ::arrayvec::ArrayVec<[A; MaxChildCount::USIZE]>;
+ pub type ParentsVec<A> = ::arrayvec::ArrayVec<[A; MaxParentsCount::USIZE]>;
+
+ pub fn new_map_with_capacity<K, V>(_capacity: usize) -> Map<K, V>
+ where
+ K: Eq + ::hash32::Hash,
+ {
+ Map::new()
+ }
+
+ pub fn new_vec_with_capacity<T, A>(_capacity: usize) -> ::arrayvec::ArrayVec<A>
+ where
+ A: ::arrayvec::Array<Item = T>,
+ {
+ ::arrayvec::ArrayVec::new()
+ }
+}
+
+#[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::*;