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
150
use std::fmt::{Debug, Error, Formatter};
use std::ops::Deref;
use std::mem;
use traits;
use traits::Stream as StreamT;
use unique_stream::Stream as UniqueStream;
use iter::IterAsync;
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use parameter::Parameter;
#[must_use = "droping a property unsubscribes it and all the chain until the origin sender"]
pub struct UniqueProperty<'a, T: Clone + Debug + 'static>{
parent: Option<Box<UniqueStream<'a,T>>>,
value: T,
}
impl<'a,T: Clone + Debug + 'a> UniqueProperty<'a,T>{
pub fn new(initial: T) -> UniqueProperty<'a,T>{
UniqueProperty{
parent: None,
value: initial,
}
}
pub fn connect_stream<'b,'c, S: Into<UniqueStream<'b,T>>>(&'c mut self, stream: S) where 'c: 'b, 'a: 'b{
let value: &mut T = &mut self.value;
let stream = stream.into();
let next_parent = match self.parent{
Some(ref mut parent) => {
let prev_parent = mem::replace(parent, unsafe{ mem::uninitialized() });
prev_parent.merge(stream.on_value(move |v| *value = v))
}
None => {
stream.on_value(move |v| *value = v)
}
};
let next_parent: Box<UniqueStream<T>> = Box::new(next_parent);
let next_parent: Box<UniqueStream<T>> = unsafe{ mem::transmute(next_parent) };
let _drop_previous = mem::replace(&mut self.parent, Some(next_parent));
}
pub fn value(&self) -> &T{
&self.value
}
pub fn to_value(&self) -> T{
self.value.clone()
}
pub fn into_value(self) -> T{
self.value
}
pub fn set(&mut self, t: T){
if let Some(ref p) = self.parent{
p.send(t);
}else{
self.value = t;
}
}
pub fn stream(self) -> UniqueStream<'a, T>{
self.parent.map(|st| *st).unwrap_or_else(|| UniqueStream::never())
}
}
impl<'a,'b,T: Clone + Debug> UniqueProperty<'a,Vec<T>>{
pub fn push(&mut self, t: T){
let mut current = (**self).clone();
current.push(t);
self.set(current);
}
}
impl<'a,'b,T: Clone + Debug + 'a> Deref for UniqueProperty<'a,T>{
type Target = T;
fn deref(&self) -> &T{
self.value()
}
}
impl<'a,T:Clone + Debug + 'a> traits::Property<'a,T> for UniqueProperty<'a,T>{
fn iter_async<'b>(self) -> IterAsync<'b,T> where 'a: 'b{
let initial = self.to_value();
IterAsync::new_initial(self.stream(), initial)
}
fn to_parameter<'b>(self) -> Parameter<'b,T> where 'a: 'b{
let initial = self.to_value();
Parameter::from_parent(self.iter_async(), initial)
}
}
impl<'a,'b,T: Clone + Debug> AsRef<T> for UniqueProperty<'a,T>{
fn as_ref(&self) -> &T{
self
}
}
impl<'a,'b,T: Clone + Debug + 'a> Debug for UniqueProperty<'a,T>{
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>{
f.write_str(&format!("Property{{ value: {:?} }}", self.value))
}
}
impl<'a,'b,T: Clone + Debug + Serialize + 'a> Serialize for UniqueProperty<'a,T>{
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>{
self.value.serialize(serializer)
}
}
impl<'a,'b, 'de, T: Clone + Debug + Deserialize<'de> + 'a> Deserialize<'de> for UniqueProperty<'a,T>{
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<UniqueProperty<'a,T>, D::Error>{
T::deserialize(deserializer).map(|t| UniqueProperty::new(t))
}
}
impl<'a,'b,T: Clone + Debug + Default> Default for UniqueProperty<'a,T>{
fn default() -> UniqueProperty<'a,T>{
UniqueProperty::new(T::default())
}
}