Skip to content

Commit 11a6035

Browse files
authored
Merge pull request #54 from bassmanitram/feature/ordered-float
Implement feature "ordered-float"
2 parents bc7dd2c + 41a58b4 commit 11a6035

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ itoa = "1"
1616
ryu = "1"
1717
halfbrown = { version = "0.2", optional = true }
1818
float-cmp = "0.9"
19+
ordered-float = { version = "4", optional = true }
1920
hashbrown = { version = "0.14", optional = true }
2021
abi_stable = { version = "0.11.0", optional = true, default-features = false }
2122

@@ -27,11 +28,14 @@ default = ["custom-types", "halfbrown", "runtime-detection"]
2728
# Support for custom types
2829
custom-types = []
2930

30-
# Support for abi-stable's `StableAbi` implementation
31+
# Support for abi-stable's `StableAbi` implementation - INCOMPATIBLE WITH ordered-float
3132
c-abi = ["abi_stable"]
3233

3334
# use runtime detection of the CPU features where possible instead of enforcing an instruction set
3435
runtime-detection = []
3536

37+
# wrap floats in ordered-float - allowing them to be Eq - INCOMPATIBLE WITH c-abi
38+
ordered-float = ["dep:ordered-float"]
39+
3640
# portable simd support (as of rust 1.73 nightly only)
3741
portable = []

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ compile_error!(
2323
the `128bit` feature, and that they have been merged by Cargo."
2424
);
2525

26+
#[cfg(all(feature = "c-abi", feature = "ordered-float"))]
27+
compile_error!("Combining the features `c-abi` and `ordered-float` is impossible because ordered_float::OrderedFloat does not implement `StableAbi`");
28+
2629
use std::borrow::Cow;
2730
use std::fmt;
2831

src/node.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
use crate::base::{TypedValue, ValueAsScalar};
22

33
use super::{fmt, ValueType};
4-
use float_cmp::approx_eq;
54
use std::convert::TryFrom;
65
use std::ops::{Index, IndexMut};
76

7+
#[cfg(feature = "ordered-float")]
8+
use ordered_float::OrderedFloat;
9+
10+
#[cfg(not(feature = "ordered-float"))]
11+
use float_cmp::approx_eq;
12+
813
mod cmp;
914
mod from;
1015

