Skip to content

Commit d4cef69

Browse files
committed
Add more high-level documentation and examples
1 parent cd01fca commit d4cef69

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

src/lib.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![cfg_attr(test, deny(warnings))]
33
#![deny(missing_docs)]
44

5-
//! Wrappers for total order on Floats.
5+
//! Wrappers for total order on Floats. See the [`OrderedFloat`] and [`NotNan`] docs for details.
66
77
#[cfg(feature = "std")] extern crate std;
88
#[cfg(feature = "std")] use std::error::Error;
@@ -34,10 +34,32 @@ const MAN_MASK: u64 = 0x000fffffffffffffu64;
3434
const CANONICAL_NAN_BITS: u64 = 0x7ff8000000000000u64;
3535
const CANONICAL_ZERO_BITS: u64 = 0x0u64;
3636

37-
/// A wrapper around Floats providing an implementation of Ord and Hash.
37+
/// A wrapper around floats providing implementations of `Eq`, `Ord`, and `Hash`.
3838
///
3939
/// NaN is sorted as *greater* than all other values and *equal*
4040
/// to itself, in contradiction with the IEEE standard.
41+
///
42+
/// ```
43+
/// use ordered_float::OrderedFloat;
44+
/// use std::f32::NAN;
45+
///
46+
/// let mut v = [OrderedFloat(NAN), OrderedFloat(2.0), OrderedFloat(1.0)];
47+
/// v.sort();
48+
/// assert_eq!(v, [OrderedFloat(1.0), OrderedFloat(2.0), OrderedFloat(NAN)]);
49+
/// ```
50+
///
51+
/// Because `OrderedFloat` implements `Ord` and `Eq`, it can be used as a key in a `HashSet`,
52+
/// `HashMap`, `BTreeMap`, or `BTreeSet` (unlike the primitive `f32` or `f64` types):
53+
///
54+
/// ```
55+
/// # use ordered_float::OrderedFloat;
56+
/// # use std::collections::HashSet;
57+
/// # use std::f32::NAN;
58+
///
59+
/// let mut s: HashSet<OrderedFloat<f32>> = HashSet::new();
60+
/// s.insert(OrderedFloat(NAN));
61+
/// assert!(s.contains(&OrderedFloat(NAN)));
62+
/// ```
4163
#[derive(Debug, Default, Clone, Copy)]
4264
#[repr(transparent)]
4365
pub struct OrderedFloat<T>(pub T);
@@ -455,9 +477,44 @@ impl<T: Float + Num> Num for OrderedFloat<T> {
455477
}
456478
}
457479

458-
/// A wrapper around Floats providing an implementation of Ord and Hash.
480+
/// A wrapper around floats providing an implementation of `Eq`, `Ord` and `Hash`.
459481
///
460482
/// A NaN value cannot be stored in this type.
483+
///
484+
/// ```
485+
/// use ordered_float::NotNan;
486+
///
487+
/// let mut v = [
488+
/// NotNan::new(2.0).unwrap(),
489+
/// NotNan::new(1.0).unwrap(),
490+
/// ];
491+
/// v.sort();
492+
/// assert_eq!(v, [1.0, 2.0]);
493+
/// ```
494+
///
495+
/// Because `NotNan` implements `Ord` and `Eq`, it can be used as a key in a `HashSet`,
496+
/// `HashMap`, `BTreeMap`, or `BTreeSet` (unlike the primitive `f32` or `f64` types):
497+
///
498+
/// ```
499+
/// # use ordered_float::NotNan;
500+
/// # use std::collections::HashSet;
501+
///
502+
/// let mut s: HashSet<NotNan<f32>> = HashSet::new();
503+
/// let key = NotNan::new(1.0).unwrap();
504+
/// s.insert(key);
505+
/// assert!(s.contains(&key));
506+
/// ```
507+
///
508+
/// Arithmetic on NotNan values will panic if it produces a NaN value:
509+
///
510+
/// ```should_panic
511+
/// # use ordered_float::NotNan;
512+
/// let a = NotNan::new(std::f32::INFINITY).unwrap();
513+
/// let b = NotNan::new(std::f32::NEG_INFINITY).unwrap();
514+
///
515+
/// // This will panic:
516+
/// let c = a + b;
517+
/// ```
461518
#[derive(PartialOrd, PartialEq, Debug, Default, Clone, Copy)]
462519
#[repr(transparent)]
463520
pub struct NotNan<T>(T);

0 commit comments

Comments
 (0)