Skip to content

Commit

Permalink
Drop in support for no_std float operations
Browse files Browse the repository at this point in the history
  • Loading branch information
dflemstr committed Dec 17, 2019
1 parent 619b2b1 commit 12ef285
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ arrayvec = { version = "0.5.1", default-features = false }
hash32 = "0.1.1"
hash32-derive = "0.1.0"
heapless = "0.5.1"
num-traits = { version = "0.2.10", default-features = false }
typenum = "1.11.2"

[dependencies.hashbrown]
Expand All @@ -34,7 +35,7 @@ optional = true
[features]
default = ["std"]
alloc = ["hashbrown"]
std = []
std = ["num-traits/std"]
serde_camel_case = ["serde"]
serde_kebab_case = ["serde"]

Expand Down
12 changes: 6 additions & 6 deletions src/algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ impl Forest {
let abs_x = abs_x + layout.location.x;
let abs_y = abs_y + layout.location.y;

layout.location.x = layout.location.x.round();
layout.location.y = layout.location.y.round();
layout.size.width = (abs_x + layout.size.width).round() - abs_x.round();
layout.size.height = (abs_y + layout.size.height).round() - abs_y.round();
layout.location.x = sys::round(layout.location.x);
layout.location.y = sys::round(layout.location.y);
layout.size.width = sys::round(abs_x + layout.size.width) - sys::round(abs_x);
layout.size.height = sys::round(abs_y + layout.size.height) - sys::round(abs_y);
for child in &children[root] {
Self::round_layout(nodes, children, *child, abs_x, abs_y);
}
Expand All @@ -141,13 +141,13 @@ impl Forest {
if let Some(ref cache) = self.nodes[node].layout_cache {
if cache.perform_layout || !perform_layout {
let width_compatible = if let Number::Defined(width) = node_size.width {
(width - cache.result.size.width).abs() < f32::EPSILON
sys::abs(width - cache.result.size.width) < f32::EPSILON
} else {
cache.node_size.width.is_undefined()
};

let height_compatible = if let Number::Defined(height) = node_size.height {
(height - cache.result.size.height).abs() < f32::EPSILON
sys::abs(height - cache.result.size.height) < f32::EPSILON
} else {
cache.node_size.height.is_undefined()
};
Expand Down
6 changes: 3 additions & 3 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ pub(crate) type NodeId = usize;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(not(any(feature = "std", feature = "alloc")), derive(hash32_derive::Hash32))]
pub(crate) struct Id(u64);
pub(crate) struct Id(usize);

pub(crate) struct Allocator {
new_id: atomic::AtomicU64,
new_id: atomic::AtomicUsize,
}

impl Allocator {
pub const fn new() -> Self {
Allocator { new_id: atomic::AtomicU64::new(0) }
Allocator { new_id: atomic::AtomicUsize::new(0) }
}

pub fn allocate(&self) -> Id {
Expand Down
30 changes: 30 additions & 0 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ mod std {
pub fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
Vec::with_capacity(capacity)
}

#[inline]
pub fn round(value: f32) -> f32 {
value.round()
}

#[inline]
pub fn abs(value: f32) -> f32 {
value.abs()
}
}

#[cfg(feature = "alloc")]
Expand All @@ -33,6 +43,16 @@ mod alloc {
pub fn new_vec_with_capacity<A>(capacity: usize) -> Vec<A> {
Vec::with_capacity(capacity)
}

#[inline]
pub fn round(value: f32) -> f32 {
num_traits::float::FloatCore::round(value)
}

#[inline]
pub fn abs(value: f32) -> f32 {
num_traits::float::FloatCore::abs(value)
}
}

#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
Expand Down Expand Up @@ -61,6 +81,16 @@ mod core {
{
::arrayvec::ArrayVec::new()
}

#[inline]
pub fn round(value: f32) -> f32 {
num_traits::float::FloatCore::round(value)
}

#[inline]
pub fn abs(value: f32) -> f32 {
num_traits::float::FloatCore::abs(value)
}
}

#[cfg(feature = "alloc")]
Expand Down

0 comments on commit 12ef285

Please sign in to comment.