vec sequence: remove RwLock
This commit is contained in:
parent
0962b96c3a
commit
9125997c83
1 changed files with 14 additions and 34 deletions
|
@ -37,7 +37,7 @@ where T: Clone + Send + Sync + 'static {
|
||||||
/// Adapter View implementing `Sequence` for `Vec`
|
/// Adapter View implementing `Sequence` for `Vec`
|
||||||
pub struct VecSequence<T>
|
pub struct VecSequence<T>
|
||||||
where T: Clone + Send + Sync + 'static {
|
where T: Clone + Send + Sync + 'static {
|
||||||
cur_len: RwLock<usize>,
|
cur_len: usize,
|
||||||
data: Option<Arc<RwLock<Vec<T>>>>,
|
data: Option<Arc<RwLock<Vec<T>>>>,
|
||||||
cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = T>>>>
|
cast: Arc<RwLock<ObserverBroadcast<dyn SequenceView<Item = T>>>>
|
||||||
}
|
}
|
||||||
|
@ -158,7 +158,7 @@ where T: Clone + Send + Sync + 'static {
|
||||||
) -> Arc<RwLock<Self>> {
|
) -> Arc<RwLock<Self>> {
|
||||||
let seq = Arc::new(RwLock::new(
|
let seq = Arc::new(RwLock::new(
|
||||||
VecSequence {
|
VecSequence {
|
||||||
cur_len: RwLock::new(0),
|
cur_len: 0,
|
||||||
data: None,
|
data: None,
|
||||||
cast: port.get_broadcast()
|
cast: port.get_broadcast()
|
||||||
}
|
}
|
||||||
|
@ -171,55 +171,35 @@ where T: Clone + Send + Sync + 'static {
|
||||||
impl<T> Observer<RwLock<Vec<T>>> for VecSequence<T>
|
impl<T> Observer<RwLock<Vec<T>>> for VecSequence<T>
|
||||||
where T: Clone + Send + Sync + 'static {
|
where T: Clone + Send + Sync + 'static {
|
||||||
fn reset(&mut self, view: Option<Arc<RwLock<Vec<T>>>>) {
|
fn reset(&mut self, view: Option<Arc<RwLock<Vec<T>>>>) {
|
||||||
let old_len = self.len().unwrap();
|
let old_len = self.cur_len;
|
||||||
self.data = view;
|
self.data = view;
|
||||||
|
self.cur_len =
|
||||||
*self.cur_len.write().unwrap() =
|
|
||||||
if let Some(data) = self.data.as_ref() {
|
if let Some(data) = self.data.as_ref() {
|
||||||
data.read().unwrap().len()
|
data.read().unwrap().len()
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
let new_len = self.len().unwrap();
|
self.cast.notify_each(0 .. std::cmp::max(old_len, self.cur_len));
|
||||||
|
|
||||||
self.cast.notify_each(0 .. std::cmp::max(old_len, new_len));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn notify(&mut self, diff: &VecDiff<T>) {
|
fn notify(&mut self, diff: &VecDiff<T>) {
|
||||||
match diff {
|
match diff {
|
||||||
VecDiff::Clear => {
|
VecDiff::Clear => {
|
||||||
let l = {
|
self.cast.notify_each(0 .. self.cur_len);
|
||||||
let mut l = self.cur_len.write().unwrap();
|
self.cur_len = 0
|
||||||
let old_l = *l;
|
|
||||||
*l = 0;
|
|
||||||
old_l
|
|
||||||
};
|
|
||||||
self.cast.notify_each(0 .. l)
|
|
||||||
},
|
},
|
||||||
VecDiff::Push(_) => {
|
VecDiff::Push(_) => {
|
||||||
let l = {
|
self.cast.notify(&self.cur_len);
|
||||||
let mut l = self.cur_len.write().unwrap();
|
self.cur_len += 1;
|
||||||
*l += 1;
|
|
||||||
*l
|
|
||||||
};
|
|
||||||
self.cast.notify(&(l - 1));
|
|
||||||
},
|
},
|
||||||
VecDiff::Remove(idx) => {
|
VecDiff::Remove(idx) => {
|
||||||
let l = {
|
self.cast.notify_each(*idx .. self.cur_len);
|
||||||
let mut l = self.cur_len.write().unwrap();
|
self.cur_len -= 1;
|
||||||
*l -= 1;
|
|
||||||
*l + 1
|
|
||||||
};
|
|
||||||
self.cast.notify_each(*idx .. l);
|
|
||||||
},
|
},
|
||||||
VecDiff::Insert{ idx, val: _ } => {
|
VecDiff::Insert{ idx, val: _ } => {
|
||||||
let l = {
|
self.cur_len += 1;
|
||||||
let mut l = self.cur_len.write().unwrap();
|
self.cast.notify_each(*idx .. self.cur_len);
|
||||||
*l += 1;
|
|
||||||
*l
|
|
||||||
};
|
|
||||||
self.cast.notify_each(*idx .. l);
|
|
||||||
},
|
},
|
||||||
VecDiff::Update{ idx, val: _ } => {
|
VecDiff::Update{ idx, val: _ } => {
|
||||||
self.cast.notify(&idx);
|
self.cast.notify(&idx);
|
||||||
|
@ -244,7 +224,7 @@ where T: Clone + Send + Sync + 'static {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn len(&self) -> Option<usize> {
|
fn len(&self) -> Option<usize> {
|
||||||
Some(*self.cur_len.read().unwrap())
|
Some(self.cur_len)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue