Skip to content

Commit 03859f0

Browse files
authored
Defaults where sensible (Fix #93) (#95)
1 parent 66baff9 commit 03859f0

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,21 @@ To facilitate comparisons, the methods `is_positive_zero` and `is_negative_zero`
8989

9090
# Traits implemented
9191

92-
As none of the types of this crate can be `NaN`, the following traits are implemented on all 12 types:
92+
## On all 12 types
93+
94+
As none of the types of this crate can be `NaN`, the following traits are implemented on all types:
9395

9496
- [`core::cmp::Ord`]
9597
- [`core::cmp::Eq`]
9698
- [`core::hash::Hash`]
9799

98-
Note: for [`core::hash::Hash`] on [`NonNaN`] and [`NonNaNFinite`] there is a (small) overhead because they both accept `0.0` and `-0.0`, which are equal so they mush `core::hash::Hash` to the same value.
100+
Note: for [`core::hash::Hash`] on [`NonNaN`] and [`NonNaNFinite`] there is a (small) overhead because they both accept `0.0` and `-0.0`, which are equal so they must `core::hash::Hash` to the same value.
101+
102+
## Only on some types
103+
104+
- [`core::default::Default`]:
105+
- with the value `0.0` for [`NonNaN`], [`NonNaNFinite`], [`Positive`], and [`PositiveFinite`].
106+
- with the value `-0.0` for [`Negative`], and [`NegativeFinite`].
99107

100108
# Methods implemented
101109

@@ -243,6 +251,7 @@ Is on [docs.rs](https://docs.rs/typed_floats).
243251
[`core::f64::consts`]: https://doc.rust-lang.org/core/f64/consts/index.html
244252
[`core::cmp::Ord`]: https://doc.rust-lang.org/core/cmp/trait.Ord.html "`Ord`"
245253
[`core::cmp::Eq`]: https://doc.rust-lang.org/core/cmp/trait.Eq.html "`Eq`"
254+
[`core::default::Default`]: https://doc.rust-lang.org/core/core/default/trait.Default.html "`Default`"
246255
[`core::hash::Hash`]: https://doc.rust-lang.org/core/hash/trait.Hash.html "`Hash`"
247256
[`core::convert::From`]: https://doc.rust-lang.org/core/convert/trait.From.html "`From`"
248257
[`core::convert::TryFrom`]: https://doc.rust-lang.org/core/convert/trait.TryFrom.html "`TryFrom`"
@@ -277,4 +286,3 @@ Is on [docs.rs](https://docs.rs/typed_floats).
277286
[`Copysign`]: https://docs.rs/typed_floats/latest/typed_floats/trait.Copysign.html
278287
[`DivEuclid`]: https://docs.rs/typed_floats/latest/typed_floats/trait.DivEuclid.html
279288
[`Atan2`]: https://docs.rs/typed_floats/latest/typed_floats/trait.Atan2.html
280-
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#![allow(clippy::comparison_chain)]
2+
3+
use crate::{Negative, NegativeFinite, NonNaN, NonNaNFinite, Positive, PositiveFinite};
4+
5+
macro_rules! impl_default {
6+
($test:ident, $type:ident, $default:expr) => {
7+
impl core::default::Default for $type<f32> {
8+
fn default() -> Self {
9+
// # Safety
10+
// This is safe because the value is valid for that type.
11+
unsafe { Self::new_unchecked($default) }
12+
}
13+
}
14+
15+
impl core::default::Default for $type<f64> {
16+
fn default() -> Self {
17+
// # Safety
18+
// This is safe because the value is valid for that type.
19+
unsafe { Self::new_unchecked($default) }
20+
}
21+
}
22+
23+
#[cfg(test)]
24+
mod $test {
25+
use crate::{Negative, NegativeFinite, NonNaN, NonNaNFinite, Positive, PositiveFinite};
26+
27+
#[derive(Default)]
28+
struct SomeOptions {
29+
foo: f32,
30+
bar: f64,
31+
baz: $type<f32>,
32+
qux: $type<f64>,
33+
}
34+
35+
#[test]
36+
fn test() {
37+
let options: SomeOptions = Default::default();
38+
39+
// N.B. The assert succeeds if the value is `0.0` or `-0.0
40+
assert_eq!(options.foo, 0.0);
41+
assert_eq!(options.bar, 0.0);
42+
assert_eq!(options.baz.get(), 0.0);
43+
assert_eq!(options.qux.get(), 0.0);
44+
}
45+
}
46+
};
47+
}
48+
49+
impl_default!(non_nan, NonNaN, 0.0);
50+
impl_default!(non_nan_finite, NonNaNFinite, 0.0);
51+
impl_default!(positive, Positive, 0.0);
52+
impl_default!(negative, Negative, -0.0);
53+
impl_default!(positive_finite, PositiveFinite, 0.0);
54+
impl_default!(negative_finite, NegativeFinite, -0.0);

typed_floats/src/types/impls/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod default;
12
mod display;
23
mod eq;
34
mod from_str;

0 commit comments

Comments
 (0)