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
use std::marker::PhantomData;
use reactive::*;

pub struct MergedSignals<S, Y>(S, Y);

/// Merge two signals to act as one
pub fn merge<S, Y>(left: S, right: Y) -> MergedSignals<S, Y> {
	MergedSignals(left, right)
}

impl<T, S, Y, X> Signal<T, X> for MergedSignals<S, Y> where S: Signal<T, X>, Y: Signal<T, X>, X: Clone + Slot<T> {
	type Connected = (S::Connected, Y::Connected);
	fn connect_slot(self, slot: X) -> (S::Connected, Y::Connected) {
		(self.0.connect_slot(slot.clone()), self.1.connect_slot(slot))
	}
}




pub trait SignalExt: Sized {
	fn map<T, R, F>(self, f: F) -> MappedSignal<T, R, Self, F> where F: Fn(T)->R, T: Clone { MappedSignal(self, f, PhantomData) }
}

pub struct MappedSignal<T, R, X, F>(X, F, PhantomData<(T, R)>);
pub struct MappedSlot<T, R, X, F>(X, F, PhantomData<(T, R)>);

impl<T, R, S, X, F> Signal<R, S> for MappedSignal<T, R, X, F> where F: Fn(T)->R, X: Signal<T, MappedSlot<T, R, S, F>>, S: Slot<R>, T: Clone {
	type Connected = X::Connected;
	fn connect_slot(self, slot: S) -> Self::Connected {
		self.0.connect_slot(MappedSlot(slot, self.1, PhantomData))
	}
}

impl<T, R, X, F> Slot<T> for MappedSlot<T, R, X, F> where F: Fn(T)->R, X: Slot<R>, T: Clone {
	fn push(&self, value: T) -> Result<(), SlotError<T>> {
		self.0.push((self.1)(value.clone())).map_err(|err| 
			match err {
				Full(_) => Full(value),
				SomeFull => SomeFull
			}
		)
	}
}