conv2/misc.rs
1//! This module defines some additional traits not *directly* tied to conversions.
2
3/// This trait indicates that values of a type can be logically "saturated".
4///
5/// This is used by the `errors::UnwrapOrSaturate` extension trait.
6pub trait Saturated {
7 /// Returns the type's saturated, maximum value.
8 fn saturated_max() -> Self;
9
10 /// Returns the type's saturated, minimum value.
11 fn saturated_min() -> Self;
12}
13
14item_for_each! {
15 (i8), (i16), (i32), (i64), (u8), (u16), (u32), (u64), (isize), (usize) => {
16 ($ity:ident) => {
17 impl Saturated for $ity {
18 #[inline] fn saturated_max() -> Self { $ity::MAX }
19 #[inline] fn saturated_min() -> Self { $ity::MIN }
20 }
21 };
22 }
23}
24
25/// This trait indicates that a type has an "invalid" sentinel value.
26///
27/// This is used by the `errors::UnwrapOrInvalid` extension trait.
28pub trait InvalidSentinel {
29 /// Returns the type's "invalid" sentinel value.
30 fn invalid_sentinel() -> Self;
31}
32
33item_for_each! {
34 (f32), (f64) => {
35 ($ity:ident) => {
36 impl InvalidSentinel for $ity {
37 #[inline] fn invalid_sentinel() -> Self { $ity::NAN }
38 }
39 };
40 }
41}
42
43/// This trait indicates that a type has positive and negative "infinity" values.
44///
45/// This is used by the `errors::UnwrapOrInf` extension trait.
46pub trait SignedInfinity {
47 /// Returns the type's positive infinity value.
48 fn neg_infinity() -> Self;
49
50 /// Returns the type's negative infinity value.
51 fn pos_infinity() -> Self;
52}
53
54item_for_each! {
55 (f32), (f64) => {
56 ($ity:ident) => {
57 impl SignedInfinity for $ity {
58 #[inline] fn neg_infinity() -> Self { $ity::NEG_INFINITY }
59 #[inline] fn pos_infinity() -> Self { $ity::INFINITY }
60 }
61 };
62 }
63}