conv/
misc.rs

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