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
// Rx -- Reactive programming for Rust
// Copyright 2016 Ruud van Asseldonk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// A copy of the License has been included in the root of the repository.

use std::fmt::Debug;

/// An observer that receives values from an observable.
pub trait Observer<T, E> {
    /// Provides the observer with new data.
    fn on_next(&mut self, item: T);

    /// Notifies the observer that the provider has finished sending notifications.
    fn on_completed(self);

    /// Notifies the observer that the provider experienced an error condition.
    fn on_error(self, error: E);
}

pub struct NextObserver<FnNext> {
    pub fn_next: FnNext,
}

pub struct CompletedObserver<FnNext, FnCompleted> {
    pub fn_next: FnNext,
    pub fn_completed: FnCompleted,
}

pub struct ErrorObserver<FnNext, FnCompleted, FnError> {
    pub fn_next: FnNext,
    pub fn_completed: FnCompleted,
    pub fn_error: FnError,
}

pub struct OptionObserver<FnOption> {
    pub fn_option: FnOption
}

pub struct ResultObserver<FnResult> {
    pub fn_result: FnResult
}

impl<T, E, FnNext> Observer<T, E> for NextObserver<FnNext>
    where E: Debug, FnNext: FnMut(T) {

    fn on_next(&mut self, item: T) {
        self.fn_next.call_mut((item,));
    }

    fn on_completed(self) {
        // Ignore completion.
    }

    fn on_error(self, error: E) {
        panic!("observer received error: {:?}", error);
    }
}

impl<T, E, FnNext, FnCompleted> Observer<T, E> for CompletedObserver<FnNext, FnCompleted>
    where E: Debug, FnNext: FnMut(T), FnCompleted: FnOnce() {

    fn on_next(&mut self, item: T) {
        self.fn_next.call_mut((item,));
    }

    fn on_completed(self) {
        self.fn_completed.call_once(());
    }

    fn on_error(self, error: E) {
        panic!("observer received error: {:?}", error);
    }
}

impl<T, E, FnNext, FnCompleted, FnError>
    Observer<T, E> for ErrorObserver<FnNext, FnCompleted, FnError>
    where FnNext: FnMut(T), FnCompleted: FnOnce(), FnError: FnOnce(E) {

    fn on_next(&mut self, item: T) {
        self.fn_next.call_mut((item,));
    }

    fn on_completed(self) {
        self.fn_completed.call_once(());
    }

    fn on_error(self, error: E) {
        self.fn_error.call_once((error,));
    }
}

impl<T, E, FnOption> Observer<T, E> for OptionObserver<FnOption>
    where E: Debug, FnOption: FnMut(Option<T>) {

    fn on_next(&mut self, item: T) {
        self.fn_option.call_mut((Some(item),));
    }

    fn on_completed(mut self) {
        self.fn_option.call_mut((None,));
    }

    fn on_error(self, error: E) {
        panic!("observer received error: {:?}", error);
    }
}

impl<T, E, FnResult> Observer<T, E> for ResultObserver<FnResult>
    where FnResult: FnMut(Result<Option<T>, E>) {

    fn on_next(&mut self, item: T) {
        self.fn_result.call_mut((Ok(Some(item)),));
    }

    fn on_completed(mut self) {
        self.fn_result.call_mut((Ok(None),));
    }

    fn on_error(mut self, error: E) {
        self.fn_result.call_mut((Err(error),));
    }
}

/// Trait that enables using `Observer` as a trait object.
///
/// The methods `on_completed()` and `on_error()` cannot be called on trait objects,
/// because they take self by value, and the size of self is not known for a trait
/// object. Fortunately, this can be worked around by taking self as box instead of
/// by value. To keep the `Observer` trait simple, these methods have been moved here
/// with automatic implementations.
pub trait BoxedObserver<T, E>: Observer<T, E> {
    /// As `on_completed()`, but takes self as box so it can be called on a trait object.
    fn on_completed_box(self: Box<Self>);

    /// As `on_error()`, but takes self as box so it can be called on a trait object.
    fn on_error_box(self: Box<Self>, error: E);
}

impl<O, T, E> BoxedObserver<T, E> for O where O: Observer<T, E> {
    fn on_completed_box(self: Box<Self>) {
        self.on_completed();
    }

    fn on_error_box(self: Box<Self>, error: E) {
        self.on_error(error);
    }
}