1116
/// Static tape node
1217
#[derive(Debug, Clone, Copy)]
18+
#[cfg_attr(feature = "ordered-float", derive(Eq))]
1319
#[cfg_attr(feature = "c-abi", repr(C))]
1420
#[cfg_attr(feature = "c-abi", derive(abi_stable::StableAbi))]
21+
1522
pub enum StaticNode {
1623
/// A signed 64 bit integer.
1724
I64(i64),
@@ -24,7 +31,11 @@ pub enum StaticNode {
2431
/// An unsigned 128 bit integer.
2532
U128(u128),
2633
/// A floating point value
34+
#[cfg(not(feature = "ordered-float"))]
2735
F64(f64),
36+
#[cfg(feature = "ordered-float")]
37+
/// A floating point value, as an `OrderedFloat` (not allow for Eq)
38+
F64(OrderedFloat<f64>),
2839
/// A boolean value
2940
Bool(bool),
3041
/// The null value
@@ -191,7 +202,8 @@ impl ValueAsScalar for StaticNode {
191202
#[must_use]
192203
fn as_f64(&self) -> Option<f64> {
193204
match self {
194-
Self::F64(i) => Some(*i),
205+
#[allow(clippy::useless_conversion)] // .into() required by ordered-float
206+
Self::F64(i) => Some((*i).into()),
195207
_ => None,
196208
}
197209
}
@@ -202,7 +214,8 @@ impl ValueAsScalar for StaticNode {
202214
#[allow(clippy::cast_precision_loss)]
203215
fn cast_f64(&self) -> Option<f64> {
204216
match self {
205-
Self::F64(i) => Some(*i),
217+
#[allow(clippy::useless_conversion)] // .into() required by ordered-float
218+
Self::F64(i) => Some((*i).into()),
206219
Self::I64(i) => Some(*i as f64),
207220
Self::U64(i) => Some(*i as f64),
208221
_ => None,
@@ -214,7 +227,7 @@ impl ValueAsScalar for StaticNode {
214227
#[allow(clippy::cast_precision_loss)]
215228
fn cast_f64(&self) -> Option<f64> {
216229
match self {
217-
Self::F64(i) => Some(*i),
230+
Self::F64(i) => Some((*i).into()),
218231
Self::I64(i) => Some(*i as f64),
219232
Self::U64(i) => Some(*i as f64),
220233
Self::I128(i) => Some(*i as f64),
@@ -238,7 +251,10 @@ impl fmt::Display for StaticNode {
238251
Self::Bool(b) => write!(f, "{b}"),
239252
Self::I64(n) => write!(f, "{n}"),
240253
Self::U64(n) => write!(f, "{n}"),
254+
#[cfg(not(feature = "ordered-float"))]
241255
Self::F64(n) => write!(f, "{n}"),
256+
#[cfg(feature = "ordered-float")]
257+
Self::F64(n) => write!(f, "{}", n.0),
242258
}
243259
}
244260
#[cfg(feature = "128bit")]
@@ -248,7 +264,10 @@ impl fmt::Display for StaticNode {
248264
Self::Bool(b) => write!(f, "{b}"),
249265
Self::I64(n) => write!(f, "{n}"),
250266
Self::U64(n) => write!(f, "{n}"),
267+
#[cfg(not(feature = "ordered-float"))]
251268
Self::F64(n) => write!(f, "{n}"),
269+
#[cfg(feature = "ordered-float")]
270+
Self::F64(n) => write!(f, "{}", n.0),
252271
Self::I128(n) => write!(f, "{n}"),
253272
Self::U128(n) => write!(f, "{n}"),
254273
}
@@ -264,7 +283,10 @@ impl PartialEq for StaticNode {
264283
match (self, other) {
265284
(Self::Null, Self::Null) => true,
266285
(Self::Bool(v1), Self::Bool(v2)) => v1.eq(v2),
286+
#[cfg(not(feature = "ordered-float"))]
267287
(Self::F64(v1), Self::F64(v2)) => approx_eq!(f64, *v1, *v2),
288+
#[cfg(feature = "ordered-float")]
289+
(Self::F64(v1), Self::F64(v2)) => v1.eq(v2),
268290
(Self::U64(v1), Self::U64(v2)) => v1.eq(v2),
269291
(Self::I64(v1), Self::I64(v2)) => v1.eq(v2),
270292
(Self::U64(v1), Self::I64(v2)) if *v2 >= 0 => (*v2 as u64).eq(v1),
@@ -280,7 +302,10 @@ impl PartialEq for StaticNode {
280302
match (self, other) {
281303
(Self::Null, Self::Null) => true,
282304
(Self::Bool(v1), Self::Bool(v2)) => v1.eq(v2),
305+
#[cfg(not(feature = "ordered-float"))]
283306
(Self::F64(v1), Self::F64(v2)) => approx_eq!(f64, *v1, *v2),
307+
#[cfg(feature = "ordered-float")]
308+
(Self::F64(v1), Self::F64(v2)) => v1.eq(v2),
284309
(Self::U64(v1), Self::U64(v2)) => v1.eq(v2),
285310
(Self::U128(v1), Self::U128(v2)) => v1.eq(v2),
286311
(Self::I64(v1), Self::I64(v2)) => v1.eq(v2),

src/node/from.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(feature = "ordered-float")]
2+
use ordered_float::OrderedFloat;
3+
14
use crate::StaticNode;
25

36
/********* atoms **********/
@@ -110,6 +113,7 @@ impl From<usize> for StaticNode {
110113
}
111114

112115
/********* f_ **********/
116+
#[cfg(not(feature = "ordered-float"))]
113117
impl From<f32> for StaticNode {
114118
#[inline]
115119
#[must_use]
@@ -118,10 +122,29 @@ impl From<f32> for StaticNode {
118122
}
119123
}
120124

125+
#[cfg(not(feature = "ordered-float"))]
121126
impl From<f64> for StaticNode {
122127
#[inline]
123128
#[must_use]
124129
fn from(f: f64) -> Self {
125130
Self::F64(f)
126131
}
127132
}
133+
134+
#[cfg(feature = "ordered-float")]
135+
impl From<f32> for StaticNode {
136+
#[inline]
137+
#[must_use]
138+
fn from(f: f32) -> Self {
139+
Self::F64(OrderedFloat::from(f64::from(f)))
140+
}
141+
}
142+
143+
#[cfg(feature = "ordered-float")]
144+
impl From<f64> for StaticNode {
145+
#[inline]
146+
#[must_use]
147+
fn from(f: f64) -> Self {
148+
Self::F64(OrderedFloat::from(f))
149+
}
150+
}

0 commit comments

Comments
 (0)