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

use reactive::*;

pub use self::ActivateError::*;

#[derive(PartialOrd, Ord, PartialEq, Eq, Debug)]
pub enum ActivateError<T> {
	SourceActivationError(SourceError),
	SlotActivationError(SlotError<T>),
	NotConnected,
}

impl<T> From<SourceError> for ActivateError<T> {
	fn from(error: SourceError) -> Self { SourceActivationError(error) }
}
impl<T> From<SlotError<T>> for ActivateError<T> {
	fn from(error: SlotError<T>) -> Self { SlotActivationError(error) }
}

/// Represents activator which have an opportunity to become connected one
pub struct DisconnectedActivator;

impl<T, S> Sink<T, S> for DisconnectedActivator where S: Source<T> {
	type Connected = SourceConnectedActivator<T, S>;
	fn connect_source(self, src: S) -> SourceConnectedActivator<T, S> {
		SourceConnectedActivator(src, PhantomData)
	}
}

impl<T, S> Signal<T, S> for DisconnectedActivator where S: Slot<T> {
	type Connected = SlotConnectedActivator<T, S>;
	fn connect_slot(self, slot: S) -> SlotConnectedActivator<T, S> {
		SlotConnectedActivator(slot, PhantomData)
	}
}

/// Activator connected to `Source`
pub struct SourceConnectedActivator<T, S>(S, PhantomData<(T, S)>) where S: Source<T>;

impl<T, S, U> Signal<T, S> for SourceConnectedActivator<T, U> where S: Slot<T>, U: Source<T> {
	type Connected = ConnectedActivator<T, U, S>;
	fn connect_slot(self, slot: S) -> ConnectedActivator<T, U, S> {
		ConnectedActivator(self.0, slot, PhantomData)
	}
}


/// Activator connected to `Slot`
pub struct SlotConnectedActivator<T, S>(S, PhantomData<(T, S)>) where S: Slot<T>;

impl<T, S, U> Sink<T, S> for SlotConnectedActivator<T, U> where S: Source<T>, U: Slot<T> {
	type Connected = ConnectedActivator<T, S, U>;
	fn connect_source(self, src: S) -> ConnectedActivator<T, S, U> {
		ConnectedActivator(src, self.0, PhantomData)
	}
}


/// Fully connected activator
pub struct ConnectedActivator<T, S, U>(S, U, PhantomData<(T, S, U)>) where S: Source<T>, U: Slot<T>;

impl<T, S, U> ConnectedActivator<T, S, U> where S: Source<T>, U: Slot<T> {
	/// transfer portion of data from connected `Source` to `Slot`
	pub fn activate(&self) -> Result<(), ActivateError<T>> {
		let (ref src, ref slt) = (&self.0, &self.1);
		let value = try!(src.pull());
		try!(slt.push(value));
		Ok(())
	}
}


pub struct Activator<T, S, U> where S: Slot<T>, U: Source<T> {
	slot: Option<S>,
	source: Option<U>,
	pd: PhantomData<T>
}


impl<'a, T, S, U> Signal<T, S> for &'a mut Activator<T, S, U> where S: Slot<T>, U: Source<T> {
	type Connected = ();
	fn connect_slot(self, slot: S) {
		self.slot = Some(slot);
	}
}

impl<'a, T, S, U> Sink<T, U> for &'a mut Activator<T, S, U> where S: Slot<T>, U: Source<T> {
	type Connected = ();
	fn connect_source(self, source: U) {
		self.source = Some(source);
	}
}

impl<T, S, U> Activator<T, S, U> where S: Slot<T>, U: Source<T> {
	pub fn new() -> Activator<T, S, U> {
		Activator{slot: None, source: None, pd: PhantomData}
	}

	pub fn activate(&self) -> Result<(), ActivateError<T>> {
		if let (&Some(ref src), &Some(ref slt)) = (&self.source, &self.slot) {
			let value = try!(src.pull());
			try!(slt.push(value));
			Ok(())
		} else {
			Err(NotConnected)
		}
	}
}