1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! Miscellaneous types used by the library.

#[cfg(feature = "either")]
pub use either::Either;
pub use maybe_owned::MaybeOwned;

mod callbacks;
pub(crate) use crate::types::callbacks::Callbacks;

mod storage;
pub(crate) use crate::types::storage::Storage;

/// Generic sum type of two elements.
///
/// It's used to provide generics over the `Option`/`Result`/`Either` types
pub trait SumType2 {
    /// Type of the first variant.
    type Type1;
    /// Type of the second variant.
    type Type2;

    /// Creates a value using the first variant.
    fn from_type1(val: Self::Type1) -> Self;
    /// Creates a value using the second variant.
    fn from_type2(val: Self::Type2) -> Self;

    /// Checks if the value is of the first variant.
    fn is_type1(&self) -> bool;
    /// Checks if the value is of the second variant.
    fn is_type2(&self) -> bool;

    /// Attempts to extract the value contained on the first variant.
    fn into_type1(self) -> Option<Self::Type1>;
    /// Attempts to extract the value contained on the second variant.
    fn into_type2(self) -> Option<Self::Type2>;
}

impl<T> SumType2 for Option<T> {
    type Type1 = T;
    type Type2 = ();

    fn from_type1(val: Self::Type1) -> Self {
        Some(val)
    }
    fn from_type2(_: Self::Type2) -> Self {
        None
    }

    fn is_type1(&self) -> bool {
        self.is_some()
    }
    fn is_type2(&self) -> bool {
        self.is_none()
    }

    fn into_type1(self) -> Option<Self::Type1> {
        self
    }
    fn into_type2(self) -> Option<Self::Type2> {
        self.ok_or(()).err()
    }
}

impl<T, E> SumType2 for Result<T, E> {
    type Type1 = T;
    type Type2 = E;

    fn from_type1(val: Self::Type1) -> Self {
        Ok(val)
    }
    fn from_type2(val: Self::Type2) -> Self {
        Err(val)
    }

    fn is_type1(&self) -> bool {
        self.is_ok()
    }
    fn is_type2(&self) -> bool {
        self.is_err()
    }

    fn into_type1(self) -> Option<Self::Type1> {
        self.ok()
    }
    fn into_type2(self) -> Option<Self::Type2> {
        self.err()
    }
}

#[cfg(feature = "either")]
impl<L, R> SumType2 for Either<L, R> {
    type Type1 = L;
    type Type2 = R;

    fn from_type1(val: Self::Type1) -> Self {
        Either::Left(val)
    }
    fn from_type2(val: Self::Type2) -> Self {
        Either::Right(val)
    }

    fn is_type1(&self) -> bool {
        self.is_left()
    }
    fn is_type2(&self) -> bool {
        self.is_right()
    }

    fn into_type1(self) -> Option<Self::Type1> {
        self.left()
    }
    fn into_type2(self) -> Option<Self::Type2> {
        self.right()
    }
}

/// Determines if the `Stream::observe` callback should be dropped or not.
pub trait ObserveResult {
    /// If it returns `true` the callback is kept, otherwise it's dropped.
    fn is_callback_alive(self) -> bool;
}

impl ObserveResult for () {
    /// No return value: never dropped.
    fn is_callback_alive(self) -> bool {
        true
    }
}

impl ObserveResult for bool {
    /// `bool` return value: dropped when it's `false`.
    fn is_callback_alive(self) -> bool {
        self
    }
}

impl<T> ObserveResult for Option<T> {
    /// `Option` return value: dropped when it's `None`.
    fn is_callback_alive(self) -> bool {
        self.is_some()
    }
}

impl<T, E> ObserveResult for Result<T, E> {
    /// `Result` return value: dropped when it's `Err`.
    fn is_callback_alive(self) -> bool {
        self.is_ok()
    }
}