serde_hashkey/float/
float_policy.rs

1use crate::float::FloatRepr;
2
3/// A policy for handling floating point types in a [Key].
4///
5/// Currently there are two important `FloatPolicy` types: [RejectFloatPolicy]
6/// and [OrderedFloat]. The former will emit errors instead of allowing floats
7/// to be serialized and the latter while serialize them and provide a total
8/// order which does not adhere to the IEEE standard.
9///
10/// [Key]: crate::Key
11/// [RejectFloatPolicy]: crate::RejectFloatPolicy
12/// [OrderedFloat]: crate::OrderedFloat
13///
14/// # Examples
15///
16/// Example using a non-default float policy:
17///
18/// ```
19/// use serde_hashkey::{Key, Float, to_key_with_ordered_float, OrderedFloat, OrderedFloatPolicy};
20///
21/// # fn main() -> Result<(), serde_hashkey::Error> {
22/// let a: Key<OrderedFloatPolicy> = to_key_with_ordered_float(&42.42f32)?;
23/// assert!(matches!(a, Key::Float(Float::F32(OrderedFloat(..)))));
24///
25/// let b: Key<OrderedFloatPolicy> = to_key_with_ordered_float(&42.42f64)?;
26/// assert!(matches!(b, Key::Float(Float::F64(OrderedFloat(..)))));
27/// # Ok(()) }
28/// ```
29pub trait FloatPolicy: self::private::Sealed {
30    /// The type encapsulating a 32-bit float, or `f32`.
31    type F32: FloatRepr<f32>;
32
33    /// The type encapsulating a 64-bit float, or `f64`.
34    type F64: FloatRepr<f64>;
35}
36
37// NB: we completely seal the FloatPolicy to prevent external implementations.
38mod private {
39    pub trait Sealed {}
40    impl<T> Sealed for T where T: super::FloatPolicy {}
41}