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
use crate::reducer::*;
use std::sync::Arc;

/// Enhances a potentially _unsized_ [`Reducer`] with copy-on-write semantics (requires [`std`]).
///
/// Helps avoiding cloning the entire state when it needs to be sent to other threads,
/// e.g to the rendering thread of a GUI.
///
/// [`std`]: index.html#optional-features
///
/// # Example
///
/// ```rust
/// use reducer::*;
/// use std::sync::Arc;
///
/// #[derive(Clone)]
/// struct State { /* ... */ }
/// struct Action { /* ... */ }
///
/// impl Reducer<Action> for State {
///     fn reduce(&mut self, action: Action) {
///         // ...
///     }
/// }
///
/// let (tx, mut rx) = futures::channel::mpsc::channel(10);
///
/// let state = Arc::new(State { /* ... */ });
/// let reactor = Reactor::<Error = _>::from_sink(tx);
///
/// let mut store = Store::new(state, reactor);
///
/// store.dispatch(Action { /* ... */ }); // State is not cloned.
///
/// // The channel now holds a reference to the current state.
///
/// store.dispatch(Action { /* ... */ }); // State is cloned.
///
/// // Drain the channel so that it doesn't hold any references to the current state.
/// while let Ok(Some(s)) = rx.try_next() {
///     // Consume `s`.
/// }
///
/// store.dispatch(Action { /* ... */ }); // State is not cloned.
/// ```
impl<A, T> Reducer<A> for Arc<T>
where
    T: Reducer<A> + Clone + ?Sized,
{
    fn reduce(&mut self, action: A) {
        Arc::make_mut(self).reduce(action);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mock::*;
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn ok(actions: Vec<u8>) {
            let mut reducer = Arc::new(Mock::<_>::default());

            for (i, &action) in actions.iter().enumerate() {
                reduce(&mut reducer, action);
                assert_eq!(reducer.calls(), &actions[0..=i]);
            }
        }
    }

    proptest! {
        #[test]
        fn cow([a, b, c]: [u8; 3]) {
            let mut reducer = Arc::new(Mock::<_>::default());

            reduce(&mut reducer, a);
            assert_eq!(reducer.calls(), &[a]);
            assert_eq!(reducer.generation(), 0);

            let other = reducer.clone();

            assert_eq!(other.generation(), 0);
            assert_eq!(reducer.generation(), 0);

            reduce(&mut reducer, b);
            assert_eq!(reducer.calls(), &[a, b]);
            assert_eq!(reducer.generation(), 1);

            assert_eq!(other.calls(), &[a]);
            assert_eq!(other.generation(), 0);

            reduce(&mut reducer, c);
            assert_eq!(reducer.calls(), &[a, b, c]);
            assert_eq!(reducer.generation(), 1);

            assert_eq!(other.calls(), &[a]);
            assert_eq!(other.generation(), 0);
        }
    }
}